Pablo 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/pablo.rb +70 -7
  2. data/tests/help.rb +65 -1
  3. metadata +2 -2
data/lib/pablo.rb CHANGED
@@ -11,7 +11,10 @@
11
11
  # For further information regarding this license, you can go to
12
12
  # http://creativecommons.org/licenses/by/3.0/de/
13
13
  # Homepage:: http://pablo.rubyforge.org/
14
- # Git repo:: http://www.github.org/pablo-commandline-parser/
14
+ # Git repo:: http://rubyforge.org/scm/?group_id=7334
15
+ #
16
+ # More information can be found in the documentation of the parser (_Pablo::parse_) and
17
+ # the classes (_Pablo::Token_, _Pablo::Option_ and _Pablo::Command_).
15
18
  #
16
19
 
17
20
  module Pablo
@@ -25,8 +28,14 @@ module Pablo
25
28
  # Either of the two can be omitted.
26
29
  # If no arguments Array is found, +ARGV+ is parsed.
27
30
  # If no options Hash is found, the defaults are used.
31
+ #
32
+ # The options Hash can have the following arguments:
33
+ # - +:program+ - The name of the program
34
+ # - +:version+ - The program version
35
+ # - +:usage+ - How this program is to be used via the command-line, e.g.
36
+ # "[help] [--verbose [level]] delete [number]"
28
37
  #
29
- # See _Pablo::Parser#command_ and _Pablo::Parser#option_ for more details.
38
+ # See _Pablo::Token_,_Pablo::Command_ and _Pablo::Option_ for more details.
30
39
  #
31
40
  def self.parse *argsopts, &block
32
41
  # parse arguments and options apart
@@ -39,6 +48,8 @@ module Pablo
39
48
  args = ARGV if args.nil?
40
49
  # do under NO circumstances modify the real args
41
50
  args = args.dup
51
+
52
+ opts[:version] = opts[:version].join '.' if opts[:version].is_a? Array
42
53
 
43
54
  # create tokens
44
55
  p = Pablo::TopLevel.new opts
@@ -46,6 +57,8 @@ module Pablo
46
57
 
47
58
  # test on a present help command
48
59
  return if p.help? args
60
+ # test on a present version command
61
+ return if opts[:version] and p.version?(args)
49
62
 
50
63
  # prepare arguments for marking
51
64
  args.collect! { |a| [ [], a ] }
@@ -244,6 +257,8 @@ module Pablo
244
257
  ##
245
258
  # Keeps and parses an option.
246
259
  # Simple, easy, no children.
260
+ #
261
+ # See also: +Pablo::Token+.
247
262
  #
248
263
  class Option < Pablo::Token
249
264
  ##
@@ -280,6 +295,8 @@ module Pablo
280
295
  ##
281
296
  # Keeps and parses a command.
282
297
  # More sophisticated than an option, can have children.
298
+ #
299
+ # See also: +Pablo::Token+.
283
300
  #
284
301
  class Command < Pablo::Token
285
302
  ##
@@ -330,6 +347,7 @@ module Pablo
330
347
  @everyone = []
331
348
  @options = options
332
349
  @help = false
350
+ @version = false
333
351
  super self # set toplevel
334
352
  end
335
353
 
@@ -337,7 +355,7 @@ module Pablo
337
355
 
338
356
  ##
339
357
  # Will run the default token's run block, if there is one or will try to show
340
- # the help otherwise.
358
+ # the help or version otherwise.
341
359
  #
342
360
  def do_defaults
343
361
  done = false
@@ -348,9 +366,27 @@ module Pablo
348
366
  end
349
367
  }
350
368
 
351
- help? [] unless done
369
+ version? [] unless help? [] unless done
352
370
  end
353
371
 
372
+ ##
373
+ # Tests whether a version command is present in +args+ and +version!+ has been called
374
+ # and if so:
375
+ # - executes +Pablo::version+
376
+ # - and returns true
377
+ # otherwise only returns false.
378
+ #
379
+ def version? args
380
+ return false unless @version
381
+ found = args.index { |a|
382
+ @version_aliases.any? { |v| v == a }
383
+ }
384
+ found = 0 if args.empty?
385
+ return false unless found
386
+ Pablo::version @options
387
+ return true
388
+ end
389
+
354
390
  ##
355
391
  # Tests whether a help command is present in +args+ and +help!+ has been called
356
392
  # and if so:
@@ -365,7 +401,8 @@ module Pablo
365
401
  }
366
402
  found = 0 if args.empty?
367
403
  return false unless found
368
- Pablo::help args[found+1..-1], @everyone, @options
404
+ a = args[found+1..-1] || []
405
+ Pablo::help a, @everyone, @options
369
406
  return true
370
407
  end
371
408
 
@@ -380,9 +417,22 @@ module Pablo
380
417
  @help = true
381
418
  @help_aliases = alis
382
419
  end
420
+
421
+ ##TODO: usage!, each token gives it's usage -> [alias1|alias2 usage]
422
+
423
+ ##
424
+ # Instructs Pablo to display a version message generated from the +:version+ argument
425
+ # given to the +parse+ function.
426
+ #
427
+ # +alis+ are the aliases the version command should respond to.
428
+ # By default these are _version_, _--version_ and _-v_.
429
+ #
430
+ def version! alis = ['version', '--version', '-v']
431
+ @version = true
432
+ @version_aliases = alis
433
+ end
383
434
  end
384
435
 
385
- ##TODO: Version command and version in help command
386
436
  ##TODO: caseless aliases!
387
437
 
388
438
  ##
@@ -406,7 +456,7 @@ module Pablo
406
456
  found = found[1] if found
407
457
 
408
458
  # program name and version
409
- puts "#{options[:program]} v#{options[:version].join '.'}" if
459
+ puts "#{options[:program]} v#{options[:version]}" if
410
460
  options[:program] and options[:version]
411
461
 
412
462
  # usage
@@ -436,6 +486,7 @@ module Pablo
436
486
  puts strs.join " "
437
487
  else # do short_descs
438
488
  # print short_descs
489
+ puts
439
490
  puts '::Available commands and options'
440
491
  puts
441
492
 
@@ -454,5 +505,17 @@ module Pablo
454
505
  end
455
506
  end
456
507
 
508
+ ##
509
+ # Displays a version message generated from the +:version+ parameter passed to
510
+ # the +parse+ function.
511
+ #
512
+ # +options+ must be a hash of options that will be used to generate the
513
+ # output, namely +:version+, +:program+.
514
+ #
515
+ def self.version options
516
+ puts "#{options[:program]} v#{options[:version]}" if
517
+ options[:program] and options[:version]
518
+ end
519
+
457
520
  end
458
521
 
data/tests/help.rb CHANGED
@@ -16,6 +16,13 @@ def message msg
16
16
  end
17
17
 
18
18
  class Help < Test::Unit::TestCase
19
+
20
+ def assert_output
21
+ puts
22
+ print 'Does the output match? [yn] '
23
+ assert_match /^(y|Y|yes|Yes)?\n$/, gets, "Bad output."
24
+ end
25
+
19
26
  def testHelp
20
27
  args = ['help']
21
28
 
@@ -46,6 +53,7 @@ class Help < Test::Unit::TestCase
46
53
 
47
54
  message '-----------------------------------------'
48
55
  message 'End of expected output'
56
+ assert_output
49
57
  end
50
58
 
51
59
  def testCommandHelp
@@ -83,7 +91,7 @@ class Help < Test::Unit::TestCase
83
91
 
84
92
  message '-----------------------------------------'
85
93
  message 'End of expected output'
86
- puts
94
+ assert_output
87
95
  end
88
96
 
89
97
  def testHelpAliases
@@ -116,6 +124,7 @@ class Help < Test::Unit::TestCase
116
124
 
117
125
  message '-----------------------------------------'
118
126
  message 'End of expected output'
127
+ assert_output
119
128
  end
120
129
 
121
130
  def testHelpDefault
@@ -130,6 +139,61 @@ class Help < Test::Unit::TestCase
130
139
 
131
140
  message '-----------------------------------------'
132
141
  message 'End of expected output'
142
+ assert_output
143
+ end
144
+
145
+ def testVersion
146
+
147
+ puts
148
+ message 'Expected output: a version message'
149
+ message "-----------------------------------------"
150
+
151
+ Pablo::parse ['version'], :program => 'bacon',
152
+ :version => [0,0,1,'alpha'] do
153
+ version!
154
+ end
155
+
156
+ message '-----------------------------------------'
157
+ message 'End of expected output'
158
+ assert_output
159
+ end
160
+
161
+ def testVersion
162
+
163
+ puts
164
+ message 'Expected output: a version message'
165
+ message "-----------------------------------------"
166
+
167
+ Pablo::parse ['version'], :program => 'bacon',
168
+ :version => [0,0,1,'alpha'] do
169
+ version!
170
+ end
171
+
172
+ message '-----------------------------------------'
173
+ message 'End of expected output'
174
+ assert_output
175
+ end
176
+
177
+ def testVersionHelpDefault
178
+
179
+ puts
180
+ message 'Expected output: a help message'
181
+ message "-----------------------------------------"
182
+
183
+ Pablo::parse [], :program => 'bacon',
184
+ :version => [0,0,1,'alpha'] do
185
+ version!
186
+ help!
187
+
188
+ command do
189
+ name 'stupid'
190
+ short_desc 'foo'
191
+ end
192
+ end
193
+
194
+ message '-----------------------------------------'
195
+ message 'End of expected output'
196
+ assert_output
133
197
  end
134
198
  end
135
199
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Pablo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Streitel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-07 00:00:00 -08:00
12
+ date: 2008-12-26 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15