mcli 0.3.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: 58ba7853c0e14c2f1998e4d1dcce81cf5c99eb5d414ec720db0d4612a3b397d1
4
- data.tar.gz: b0a1caa1a951a1bec4468e384a494fffc6c8e83f6ec15e60ff93354e9cc86d7e
3
+ metadata.gz: 0011cb49282f8d026747d15ceaff822cedafb0e56920a0dcc952d35bc3d44c5e
4
+ data.tar.gz: 744d35d6c593b620a5fd8aa996abb789af17411116560b136a3c824aba37cd84
5
5
  SHA512:
6
- metadata.gz: 844fb154e3d2cca1395b478b9d306866f2491bf69ff478cdb3dce12e8ade22f168ba9a6edbf2b8df72d9cab6a6f9072654819a0c4f1b14f6803904d2a9f9ec98
7
- data.tar.gz: 2a6fd377bab075c2b873e01f2839ea4329ae615493dac1386a530434e8bad5ed6d7b54eff0dffc79e2adb94a9645ffe372731c5b84bf1675b93b12ebf1a19338
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.commands[:_root] || 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,8 +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)
65
+ end
66
+
67
+ def register_as_root
68
+ MCLI::CommandGroup.register_root(self)
57
69
  end
58
70
 
59
71
  def capture_all!
@@ -64,8 +76,16 @@ class MCLI::Command
64
76
  @capture_all
65
77
  end
66
78
 
67
- def call
68
- 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|
69
89
  begin
70
90
  command.parse unless capture_all?
71
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
@@ -4,6 +4,10 @@ class MCLI::CommandGroup
4
4
  commands[command_name] = command_klass
5
5
  end
6
6
 
7
+ def register_root(command_klass)
8
+ @root_command = command_klass
9
+ end
10
+
7
11
  def commands
8
12
  @commands ||= {}
9
13
  end
@@ -11,6 +15,19 @@ class MCLI::CommandGroup
11
15
  def clear
12
16
  @commands = {}
13
17
  end
18
+
19
+ def root_command
20
+ @root_command
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
14
31
  end
15
32
  end
16
33
 
@@ -1,3 +1,3 @@
1
1
  module MCLI
2
- VERSION = "0.3.0"
2
+ VERSION = "0.8.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.3.0
4
+ version: 0.8.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-17 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: []