choosy 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +8 -0
- data/LICENSE +2 -0
- data/README.markdown +102 -54
- data/Rakefile +27 -5
- data/TODO.md +5 -0
- data/examples/bar.rb +9 -7
- data/examples/foo.rb +9 -7
- data/examples/superfoo.rb +48 -36
- data/lib/VERSION.yml +6 -0
- data/lib/choosy/argument.rb +5 -0
- data/lib/choosy/base_command.rb +11 -10
- data/lib/choosy/command.rb +17 -1
- data/lib/choosy/converter.rb +5 -1
- data/lib/choosy/dsl/argument_builder.rb +33 -21
- data/lib/choosy/dsl/base_builder.rb +26 -0
- data/lib/choosy/dsl/base_command_builder.rb +23 -30
- data/lib/choosy/dsl/command_builder.rb +8 -12
- data/lib/choosy/dsl/option_builder.rb +14 -45
- data/lib/choosy/dsl/super_command_builder.rb +17 -20
- data/lib/choosy/errors.rb +1 -0
- data/lib/choosy/option.rb +19 -0
- data/lib/choosy/printing/base_printer.rb +231 -0
- data/lib/choosy/printing/color.rb +8 -5
- data/lib/choosy/printing/erb_printer.rb +1 -1
- data/lib/choosy/printing/help_printer.rb +49 -163
- data/lib/choosy/printing/manpage.rb +235 -0
- data/lib/choosy/printing/manpage_printer.rb +95 -0
- data/lib/choosy/printing/terminal.rb +39 -8
- data/lib/choosy/printing.rb +1 -0
- data/lib/choosy/super_command.rb +13 -4
- data/lib/choosy/super_parser.rb +5 -1
- data/lib/choosy/verifier.rb +8 -0
- data/lib/choosy/version.rb +64 -5
- data/spec/choosy/argument_spec.rb +28 -0
- data/spec/choosy/base_command_spec.rb +7 -3
- data/spec/choosy/command_spec.rb +2 -2
- data/spec/choosy/converter_spec.rb +3 -2
- data/spec/choosy/dsl/argument_builder_spec.rb +19 -8
- data/spec/choosy/dsl/base_builder_spec.rb +43 -0
- data/spec/choosy/dsl/base_command_builder_spec.rb +7 -9
- data/spec/choosy/dsl/commmand_builder_spec.rb +9 -1
- data/spec/choosy/dsl/option_builder_spec.rb +1 -65
- data/spec/choosy/dsl/super_command_builder_spec.rb +19 -8
- data/spec/choosy/option_spec.rb +68 -0
- data/spec/choosy/printing/base_printer_spec.rb +155 -0
- data/spec/choosy/printing/color_spec.rb +4 -0
- data/spec/choosy/printing/help_printer_spec.rb +15 -109
- data/spec/choosy/printing/manpage_printer_spec.rb +95 -0
- data/spec/choosy/printing/manpage_spec.rb +206 -0
- data/spec/choosy/super_command_spec.rb +7 -0
- data/spec/choosy/super_parser_spec.rb +9 -0
- data/spec/choosy/verifier_spec.rb +31 -1
- data/spec/choosy/version.yml +5 -0
- data/spec/choosy/version_spec.rb +87 -0
- data/spec/integration/command-C_spec.rb +23 -0
- data/spec/integration/supercommand-C_spec.rb +45 -0
- metadata +81 -78
- data/lib/VERSION +0 -1
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
## 0.3.0 (March 24, 2011)
|
2
|
+
|
3
|
+
Features:
|
4
|
+
|
5
|
+
- Added the basic manpage printing.
|
6
|
+
- Added a <code>default</code> directive to the <code>SuperCommand</code> to allow for default commands.
|
7
|
+
- Added <code>:option_styles</code> directive to the manpage and standard printers.
|
8
|
+
|
data/LICENSE
CHANGED
@@ -21,3 +21,5 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
21
21
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
22
22
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
23
23
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
24
|
+
|
25
|
+
The Software shall be used for Good, not Evil.
|
data/README.markdown
CHANGED
@@ -9,7 +9,6 @@ This library should:
|
|
9
9
|
- Allow you to add validation logic for your arguments within the parsing phase.
|
10
10
|
- Allowing for dependencies between options, so that you can more easily validate related options (i.e. if the<code>--bold</code> flag requires the <code>--font Arial</code> flag, then you should be able to ask for the <code>--font</code> option to be validated first, and then the <code>--bold</code> option.
|
11
11
|
- Allow you to customize its output using your own formatting system.
|
12
|
-
- Allow you to customize the output to your specifications.
|
13
12
|
|
14
13
|
This library should never:
|
15
14
|
|
@@ -19,10 +18,8 @@ This library should never:
|
|
19
18
|
|
20
19
|
# Examples
|
21
20
|
|
22
|
-
#!/usr/bin/env ruby
|
21
|
+
#!/usr/bin/env ruby
|
23
22
|
# foo.rb
|
24
|
-
|
25
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
26
23
|
require 'choosy'
|
27
24
|
|
28
25
|
FOO_VERSION = '1.0.1'
|
@@ -37,7 +34,7 @@ This library should never:
|
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
40
|
-
foo_cmd = Choosy::Command.new :foo do |foo|
|
37
|
+
$foo_cmd = Choosy::Command.new :foo do |foo|
|
41
38
|
# Add a class to do the execution when you call foo_cmd.execute!
|
42
39
|
# You can also use a proc that takes the options and the args, like:
|
43
40
|
# executor { |args, options| puts 'Hi!' }
|
@@ -147,7 +144,7 @@ This library should never:
|
|
147
144
|
negate
|
148
145
|
default true
|
149
146
|
validate do
|
150
|
-
foo.
|
147
|
+
foo.entity.alter do
|
151
148
|
printer :standard, :colored => false
|
152
149
|
end
|
153
150
|
end
|
@@ -188,7 +185,7 @@ This library should never:
|
|
188
185
|
'--', # Stops parsing all arguments
|
189
186
|
'-h', '--help', '-v', '--version' # Ignored
|
190
187
|
]
|
191
|
-
result = foo_cmd.parse!(args)
|
188
|
+
result = $foo_cmd.parse!(args)
|
192
189
|
|
193
190
|
require 'pp'
|
194
191
|
pp result[:prefix] # => '{'
|
@@ -211,7 +208,7 @@ This library should never:
|
|
211
208
|
# This allows you to easily associate command classes with
|
212
209
|
# commands, without resorting to a hash or combining
|
213
210
|
# execution logic with command parsing logic.
|
214
|
-
foo_cmd.execute!(args) # {high,there,you,foo}
|
211
|
+
$foo_cmd.execute!(args) # {high,there,you,foo}
|
215
212
|
# {high,there,you,foo}
|
216
213
|
# {high,there,you,foo}
|
217
214
|
# and handsom devil http://not.posting.here -h --help -v --verbose
|
@@ -220,19 +217,16 @@ This library should never:
|
|
220
217
|
|
221
218
|
### Super Commands
|
222
219
|
|
223
|
-
You can also combine multiple choices into an uber-choice, creating
|
224
|
-
commands that look a lot like git or subversion.
|
220
|
+
You can also combine multiple choices into an uber-choice, creating commands that look a lot like git or subversion.
|
225
221
|
|
226
222
|
First, we create another command.
|
227
223
|
|
228
|
-
#!/usr/bin/env ruby
|
224
|
+
#!/usr/bin/env ruby
|
229
225
|
# bar.rb
|
230
|
-
|
231
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
232
226
|
require 'choosy'
|
233
227
|
|
234
228
|
# Create a new command
|
235
|
-
bar_cmd = Choosy::Command.new :bar do
|
229
|
+
$bar_cmd = Choosy::Command.new :bar do
|
236
230
|
executor do |args, options|
|
237
231
|
if options[:bold]
|
238
232
|
puts "BOLD!!!"
|
@@ -258,28 +252,26 @@ First, we create another command.
|
|
258
252
|
if __FILE__ == $0
|
259
253
|
args = ['--un-bold']
|
260
254
|
|
261
|
-
result = bar_cmd.parse!(args)
|
255
|
+
result = $bar_cmd.parse!(args)
|
262
256
|
|
263
257
|
require 'pp'
|
264
258
|
pp result.options[:bold] # => false
|
265
259
|
pp result.args # => []
|
266
260
|
|
267
|
-
bar_cmd.execute!(args) # => 'plain'
|
261
|
+
$bar_cmd.execute!(args) # => 'plain'
|
268
262
|
|
269
263
|
args << 'should-throw-error'
|
270
|
-
bar_cmd.execute!(args)
|
264
|
+
$bar_cmd.execute!(args)
|
271
265
|
end
|
272
266
|
|
273
267
|
We can now create our super command.
|
274
268
|
|
275
|
-
#!/usr/bin/env ruby
|
276
|
-
# superfoo.rb
|
269
|
+
#!/usr/bin/env ruby
|
277
270
|
|
278
|
-
|
279
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'examples')
|
271
|
+
# superfoo.rb
|
280
272
|
require 'choosy'
|
281
|
-
require
|
282
|
-
require
|
273
|
+
require 'foo'
|
274
|
+
require 'bar'
|
283
275
|
|
284
276
|
SUPERFOO_VERSION = "1.0.1"
|
285
277
|
|
@@ -291,8 +283,8 @@ We can now create our super command.
|
|
291
283
|
# Note that, when added, these commands have their
|
292
284
|
# -h/--help/--version flags suppressed, so you'll
|
293
285
|
# need to add those flags here.
|
294
|
-
command bar_cmd
|
295
|
-
command foo_cmd
|
286
|
+
command $bar_cmd
|
287
|
+
command $foo_cmd
|
296
288
|
|
297
289
|
# Creates a 'help' command, message optional
|
298
290
|
help "Prints this help message"
|
@@ -317,7 +309,7 @@ We can now create our super command.
|
|
317
309
|
if __FILE__ == $0
|
318
310
|
args = ['foo',
|
319
311
|
'-c', '5',
|
320
|
-
'--config', '
|
312
|
+
'--config', 'superfoo.yaml',
|
321
313
|
'--prefix', '{',
|
322
314
|
'--suffix', '}',
|
323
315
|
'cruft',
|
@@ -327,25 +319,31 @@ We can now create our super command.
|
|
327
319
|
result = superfoo.parse!(args)
|
328
320
|
|
329
321
|
require 'pp'
|
330
|
-
pp result[:
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
pp
|
335
|
-
pp
|
336
|
-
pp
|
337
|
-
|
322
|
+
pp result[:Config] # => {:here => 'text'} # Pulled the config!
|
323
|
+
|
324
|
+
foores = result.subresults[0]
|
325
|
+
|
326
|
+
pp foores[:Config] # => {:here => 'text'} # Passed along!
|
327
|
+
pp foores[:prefix] # => '{'
|
328
|
+
pp foores[:suffix] # => '}'
|
329
|
+
pp foores[:count] # => 5
|
330
|
+
pp foores[:bold] # => true
|
331
|
+
pp foores.options # => {:prefix => '{', :suffix => '}'
|
332
|
+
# :count => 5,
|
338
333
|
# :bold => true,
|
339
334
|
# :words => [],
|
340
335
|
# :config => '~/.superfoo' }
|
341
|
-
pp
|
336
|
+
pp foores.args # => ['cruft', 'bar']
|
342
337
|
|
343
338
|
# Now, we can call the result
|
344
339
|
superfoo.execute!(args) ## Calls superfoo.result.execute!
|
345
340
|
## Prints:
|
346
341
|
# BOLDED!!
|
347
342
|
# {foo}
|
348
|
-
# {foo}
|
343
|
+
# {foo,foo}
|
344
|
+
# {foo,foo,foo}
|
345
|
+
# {foo,foo,foo,foo}
|
346
|
+
# {foo,foo,foo,foo,foo}
|
349
347
|
# and cruft bar
|
350
348
|
|
351
349
|
# Instead of parsing the 'bar' parameter as an argument to
|
@@ -364,26 +362,29 @@ We can now create our super command.
|
|
364
362
|
end
|
365
363
|
|
366
364
|
result = superfoo.parse!(args)
|
367
|
-
|
368
|
-
|
369
|
-
pp
|
370
|
-
pp
|
371
|
-
pp
|
372
|
-
pp
|
373
|
-
pp
|
374
|
-
pp
|
375
|
-
pp
|
376
|
-
# :count =>
|
365
|
+
|
366
|
+
foores = result.subresults[0]
|
367
|
+
pp foores[:Config] # => {:here => 'text'} # Passed along!
|
368
|
+
pp foores.command.name # => :foo
|
369
|
+
pp foores[:prefix] # => '{'
|
370
|
+
pp foores[:suffix] # => '}'
|
371
|
+
pp foores[:count] # => 5
|
372
|
+
pp foores[:bold] # => true
|
373
|
+
pp foores.options # => {:prefix => '{', :suffix => '}'
|
374
|
+
# :count => 5,
|
377
375
|
# :bold => false,
|
378
376
|
# :words => [],
|
379
|
-
# :config => '
|
380
|
-
|
377
|
+
# :config => {:here => 'text'}
|
378
|
+
# }
|
379
|
+
pp foores.args # => ['cruft']
|
381
380
|
|
382
|
-
|
383
|
-
pp
|
384
|
-
pp
|
385
|
-
|
386
|
-
|
381
|
+
barres = result.subresults[1]
|
382
|
+
pp barres.command.name # => :bar
|
383
|
+
pp barres[:bold] # => true
|
384
|
+
pp barres.options # => {:bold => true,
|
385
|
+
# :config => {:here => 'text'}
|
386
|
+
# }
|
387
|
+
pp barres.args # => []
|
387
388
|
|
388
389
|
# Now, execute the results in order
|
389
390
|
superfoo.execute!(args) ## Same as:
|
@@ -398,4 +399,51 @@ We can now create our super command.
|
|
398
399
|
end
|
399
400
|
|
400
401
|
|
401
|
-
|
402
|
+
# Output Printing
|
403
|
+
|
404
|
+
Choosy allows you to customize the output printing of your documentation. It exposes the internal object model to any class that implements a <code>print!(command)</code> method.
|
405
|
+
|
406
|
+
The <code>:standard</code> printer that is the default for any command can also be customized to meet some of your needs:
|
407
|
+
|
408
|
+
Choosy::Command.new :foo do
|
409
|
+
printer :standard,
|
410
|
+
:max_width => 80,
|
411
|
+
:color => true,
|
412
|
+
:header_styles => [:bold, :green],
|
413
|
+
:indent => ' ',
|
414
|
+
:offset => ' '
|
415
|
+
|
416
|
+
help "Show this help command."
|
417
|
+
end
|
418
|
+
|
419
|
+
This above example sets some useful properties for the printer. First, the <code>:max_width</code> limits the wrapping size on the console. By default, choosy tries to be smart and wrap to the currend column width, but you can introduce this hash parameter as a default max. Next, you can turn off and on color printing by setting <code>:color</code>. Color is on by default, so it's actually superfluous in this example -- just a demonstration of the syntax. The <code>:header_styles</code> is an array of styles that will be applied to the headers for this document. By default, headers are <code>[:bold, :blue]</code>. Most of the ANSI colors and codes are supported, but check <code>lib/choosy/printing/color.rb</code> for additional options. The last two options given are actually formatting spacers in the output that you can customize: <code>:indent</code> is the default indent for commands and options; <code>:offset</code> is the distance between the options and commands to their associated descriptive text.
|
420
|
+
|
421
|
+
For those who want the nice, manpage experience, there's also the <code>:manpage</code> printer:
|
422
|
+
|
423
|
+
Choosy::Command.new :foo do
|
424
|
+
printer :manpage,
|
425
|
+
:max_width => 80, # Same as :standard
|
426
|
+
:color => true, # Same as :standard
|
427
|
+
:header_styles => [:bold, :green], # Same as :standard
|
428
|
+
:option_sytles => [:bold], # Same as :standard
|
429
|
+
:indent => ' ', # Same as :standard
|
430
|
+
:offset => ' ', # Same as :standard
|
431
|
+
:version => FOO_VERSION, # Will use the version name you specify
|
432
|
+
:section => 1, # Default is always '1'
|
433
|
+
:date => '03/24/2011', # Date you want displayed
|
434
|
+
:manual => 'Foo Co.' # The manual page group
|
435
|
+
|
436
|
+
version FOO_VERSION # If you don't supply a version above, this will be used
|
437
|
+
end
|
438
|
+
|
439
|
+
Because the library is super-awesome, the manpage will even be in color when piped to less (!). If you don't like the format of my manpage, feel free to implement your own using the <code>choosy/printing/manpage</code> class, a useful utility class for formatting manpage output correctly.
|
440
|
+
|
441
|
+
There is also the <code>:erb</code> template that can be customized by writing a template of your choice:
|
442
|
+
|
443
|
+
Choosy::Command.new :foo od
|
444
|
+
printer :erb, :color => true, :template => 'path/to/file.erb'
|
445
|
+
end
|
446
|
+
|
447
|
+
The ERB printer also accepts the <code>:color</code> option. The color is exposed via a <code>color</code> property in the template; the command is exposed by the <code>command</code> property.
|
448
|
+
|
449
|
+
By the way, if you implement a custom printer, you can also include the <code>choosy/printing/terminal</code> module to get access to the line and column information of the console, if available.
|
data/Rakefile
CHANGED
@@ -8,11 +8,26 @@ require 'rspec/core/rake_task'
|
|
8
8
|
require './lib/choosy/version'
|
9
9
|
|
10
10
|
PACKAGE_NAME = "choosy"
|
11
|
-
PACKAGE_VERSION = Choosy::Version
|
11
|
+
PACKAGE_VERSION = Choosy::Version.new(File.join(File.dirname(__FILE__), 'lib', 'VERSION.yml'))
|
12
12
|
|
13
13
|
desc "Default task"
|
14
14
|
task :default => [ :spec ]
|
15
15
|
|
16
|
+
namespace :version do
|
17
|
+
desc "Tiny bump"
|
18
|
+
task :tiny do
|
19
|
+
PACKAGE_VERSION.version!(:tiny)
|
20
|
+
end
|
21
|
+
desc "Minor bump"
|
22
|
+
task :minor do
|
23
|
+
PACKAGE_VERSION.version!(:minor)
|
24
|
+
end
|
25
|
+
desc "Major bump"
|
26
|
+
task :major do
|
27
|
+
PACKAGE_VERSION.version!(:major)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
16
31
|
desc "Build documentation"
|
17
32
|
task :doc => [ :rdoc ] do
|
18
33
|
File.open 'docs/README.markdown', 'r' do |template|
|
@@ -20,9 +35,16 @@ task :doc => [ :rdoc ] do
|
|
20
35
|
template.each do |line|
|
21
36
|
if line =~ /^>>> (.*)/
|
22
37
|
puts $1
|
23
|
-
File.open $1, 'r' do |
|
24
|
-
|
25
|
-
|
38
|
+
File.open $1, 'r' do |toinsert|
|
39
|
+
exclude = false
|
40
|
+
toinsert.each_line do |ins|
|
41
|
+
if ins =~ /^##-/
|
42
|
+
exclude = true
|
43
|
+
end
|
44
|
+
output.puts " #{ins}" unless exclude
|
45
|
+
if ins =~ /^##\+/
|
46
|
+
exclude = false
|
47
|
+
end
|
26
48
|
end
|
27
49
|
end
|
28
50
|
else
|
@@ -43,7 +65,7 @@ begin
|
|
43
65
|
require 'jeweler'
|
44
66
|
Jeweler::Tasks.new do |gem|
|
45
67
|
gem.name = PACKAGE_NAME
|
46
|
-
gem.version = PACKAGE_VERSION
|
68
|
+
gem.version = PACKAGE_VERSION.to_s
|
47
69
|
gem.summary = 'Yet another option parsing library.'
|
48
70
|
gem.description = 'This is a DSL for creating more complicated command line tools.'
|
49
71
|
gem.email = ['madeonamac@gmail.com']
|
data/TODO.md
ADDED
data/examples/bar.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##-
|
3
|
+
BEGIN {$VERBOSE = true}
|
4
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
|
+
##+
|
2
6
|
# bar.rb
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
5
7
|
require 'choosy'
|
6
8
|
|
7
9
|
# Create a new command
|
8
|
-
bar_cmd = Choosy::Command.new :bar do
|
10
|
+
$bar_cmd = Choosy::Command.new :bar do
|
9
11
|
executor do |args, options|
|
10
12
|
if options[:bold]
|
11
13
|
puts "BOLD!!!"
|
@@ -31,14 +33,14 @@ end
|
|
31
33
|
if __FILE__ == $0
|
32
34
|
args = ['--un-bold']
|
33
35
|
|
34
|
-
result = bar_cmd.parse!(args)
|
36
|
+
result = $bar_cmd.parse!(args)
|
35
37
|
|
36
38
|
require 'pp'
|
37
39
|
pp result.options[:bold] # => false
|
38
40
|
pp result.args # => []
|
39
41
|
|
40
|
-
bar_cmd.execute!(args) # => 'plain'
|
42
|
+
$bar_cmd.execute!(args) # => 'plain'
|
41
43
|
|
42
44
|
args << 'should-throw-error'
|
43
|
-
bar_cmd.execute!(args)
|
45
|
+
$bar_cmd.execute!(args)
|
44
46
|
end
|
data/examples/foo.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##-
|
3
|
+
BEGIN {$VERBOSE = true}
|
4
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
|
+
##+
|
2
6
|
# foo.rb
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'lib')
|
5
7
|
require 'choosy'
|
6
8
|
|
7
9
|
FOO_VERSION = '1.0.1'
|
@@ -16,7 +18,7 @@ class FooExecutor
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
foo_cmd = Choosy::Command.new :foo do |foo|
|
21
|
+
$foo_cmd = Choosy::Command.new :foo do |foo|
|
20
22
|
# Add a class to do the execution when you call foo_cmd.execute!
|
21
23
|
# You can also use a proc that takes the options and the args, like:
|
22
24
|
# executor { |args, options| puts 'Hi!' }
|
@@ -126,7 +128,7 @@ foo_cmd = Choosy::Command.new :foo do |foo|
|
|
126
128
|
negate
|
127
129
|
default true
|
128
130
|
validate do
|
129
|
-
foo.
|
131
|
+
foo.entity.alter do
|
130
132
|
printer :standard, :colored => false
|
131
133
|
end
|
132
134
|
end
|
@@ -167,7 +169,7 @@ if __FILE__ == $0
|
|
167
169
|
'--', # Stops parsing all arguments
|
168
170
|
'-h', '--help', '-v', '--version' # Ignored
|
169
171
|
]
|
170
|
-
result = foo_cmd.parse!(args)
|
172
|
+
result = $foo_cmd.parse!(args)
|
171
173
|
|
172
174
|
require 'pp'
|
173
175
|
pp result[:prefix] # => '{'
|
@@ -190,7 +192,7 @@ if __FILE__ == $0
|
|
190
192
|
# This allows you to easily associate command classes with
|
191
193
|
# commands, without resorting to a hash or combining
|
192
194
|
# execution logic with command parsing logic.
|
193
|
-
foo_cmd.execute!(args) # {high,there,you,foo}
|
195
|
+
$foo_cmd.execute!(args) # {high,there,you,foo}
|
194
196
|
# {high,there,you,foo}
|
195
197
|
# {high,there,you,foo}
|
196
198
|
# and handsom devil http://not.posting.here -h --help -v --verbose
|
data/examples/superfoo.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
##-
|
3
|
+
BEGIN {$VERBOSE = true}
|
4
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
5
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
6
|
+
##+
|
3
7
|
|
4
|
-
|
5
|
-
$LOAD_PATH.unshift File.join(File.dirname(File.dirname(__FILE__)), 'examples')
|
8
|
+
# superfoo.rb
|
6
9
|
require 'choosy'
|
7
|
-
require
|
8
|
-
require
|
10
|
+
require 'foo'
|
11
|
+
require 'bar'
|
9
12
|
|
10
13
|
SUPERFOO_VERSION = "1.0.1"
|
11
14
|
|
@@ -17,8 +20,8 @@ superfoo = Choosy::SuperCommand.new :superfoo do
|
|
17
20
|
# Note that, when added, these commands have their
|
18
21
|
# -h/--help/--version flags suppressed, so you'll
|
19
22
|
# need to add those flags here.
|
20
|
-
command bar_cmd
|
21
|
-
command foo_cmd
|
23
|
+
command $bar_cmd
|
24
|
+
command $foo_cmd
|
22
25
|
|
23
26
|
# Creates a 'help' command, message optional
|
24
27
|
help "Prints this help message"
|
@@ -43,7 +46,7 @@ end
|
|
43
46
|
if __FILE__ == $0
|
44
47
|
args = ['foo',
|
45
48
|
'-c', '5',
|
46
|
-
'--config', '
|
49
|
+
'--config', 'superfoo.yaml',
|
47
50
|
'--prefix', '{',
|
48
51
|
'--suffix', '}',
|
49
52
|
'cruft',
|
@@ -53,25 +56,31 @@ if __FILE__ == $0
|
|
53
56
|
result = superfoo.parse!(args)
|
54
57
|
|
55
58
|
require 'pp'
|
56
|
-
pp result[:
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
pp
|
61
|
-
pp
|
62
|
-
pp
|
63
|
-
|
59
|
+
pp result[:Config] # => {:here => 'text'} # Pulled the config!
|
60
|
+
|
61
|
+
foores = result.subresults[0]
|
62
|
+
|
63
|
+
pp foores[:Config] # => {:here => 'text'} # Passed along!
|
64
|
+
pp foores[:prefix] # => '{'
|
65
|
+
pp foores[:suffix] # => '}'
|
66
|
+
pp foores[:count] # => 5
|
67
|
+
pp foores[:bold] # => true
|
68
|
+
pp foores.options # => {:prefix => '{', :suffix => '}'
|
69
|
+
# :count => 5,
|
64
70
|
# :bold => true,
|
65
71
|
# :words => [],
|
66
72
|
# :config => '~/.superfoo' }
|
67
|
-
pp
|
73
|
+
pp foores.args # => ['cruft', 'bar']
|
68
74
|
|
69
75
|
# Now, we can call the result
|
70
76
|
superfoo.execute!(args) ## Calls superfoo.result.execute!
|
71
77
|
## Prints:
|
72
78
|
# BOLDED!!
|
73
79
|
# {foo}
|
74
|
-
# {foo}
|
80
|
+
# {foo,foo}
|
81
|
+
# {foo,foo,foo}
|
82
|
+
# {foo,foo,foo,foo}
|
83
|
+
# {foo,foo,foo,foo,foo}
|
75
84
|
# and cruft bar
|
76
85
|
|
77
86
|
# Instead of parsing the 'bar' parameter as an argument to
|
@@ -90,26 +99,29 @@ if __FILE__ == $0
|
|
90
99
|
end
|
91
100
|
|
92
101
|
result = superfoo.parse!(args)
|
93
|
-
|
94
|
-
|
95
|
-
pp
|
96
|
-
pp
|
97
|
-
pp
|
98
|
-
pp
|
99
|
-
pp
|
100
|
-
pp
|
101
|
-
pp
|
102
|
-
# :count =>
|
102
|
+
|
103
|
+
foores = result.subresults[0]
|
104
|
+
pp foores[:Config] # => {:here => 'text'} # Passed along!
|
105
|
+
pp foores.command.name # => :foo
|
106
|
+
pp foores[:prefix] # => '{'
|
107
|
+
pp foores[:suffix] # => '}'
|
108
|
+
pp foores[:count] # => 5
|
109
|
+
pp foores[:bold] # => true
|
110
|
+
pp foores.options # => {:prefix => '{', :suffix => '}'
|
111
|
+
# :count => 5,
|
103
112
|
# :bold => false,
|
104
113
|
# :words => [],
|
105
|
-
# :config => '
|
106
|
-
|
114
|
+
# :config => {:here => 'text'}
|
115
|
+
# }
|
116
|
+
pp foores.args # => ['cruft']
|
107
117
|
|
108
|
-
|
109
|
-
pp
|
110
|
-
pp
|
111
|
-
|
112
|
-
|
118
|
+
barres = result.subresults[1]
|
119
|
+
pp barres.command.name # => :bar
|
120
|
+
pp barres[:bold] # => true
|
121
|
+
pp barres.options # => {:bold => true,
|
122
|
+
# :config => {:here => 'text'}
|
123
|
+
# }
|
124
|
+
pp barres.args # => []
|
113
125
|
|
114
126
|
# Now, execute the results in order
|
115
127
|
superfoo.execute!(args) ## Same as:
|
data/lib/VERSION.yml
ADDED
data/lib/choosy/argument.rb
CHANGED
data/lib/choosy/base_command.rb
CHANGED
@@ -7,7 +7,7 @@ module Choosy
|
|
7
7
|
alias tsort_each_node each_key
|
8
8
|
|
9
9
|
def tsort_each_child(node, &block)
|
10
|
-
deps = fetch(node).
|
10
|
+
deps = fetch(node).entity.dependent_options
|
11
11
|
deps.each(&block) unless deps.nil?
|
12
12
|
end
|
13
13
|
end
|
@@ -20,23 +20,18 @@ module Choosy
|
|
20
20
|
@name = name
|
21
21
|
@listing = []
|
22
22
|
@option_builders = OptionBuilderHash.new
|
23
|
+
@printer = nil
|
23
24
|
|
24
25
|
@builder = create_builder
|
25
|
-
|
26
|
-
@builder.instance_eval(&block)
|
27
|
-
end
|
28
|
-
@builder.finalize!
|
26
|
+
@builder.evaluate!(&block)
|
29
27
|
end
|
30
28
|
|
31
29
|
def alter(&block)
|
32
|
-
|
33
|
-
@builder.instance_eval(&block)
|
34
|
-
end
|
35
|
-
@builder.finalize!
|
30
|
+
@builder.evaluate!(&block)
|
36
31
|
end
|
37
32
|
|
38
33
|
def options
|
39
|
-
@option_builders.tsort.map {|key| @option_builders[key].
|
34
|
+
@option_builders.tsort.map {|key| @option_builders[key].entity }
|
40
35
|
end
|
41
36
|
|
42
37
|
def parse!(args, propagate=false)
|
@@ -58,6 +53,12 @@ module Choosy
|
|
58
53
|
end
|
59
54
|
end
|
60
55
|
|
56
|
+
def finalize!
|
57
|
+
if @printer.nil?
|
58
|
+
builder.printer :standard
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
61
62
|
protected
|
62
63
|
def create_builder
|
63
64
|
# Override in subclasses
|