mcli 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ddfcf04bb4cb996fc21aeea2dca7ef2792d568d565beb63fba843e0aeffa29f
4
- data.tar.gz: f98c3c1b08f48e47178315b9fc7ae5bac4ab31ddf5bf667889a7d0db38cf0e4f
3
+ metadata.gz: 0011cb49282f8d026747d15ceaff822cedafb0e56920a0dcc952d35bc3d44c5e
4
+ data.tar.gz: 744d35d6c593b620a5fd8aa996abb789af17411116560b136a3c824aba37cd84
5
5
  SHA512:
6
- metadata.gz: d8901533c3bc6ec5db845560e1730815908b56ffc1b6d8bc63b00db7dd672b6a55e5b9f2ad9a1dcaea037acc42f3890a52b7326b38f8d089e0e5c7a252b505e3
7
- data.tar.gz: cf8d25519029d0d1c8c5d2b133664839f8996a2e7b5985e60db4488f16071d0354493c4755be8cbc6f16f0e89cdc936a83ca0024622746d8eaf5daae4af5c361
6
+ metadata.gz: 1acacb7282ef455e98d85caaa33e1396c79fb388962fc247df65d371b5efcf3ac7e74d061e146ae021ae9243970b54acb19053f4f600ffd0078df474b73e2446
7
+ data.tar.gz: 3c74baee24bcf1d03761877cd1b3b2e2d88357220f273cfc682631f8a2a6e326975ed5df5f194bc303481cbba94c29a8f8294287eee04d326997b5086c4da4d4
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 }
@@ -59,9 +59,9 @@ class MCLI::Command
59
59
  @options ||= []
60
60
  end
61
61
 
62
- def register_as(command_name)
62
+ def register_as(command_name, to: default_command_group)
63
63
  @command_name = command_name
64
- MCLI::CommandGroup.register(command_name, self)
64
+ to.register(command_name, self)
65
65
  end
66
66
 
67
67
  def register_as_root
@@ -80,8 +80,12 @@ class MCLI::Command
80
80
  @command_name
81
81
  end
82
82
 
83
- def call
84
- new(ARGV).tap do |command|
83
+ def default_command_group
84
+ MCLI::CommandGroup
85
+ end
86
+
87
+ def call(args)
88
+ new(args).tap do |command|
85
89
  begin
86
90
  command.parse unless capture_all?
87
91
  command.run
@@ -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.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mcli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Thomas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-16 00:00:00.000000000 Z
11
+ date: 2021-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler