argsy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +24 -0
  3. data/README.md +111 -0
  4. data/argsy.gemspec +17 -0
  5. data/lib/argsy.rb +17 -0
  6. metadata +48 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 88526113ba2dd53ad74e38032ff79b7779e1aed1d45f7717405e850cc4b5ba83
4
+ data.tar.gz: a4a5fc03b8c24c355cd8dd9b59db891b4c90d36846f07602742f1ed46fa072ac
5
+ SHA512:
6
+ metadata.gz: 2f64292c7aea41e91ba44eb0cde25aab733282aba5744e2b396a9e907b1501d892385d4dda3244fdc5c88a2f033b1c2de6016f2be10ecc840b2615615b60ab1e
7
+ data.tar.gz: '0789bbc4c7047c9e696f913b1868d1d7dd9ca1b77bbbcfe9d04b911f49bdee3a1524a85b6ff6ebbdab08eeda2320b6182538952cfd57ce557a10cd72e2b0e944'
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org>
@@ -0,0 +1,111 @@
1
+ # Argsy
2
+
3
+ Tiny helper snippet you can paste into your CLI script(s) to obtain "commands + options" DSL without
4
+ external dependencies.
5
+
6
+ [![Build](https://travis-ci.org/kolotaev/argsy.svg?branch=master)](https://travis-ci.org/kolotaev/argsy)
7
+ [![License](https://upload.wikimedia.org/wikipedia/commons/e/ee/Unlicense_Blue_Badge.svg)](https://raw.githubusercontent.com/kolotaev/argsy/master/LICENSE.txt)
8
+
9
+ ## Rationale
10
+
11
+ As you already know, Ruby is awesome for scripting and any sort of CLI tools: it gives you a superb OS commands
12
+ interaction interface, JSON/YAML parsing, HTTP tools, etc... All out of the box! But built-in `OptionParser` doesn't
13
+ provide easy-to-write functionality to create CLIs like `./script command --option 1`. The problem here is: how to
14
+ declare command, action and options conveniently?
15
+
16
+ > Sometimes, for any imaginable reason, you do not want you script to be dependent on external gems (say, you want to
17
+ > pass a script that "just works!" to your co-workers without telling them "Hey, you also need to install this or that
18
+ > gem")
19
+
20
+ `Argsy` gives you such possibility. It's [just 17 lines of code](lib/argsy.rb), published under `The Unlicense`,
21
+ so you can just copy-paste it inside your script and have a nice
22
+ tiny DSL. You can of course use it as a gem as well, if you want to.
23
+
24
+ **Disclaimer**: `Argsy` doesn't claim itself as a profound CLI DSL like `Thor` and the company,
25
+ thus for complex scripts, please, opt for those gems instead.
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ Argsy.new '1.0.0' do
31
+ command :list, 'List all files' do
32
+ action do |o|
33
+ puts %w(. .. .hidden.py).join("\n") if o[:all]
34
+ puts %w(todos.txt recipes.doc).join("\n")
35
+ puts "me.#{o[:extension]}" if o[:extension]
36
+ end
37
+ options do
38
+ on '-a', '--all', 'Show all, even hidden files'
39
+ on '-e', '--extension EXTENSION', 'List available files with extension'
40
+ end
41
+ end
42
+
43
+ command 'print', 'Print all given options' do
44
+ action &method(:puts)
45
+ options do
46
+ on '-a', '--A', 'A option'
47
+ on '-b', 'B option'
48
+ end
49
+ end
50
+
51
+ command 'hello:world', 'Greet the World group without options' do
52
+ action { puts 'Hello world!' }
53
+ end
54
+
55
+ command :post do |c|
56
+ action do |opts|
57
+ puts "I'm a command without description"
58
+ puts "Posting to facebook: #{opts[:message]}" if opts[:facebook]
59
+ puts "Posting to twitter: You're Tearing Me Apart, Lisa!" if opts[:twitter]
60
+ puts "Date and time posted: #{opts[:date]}"
61
+ end
62
+ options do
63
+ require 'optparse/time'
64
+ on_tail '-d', '--date=DATE', Time, 'Date when message was posted'
65
+ on '-f', '--facebook' do
66
+ c.opts[:message] = 'Oh, Hi Mark'
67
+ end
68
+ on '-t', '--twitter'
69
+ end
70
+ end
71
+ end.run
72
+ ```
73
+
74
+ In console:
75
+ ```
76
+ $ ./example
77
+ Usage: playground CMD [--help] [--version]
78
+ list List all files
79
+ print Print all given options
80
+ hello Greet the World without options
81
+ post
82
+
83
+ $ ./example post --help
84
+ Usage: playground post [options] [--help]
85
+ -f, --facebook
86
+ -t, --twitter
87
+ -d, --date=DATE Date when message was posted
88
+
89
+ $ ./example post -f -d 12-8-2016
90
+ I'm a command without description
91
+ Posting to facebook: Oh, Hi Mark
92
+ Date and time posted: 2016-08-12 00:00:00 +0300
93
+
94
+
95
+ $ ./example post --version
96
+ example version 1.0.0
97
+ ```
98
+
99
+ ## FAQ
100
+
101
+ | Q: | A: |
102
+ | ------------- |-------------|
103
+ | Does it support groups/subcommands? | Only in a way of declaring commands as `'group:subcommand'` |
104
+ | What can I do inside options block? | Anything that is possible inside a stdlib's `OptionParser` |
105
+ | Why not Thor, Commander, etc.? | See rationale |
106
+ | Can I use it as a gem? | Yes. `gem install argsy` |
107
+ | Why does it look like a minified jQuery? | Yes, it's not an idiomatic Ruby formatting. I wanted to make it terse, still readable |
108
+ | Copy-pasting is bad. It's not DRY! | Absolutely agree with you |
109
+
110
+ ## License
111
+ The Unlicense
@@ -0,0 +1,17 @@
1
+ lib = 'lib/argsy.rb'
2
+
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.authors = ['Egor Kolotaev']
7
+ spec.summary = 'Tiny "commands & options" DSL for your CLI scripts'
8
+ spec.email = %w(ekolotaev@gmail.com)
9
+ spec.homepage = 'https://github.com/kolotaev/argsy'
10
+ spec.license = 'The Unlicense'
11
+ spec.name = 'argsy'
12
+ spec.require_paths = [lib]
13
+ spec.required_ruby_version = '>= 2.4.5'
14
+ spec.description = spec.summary
15
+ spec.files = %w(LICENSE.txt README.md argsy.gemspec) + Dir[lib]
16
+ spec.version = '0.1.0'
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'optparse'
2
+ class Argsy
3
+ class Command
4
+ attr_reader :name, :desc, :do_it, :op, :opts
5
+ def initialize(name, desc, &bl) @name, @desc, @op, @opts = name, desc, OptionParser.new, {}; instance_eval(&bl) end
6
+ def options(&bl) @op.banner = "Usage: #{op.program_name} #{@name} [options] [--help]"; @op.instance_eval(&bl) end
7
+ def action(&block) @do_it = block end
8
+ def to_s() ' ' * 4 + @name + ' ' * (33 - @name.length).abs + @desc end
9
+ end
10
+ def initialize(ver='0.0.1', &bl) @name = File.basename($0, '.*'); @ver = ver; @cmds = {}; instance_eval(&bl) end
11
+ def command(name, desc='', &bl) @cmds[name.to_s] = Command.new(name.to_s, desc, &bl) end
12
+ def run(a=ARGV)
13
+ (puts "#{@name} version #{@ver}"; exit) if a == ['--version']
14
+ (puts "Usage: #{@name} CMD [--help] [--version]\n" + @cmds.values.map(&:to_s).join("\n"); exit) if !@cmds.key?(a[0])
15
+ c = @cmds[a[0]]; c.op.parse(a, into: c.opts); c.do_it.call(c.opts)
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: argsy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Egor Kolotaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tiny "commands & options" DSL for your CLI scripts
14
+ email:
15
+ - ekolotaev@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - argsy.gemspec
23
+ - lib/argsy.rb
24
+ homepage: https://github.com/kolotaev/argsy
25
+ licenses:
26
+ - The Unlicense
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib/argsy.rb
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.4.5
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.7.6
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Tiny "commands & options" DSL for your CLI scripts
48
+ test_files: []