mcli 0.4.0 → 0.9.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46a66749c3036b67941be0bd467f468a50bed5c65bf6339fa38e4f6860ae1576
4
- data.tar.gz: ee8160680bd44f9e119b5703ac336e5794996ae86e75882e5c16209e524b3530
3
+ metadata.gz: ecec8bb6514b3d3a92a27fdff45e246b0974770648d0f683be216eecfac14856
4
+ data.tar.gz: 274fcd6a72fa0925731e16e3c0f24e0671f203af503b11e1841c0f3236c76617
5
5
  SHA512:
6
- metadata.gz: 0aff53ff9cb2ac7cb05f984348851e6697cdba511fd3580be155d3fff4c3fc532569d18d8f6193634d6c11876938f5ecafea2605e98c2eaf62d171358febd261
7
- data.tar.gz: a1523561c64404be6211ef640f667fb8932d9e31eb50fff3f06fddf8a9c0134dff1c7066d6dae681ec074596ce49769e909ed325e77dc682b75bc5aa236a7e51
6
+ metadata.gz: c55e84020a939b109b1241303cdd6ca387e91d3bc45c5b73c503a1db7b3ffb078f801c47b44b3b5398d4be8fda3c667af39c54261dedb77b4bbb46346e0bd88b
7
+ data.tar.gz: 87217cb01f495f7646c4b8969cea3ac8e1e87c273e98e92a88de7449d8648c5f588172edad239a30beccb6e81c180bcdd605eee8b574b297abccf4912f1a3785
data/README.md CHANGED
@@ -200,7 +200,7 @@ Heads: false
200
200
  #!/usr/bin/env ruby
201
201
  require 'mcli'
202
202
 
203
- class Options < MCLI::Command
203
+ class Capture < MCLI::Command
204
204
  register_as :capture
205
205
  capture_all!
206
206
 
@@ -217,6 +217,70 @@ $ ./capture.rb capture --toast=cool one two -a ok three
217
217
  ["--toast=cool", "one", "two", "-a", "ok", "three"]
