opt-simple 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +2 -3
  2. data/lib/opt_simple.rb +31 -25
  3. data/test/test_usage.rb +10 -0
  4. metadata +6 -5
data/README CHANGED
@@ -7,15 +7,14 @@ usage statement is constructed.
7
7
 
8
8
  There are three methods to define command line parameters:
9
9
 
10
- flag - a command line switch with no arguments following (although you can still use flag
11
- as a synonym for option. But default behavior is to expect no args.)
10
+ flag - a command line switch with no arguments following
12
11
 
13
12
  option - an optional command line parameter with one or more arguments
14
13
 
15
14
  argument - a mandatory command line parameter with one or more arguments
16
15
 
17
16
  Inside the blocks in flag, option, and argument a shortcut function called 'set_opt'
18
- can be used to set an option that will be returned in the options hash. The 'accumulate_opt'
17
+ can be used to set an option that will be returned in the Result. The 'accumulate_opt'
19
18
  method can be used in the option and argument blocks to create a list of values, and in
20
19
  the flag block to increment a counter (with verbosity being the classic example).
21
20
 
data/lib/opt_simple.rb CHANGED
@@ -4,15 +4,14 @@
4
4
 
5
5
  There are three methods to define command line parameters:
6
6
 
7
- flag - a command line switch with no arguments following (although you can still use flag
8
- as a synonym for option. But default behavior is to expect no args.)
7
+ flag - a command line switch with no arguments following.
9
8
 
10
9
  option - an optional command line parameter with one or more arguments
11
10
 
12
11
  argument - a mandatory command line parameter with one or more arguments
13
12
 
14
13
  Inside the blocks in flag, option, and argument a shortcut function called 'set_opt'
15
- can be used to set an option that will be returned in the options hash. The 'accumulate_opt'
14
+ can be used to set an option that will be returned in the Result. The 'accumulate_opt'
16
15
  method can be used in the option and argument blocks to create a list of values, and in
17
16
  the flag block to increment a counter (with verbosity being the classic example).
18
17
 
@@ -206,17 +205,20 @@ class OptSimple
206
205
  @args.delete_if {|a| not @results.positional_args.include?(a)}
207
206
  end
208
207
 
209
- # Registers an optional parameter, usually with nothing following it.
210
- # although if the block has arity > 0, we'll be happy to handle
211
- # it as such.
208
+ # Registers an optional parameter, usually with nothing following it.
209
+ # If not set on the CL, it will be false in the Result.
212
210
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
213
211
  # 'help' provides a description of the parameter
214
212
  # and an optional block can do parameter validation/transformation.
215
213
  # If no block is given, then the strings specified (after the
216
- # leading '-'s removed) will be used as keys in the options hash
217
- # and the values set to true when seen in the args array
214
+ # leading '-'s removed) will be used as keys in the Result
215
+ # and the values set to true when seen on the CL
218
216
  def flag(switches,help="",&block)
219
- add_parameter(Flag.new(switches,help,&block))
217
+ parm = Flag.new(switches,help,&block)
218
+ add_parameter(parm)
219
+
220
+ # use the first name b/c they are all aliased anyways.
221
+ @defaults[parm.names.first] = false
220
222
  end
221
223
 
222
224
  # Registers an optional parameter, with one or more argument following it.
@@ -225,8 +227,8 @@ class OptSimple
225
227
  # usage statement.
226
228
  # and an optional block can do parameter validation/transformation.
227
229
  # If no block is given, then the strings specified (after the
228
- # leading '-'s removed) will be used as keys in the options hash
229
- # and the values set to the arg following the switch in the args array.
230
+ # leading '-'s removed) will be used as keys in the Result
231
+ # and the values set to the arg following the switch on the CL.
230
232
  def option(switches,help="",metavar="ARG",&block)
231
233
  add_parameter(Option.new(switches,help,metavar,&block))
232
234
  end
@@ -237,7 +239,7 @@ class OptSimple
237
239
  # usage statement.
238
240
  # and an optional block can do parameter validation/transformation.
239
241
  # If no block is given, then the strings specified (after the
240
- # leading '-'s removed) will be used as keys in the options hash
242
+ # leading '-'s removed) will be used as keys in the Result
241
243
  # and the values set to the arg following the switch the args array.
242
244
  def argument(switches,help="",metavar="ARG",&block)
243
245
  add_parameter(Argument.new(switches,help,metavar,&block))
@@ -363,19 +365,18 @@ class OptSimple
363
365
  end
364
366
 
365
367
  # An optional parameter, usually with nothing following it.
366
- # although if the block has arity > 0, we'll be happy to handle
367
- # it as such.
368
+ # If not set on the CL, it will be false in the Result.
368
369
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
369
370
  # 'help' provides a description of the parameter
370
371
  # and an optional block can do parameter validation/transformation.
