opt-simple 0.9.7 → 0.9.8

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/opt_simple.rb +24 -17
  2. data/test/test_usage.rb +10 -0
  3. metadata +2 -2
@@ -115,16 +115,6 @@ class OptSimple
115
115
  @parameters.each do | parm |
116
116
  intersection = @args & parm.switches
117
117
 
118
- # load the defaults for this particular parm no matter what is on the CL
119
- default_switches = @defaults.keys & parm.names
120
- if default_switches.length > 1
121
- raise OptSimple::Error "Clashes in the defaults for #{parm.switches}"
122
- elsif default_switches.length == 1
123
- # set the default value before we see what is on the CL
124
- parm.param_options[default_switches.first] = @defaults[default_switches.first]
125
- @results.merge! parm.param_options
126
- end
127
-
128
118
  unless intersection.empty?
129
119
  mandatory_check.delete(parm.switches)
130
120
 
@@ -215,10 +205,12 @@ class OptSimple
215
205
  # and the values set to true when seen on the CL
216
206
  def flag(switches,help="",&block)
217
207
  parm = Flag.new(switches,help,&block)
218
- add_parameter(parm)
208
+ parm.set_opt false
209
+ add_parameter parm
219
210
 
211
+ # add it to the results to make checking flags nicer.
220
212
  # use the first name b/c they are all aliased anyways.
221
- @defaults[parm.names.first] = false
213
+ @results.merge! parm.param_options
222
214
  end
223
215
 
224
216
  # Registers an optional parameter, with one or more argument following it.
@@ -230,7 +222,18 @@ class OptSimple
230
222
  # leading '-'s removed) will be used as keys in the Result
231
223
  # and the values set to the arg following the switch on the CL.
232
224
  def option(switches,help="",metavar="ARG",&block)
233
- add_parameter(Option.new(switches,help,metavar,&block))
225
+ parm = Option.new(switches,help,metavar,&block)
226
+
227
+ # set the defaults, if there are any in the defaults hash
228
+ intersection = parm.names & @defaults.keys
229
+ if intersection.length == 1
230
+ parm.set_opt @defaults[intersection.first]
231
+ @results.merge! parm.param_options
232
+ elsif intersection.length > 1
233
+ raise OptSimple::Error "Clashes in the defaults for #{parm.switches}"
234
+ end
235
+
236
+ add_parameter(parm)
234
237
  end
235
238
 
236
239
  # Registers a mandatory parameter, with one or more argument following it.
@@ -418,11 +421,10 @@ class OptSimple
418
421
  def initialize(switches,help="",metavar="ARG",&block)
419
422
  super(switches,help,&block)
420
423
  @metavar = metavar
421
- if block_given?
422
- @param_options[names.first] = []
423
- else
424
+ unless block_given?
424
425
  @block = Proc.new {|arg| @param_options[names.first] = arg}
425
426
  end
427
+ @first_call = true
426
428
  end
427
429
 
428
430
  def switch_len #:nodoc:
@@ -436,7 +438,12 @@ class OptSimple
436
438
 
437
439
  # append val to the parameter list
438
440
  def accumulate_opt(val)
439
- @param_options[names.first] << val
441
+ if @first_call
442
+ @param_options[names.first] = [val].flatten
443
+ else
444
+ @param_options[names.first] << val
445
+ end
446
+ @first_call = false
440
447
  end
441
448
 
442
449
  end
@@ -125,5 +125,15 @@ class TestHelpStatement < Test::Unit::TestCase
125
125
  assert_equal o[:some_stuff],"foo"
126
126
  end
127
127
 
128
+ must "overwrite defaults when accumulate opt is used and option is seen on the CL" do
129
+ os = OptSimple.new({:some_stuff => ['bar']},%w[--some-stuff foo --some-stuff bar])
130
+ o = os.parse_opts! do
131
+ option %w[-s --some-stuff] do | arg |
132
+ accumulate_opt arg
133
+ end
134
+ end
135
+
136
+ assert_equal o.some_stuff, %w[foo bar]
137
+ end
128
138
  end
129
139
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: opt-simple
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.7
5
+ version: 0.9.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ethan Stryker
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-03 00:00:00 +01:00
13
+ date: 2011-04-05 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16