218
218
  ```
219
219
 
220
+ ##### Register a command as the root command
221
+ ```ruby
222
+ #root.rb
223
+ ```
224
+ ```ruby
225
+ #!/usr/bin/env ruby
226
+ require 'mcli'
227
+
228
+ class Root < MCLI::Command
229
+ register_as_root
230
+ option 'option'
231
+
232
+ def run
233
+ puts "#{arg.inspect}"
234
+ puts "#{options[:root]}"
235
+ end
236
+ end
237
+
238
+ MCLI.run
239
+ ```
240
+
241
+ ```bash
242
+ $ ./root.rb arg --option opt
243
+ [arg]
244
+ opt
245
+ ```
246
+
247
+ ##### Register nested commands
248
+ ```ruby
249
+ #nestable.rb
250
+ ```
251
+ ```ruby
252
+ #!/usr/bin/env ruby
253
+ require 'mcli'
254
+
255
+ class Nestable < MCLI::Command
256
+ register_as :nestable
257
+ capture_all!
258
+
259
+ def run
260
+ NestedGroup.call(args)
261
+ end
262
+
263
+ class NestedGroup < MCLI::CommandGroup
264
+ end
265
+ end
266
+
267
+ class NestedCommand < MCLI::Command
268
+ register_as :command, to: Nestable::Group
269
+ capture_all!
270
+
271
+ def run
272
+ puts args.inspect
273
+ end
274
+ end
275
+
276
+ MCLI.run
277
+ ```
278
+
279
+ ```bash
280
+ $ ./nestable.rb nestable command --toast=cool one two -a ok three
281
+ ["--toast=cool", "one", "two", "-a", "ok", "three"]
282
+ ```
283
+
220
284
  ## Development
221
285
 
222
286
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -9,12 +9,7 @@ require 'mcli/null_command'
9
9
  module MCLI
10
10
  class << self
11
11
  def run
12
- command = ARGV.shift
13
-
14
- CommandGroup.commands.fetch(command.to_s.to_sym) do |command|
15
- ARGV.unshift(command)
16
- CommandGroup.root_command || NullCommand
17
- end.call
12
+ CommandGroup.call(ARGV.dup)
18
13
  end
19
14
  end
20
15
  end
@@ -16,7 +16,7 @@ class MCLI::Command
16
16
  end
17
17
  end
18
18
 
19
- parser.parse!
19
+ parser.parse!(@args)
20
20
 
21
21
  self.class.options
22
22
  .select { |option| option.required == true }
@@ -37,6 +37,8 @@ class MCLI::Command
37
37
 
38
38
  def create_parser
39
39
  OptionParser.new.tap do |parser|
40
+ parser.program_name = self.class.command_name
41
+
40
42
  parser.on("-h", "--help", "Help") do
41
43
  raise MCLI::HelpError.new
42
44
  end
@@ -44,6 +46,11 @@ class MCLI::Command
44
46
  end
45
47
 
46
48
  class << self
49
+ def description(desc=nil)
50
+ @description = desc unless desc.nil?
51
+ @description ||= ''
52
+ end
53
+
47
54
  def option(option_name, opts={})
48
55
  options << Option.new(option_name, opts)
49
56
  end
@@ -52,12 +59,13 @@ class MCLI::Command
52
59
  @options ||= []
53
60
  end
54
61
 
55
- def register_as(command_name)
56
- MCLI::CommandGroup.register(command_name, self)
62
+ def register_as(command_name, to: default_command_group)
63
+ @command_name = command_name
64
+ to.register(command_name, self)
57
65
  end
58
66
 
59
- def register_as_root
60
- MCLI::CommandGroup.register_root(self)
67
+ def register_as_root(to: default_command_group)
68
+ to.register_root(self)
61
69
  end
62
70
 
63
71
  def capture_all!
@@ -68,8 +76,16 @@ class MCLI::Command
68
76
  @capture_all
69
77
  end
70
78
 
71
- def call
72
- new(ARGV).tap do |command|
79
+ def command_name
80
+ @command_name
81
+ end
82
+
83
+ def default_command_group
84
+ MCLI::CommandGroup
85
+ end
86
+
87
+ def call(args)
88
+ new(args).tap do |command|
73
89
  begin
74
90
  command.parse unless capture_all?
75
91
  command.run
@@ -17,7 +17,7 @@ class MCLI::Command::Option
17
17
  [].tap do |args|
18
18
  args << "-#{@alias}" if @alias
19
19
  args << "--#{@name} #{@name.upcase}" if @required && !@boolean
20
- args << "--#{@name} [#{@name.upcase}]" if !@required && !@boolean
20
+ args << "--#{@name} [#{@default || @name.upcase}]" if !@required && !@boolean
21
21
  args << "--[no-]#{@name}" if @boolean
22
22
  end
23
23
  end
@@ -19,6 +19,15 @@ class MCLI::CommandGroup
19
19
  def root_command
20
20
  @root_command
21
21
  end
22
+
23
+ def call(args)
24
+ command = args.shift
25
+
26
+ commands.fetch(command.to_s.to_sym) do |command|
27
+ args.unshift(command)
28
+ root_command || MCLI::NullCommand
29
+ end.call(args)
30
+ end
22
31
  end
23
32
  end
24
33
 
@@ -1,3 +1,3 @@
1
1
  module MCLI
2
- VERSION = "0.4.0"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^bin/mcli}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.14"
24
- spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
26
  end
metadata CHANGED
@@ -1,58 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Thomas
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-05 00:00:00.000000000 Z
11
+ date: 2021-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.14'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.14'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
55
- description:
54
+ version: '0'
55
+ description:
56
56
  email:
57
57
  - thomas07@vt.edu
58
58
  executables:
@@ -82,7 +82,7 @@ homepage: https://github.com/thomas07vt/mcli
82
82
  licenses:
83
83
  - MIT
84
84
  metadata: {}
85
- post_install_message:
85
+ post_install_message:
86
86
  rdoc_options: []
87
87
  require_paths:
88
88
  - lib
@@ -97,8 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  requirements: []
100
- rubygems_version: 3.0.6
101
- signing_key:
100
+ rubygems_version: 3.0.8
101
+ signing_key:
102
102
  specification_version: 4
103
103
  summary: Create CLI tools using ruby objects with a nice API
104
104
  test_files: []