371
372
  # If no block is given, then the strings specified (after the
372
- # leading '-'s removed) will be used as keys in the options hash
373
- # and the values set to true when seen in the args array
373
+ # leading '-'s removed) will be used as keys in the Result
374
+ # and the values set to true when seen on the CL
374
375
  class Flag < Parameter
375
376
  def initialize(switches,help="",&block)
376
377
  super(switches,help,&block)
377
378
  if block_given?
378
- @param_options[names.first] = 0
379
+ @param_options[names.first] = false
379
380
  else
380
381
  @block = Proc.new { @param_options[names.first] = true}
381
382
  end
@@ -383,7 +384,12 @@ class OptSimple
383
384
 
384
385
  # increment the parameter by one every time it is seen on the CL
385
386
  def accumulate_opt
386
- @param_options[names.first] += 1
387
+ # if this is the first time seen, set it to 1.
388
+ if @param_options[names.first] == false
389
+ @param_options[names.first] = 1
390
+ else
391
+ @param_options[names.first] += 1
392
+ end
387
393
  end
388
394
 
389
395
  end
@@ -394,8 +400,8 @@ class OptSimple
394
400
  # usage statement.
395
401
  # and an optional block can do parameter validation/transformation.
396
402
  # If no block is given, then the strings specified (after the
397
- # leading '-'s removed) will be used as keys in the options hash
398
- # and the values set to the arg following the switch in the args array.
403
+ # leading '-'s removed) will be used as keys in the Result
404
+ # and the values set to the arg following the switch on the CL.
399
405
  class Option < Parameter
400
406
  attr_reader = :metavar
401
407
  def initialize(switches,help="",metavar="ARG",&block)
@@ -430,7 +436,7 @@ class OptSimple
430
436
  # usage statement.
431
437
  # and an optional block can do parameter validation/transformation.
432
438
  # If no block is given, then the strings specified (after the
433
- # leading '-'s removed) will be used as keys in the options hash
439
+ # leading '-'s removed) will be used as keys in the Result
434
440
  # and the values set to the arg following the switch the args array.
435
441
  class Argument < Option
436
442
  def mandatory?; true; end
@@ -456,8 +462,8 @@ class OptSimple
456
462
  # on the command line
457
463
  class ParameterUsageError < Error;end
458
464
 
459
- # The results after parsing the CL in a hash-like object with method
460
- # syntax for getters/setters as well. Each result that belong to the same
465
+ # The Results after parsing the CL in a hash-like object with method
466
+ # syntax for getters/setters as well. Each Result that belong to the same
461
467
  # Parameter are aliased to keep consistent
462
468
  class Result
463
469
  attr_accessor :positional_args
@@ -498,14 +504,14 @@ class OptSimple
498
504
  hash.keys.each { |k| self[k] = hash[k] }
499
505
  end
500
506
 
501
- # merge non-destructively, and return the result
507
+ # merge non-destructively, and return the Result
502
508
  def merge(other)
503
509
  r = Result.new
504
510
  r.merge! self
505
511
  r.merge! other
506
512
  end
507
513
 
508
- # merge into this Result and return the result
514
+ # merge into this Result and return the Result
509
515
  def merge!(other)
510
516
  other.aliases.each do | a |
511
517
  add_alias(a)
data/test/test_usage.rb CHANGED
@@ -105,5 +105,15 @@ class TestHelpStatement < Test::Unit::TestCase
105
105
  assert_equal o['i'],'foo.bar'
106
106
  assert_equal o['o'],'bar.out'
107
107
  end
108
+
109
+ must "set flags to false by default" do
110
+ os = OptSimple.new
111
+ o = os.parse_opts! do
112
+ flag %w[-v]
113
+ end
114
+
115
+ assert_equal o.v, false
116
+ end
117
+
108
118
  end
109
119
 
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt-simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ prerelease:
5
+ version: 0.9.5
5
6
  platform: ruby
6
7
  authors:
7
8
  - Ethan Stryker
@@ -9,7 +10,7 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2011-03-18 00:00:00 +00:00
13
+ date: 2011-03-25 00:00:00 +00:00
13
14
  default_executable:
14
15
  dependencies: []
15
16
 
@@ -41,21 +42,21 @@ rdoc_options: []
41
42
  require_paths:
42
43
  - lib
43
44
  required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
44
46
  requirements:
45
47
  - - ">="
46
48
  - !ruby/object:Gem::Version
47
49
  version: "0"
48
- version:
49
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
53
55
  version: "0"
54
- version:
55
56
  requirements: []
56
57
 
57
58
  rubyforge_project: opt-simple
58
- rubygems_version: 1.3.5
59
+ rubygems_version: 1.6.2
59
60
  signing_key:
60
61
  specification_version: 3
61
62
  summary: A simple and elegant command line option parser.