Pablo 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/CHANGELOG +3 -0
  2. data/lib/pablo.rb +34 -8
  3. data/tests/help.rb +46 -15
  4. metadata +4 -2
@@ -0,0 +1,3 @@
1
+ 0.0.1 :: Initial release
2
+ 0.0.2 :: Added version command
3
+ 0.1.0 :: Fixed bug regarding default! and added version and help blocks
@@ -378,12 +378,18 @@ module Pablo
378
378
  #
379
379
  def version? args
380
380
  return false unless @version
381
+
381
382
  found = args.index { |a|
382
383
  @version_aliases.any? { |v| v == a }
383
384
  }
384
- found = 0 if args.empty?
385
+
386
+ found = 0 if args.empty? and not @tokens.any? { |t|
387
+ t.default
388
+ }
389
+
385
390
  return false unless found
386
- Pablo::version @options
391
+
392
+ Pablo::version @options, @version_block
387
393
  return true
388
394
  end
389
395
 
@@ -393,16 +399,23 @@ module Pablo
393
399
  # - executes +Pablo::help+
394
400
  # - and returns true
395
401
  # otherwise only returns false.
402
+ # TODO: help and version add tokens that get default set and call these functions?
396
403
  #
397
404
  def help? args
398
405
  return false unless @help
406
+
399
407
  found = args.index { |a|
400
408
  @help_aliases.any? { |h| h == a }
401
409
  }
402
- found = 0 if args.empty?
410
+
411
+ found = 0 if args.empty? and not @tokens.any? { |t|
412
+ t.default
413
+ }
414
+
403
415
  return false unless found
416
+
404
417
  a = args[found+1..-1] || []
405
- Pablo::help a, @everyone, @options
418
+ Pablo::help a, @everyone, @options, @help_block
406
419
  return true
407
420
  end
408
421
 
@@ -413,9 +426,12 @@ module Pablo
413
426
  # +alis+ are the aliases the help command should respond to.
414
427
  # By default these are _help_, _--help_ and _-h_.
415
428
  #
416
- def help! alis = ['help', '--help', '-h']
429
+ # +block+ will be called after the help text has been rendered.
430
+ #
431
+ def help! alis = ['help', '--help', '-h'], &block
417
432
  @help = true
418
433
  @help_aliases = alis
434
+ @help_block = block
419
435
  end
420
436
 
421
437
  ##TODO: usage!, each token gives it's usage -> [alias1|alias2 usage]
@@ -427,9 +443,12 @@ module Pablo
427
443
  # +alis+ are the aliases the version command should respond to.
428
444
  # By default these are _version_, _--version_ and _-v_.
429
445
  #
430
- def version! alis = ['version', '--version', '-v']
446
+ # +block+ will be called after the version text has been rendered.
447
+ #
448
+ def version! alis = ['version', '--version', '-v'], &block
431
449
  @version = true
432
450
  @version_aliases = alis
451
+ @version_block = block
433
452
  end
434
453
  end
435
454
 
@@ -446,7 +465,9 @@ module Pablo
446
465
  # +options+ must be a hash of options that will be used to generate the
447
466
  # output, namely +:program+, +:version+, +:usage+.
448
467
  #
449
- def self.help args, tokens, options
468
+ # +block+ must be the block to call after rendering, or nil.
469
+ #
470
+ def self.help args, tokens, options, block
450
471
  msg = ""
451
472
  aliases = []
452
473
  tokens.each { |t| t.aliases.each { |a| aliases << [a,t] } }
@@ -503,6 +524,8 @@ module Pablo
503
524
  puts "\n::Defaults" unless strs.empty?
504
525
  puts strs.join ", "
505
526
  end
527
+
528
+ block.call if block
506
529
  end
507
530
 
508
531
  ##
@@ -512,9 +535,12 @@ module Pablo
512
535
  # +options+ must be a hash of options that will be used to generate the
513
536
  # output, namely +:version+, +:program+.
514
537
  #
515
- def self.version options
538
+ # +block+ must be the block to call after rendering, or nil.
539
+ #
540
+ def self.version options, block
516
541
  puts "#{options[:program]} v#{options[:version]}" if
517
542
  options[:program] and options[:version]
543
+ block.call if block
518
544
  end
519
545
 
520
546
  end
@@ -16,7 +16,14 @@ def message msg
16
16
  end
17
17
 
18
18
  class Help < Test::Unit::TestCase
19
-
19
+ def self.val
20
+ @@val
21
+ end
22
+
23
+ def self.val= v
24
+ @@val = v
25
+ end
26
+
20
27
  def assert_output
21
28
  puts
22
29
  print 'Does the output match? [yn] '
@@ -128,7 +135,6 @@ class Help < Test::Unit::TestCase
128
135
  end
129
136
 
130
137
  def testHelpDefault
131
-
132
138
  puts
133
139
  message 'Expected output: an empty help message'
134
140
  message "-----------------------------------------"
@@ -142,24 +148,19 @@ class Help < Test::Unit::TestCase
142
148
  assert_output
143
149
  end
144
150
 
145
- def testVersion
146
-
147
- puts
148
- message 'Expected output: a version message'
149
- message "-----------------------------------------"
151
+ def testHelpBlock
152
+ Help.val = 0
150
153
 
151
- Pablo::parse ['version'], :program => 'bacon',
152
- :version => [0,0,1,'alpha'] do
153
- version!
154
+ Pablo::parse [] do
155
+ help! do
156
+ Help.val = 12
157
+ end
154
158
  end
155
159
 
156
- message '-----------------------------------------'
157
- message 'End of expected output'
158
- assert_output
160
+ assert_equal 12, Help.val
159
161
  end
160
162
 
161
163
  def testVersion
162
-
163
164
  puts
164
165
  message 'Expected output: a version message'
165
166
  message "-----------------------------------------"
@@ -174,8 +175,19 @@ class Help < Test::Unit::TestCase
174
175
  assert_output
175
176
  end
176
177
 
177
- def testVersionHelpDefault
178
+ def testVersionBlock
179
+ Help.val = 0
180
+
181
+ Pablo::parse [] do
182
+ version! do
183
+ Help.val = 12
184
+ end
185
+ end
178
186
 
187
+ assert_equal 12, Help.val
188
+ end
189
+
190
+ def testVersionHelpDefault
179
191
  puts
180
192
  message 'Expected output: a help message'
181
193
  message "-----------------------------------------"
@@ -195,5 +207,24 @@ class Help < Test::Unit::TestCase
195
207
  message 'End of expected output'
196
208
  assert_output
197
209
  end
210
+
211
+ def testHelpVersionNonDefault
212
+ Help.val = 1
213
+
214
+ Pablo::parse [], :program => 'bacon',
215
+ :version => [0,0,1,'alpha'] do
216
+ version!
217
+ help!
218
+
219
+ command do
220
+ name 'stupid'
221
+ short_desc 'foo'
222
+ default!
223
+ run { Help.val = 2 }
224
+ end
225
+ end
226
+
227
+ assert_equal 2, Help.val
228
+ end
198
229
  end
199
230
 
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.2
4
+ version: 0.1.0
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-26 00:00:00 -08:00
12
+ date: 2009-01-01 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -21,9 +21,11 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - README
24
+ - CHANGELOG
24
25
  files:
25
26
  - lib/pablo.rb
26
27
  - README
28
+ - CHANGELOG
27
29
  has_rdoc: true
28
30
  homepage: http://pablo.rubyforge.org/
29
31
  post_install_message: