opt-simple 0.9.10 → 0.9.11

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 +14 -3
  2. data/lib/opt_simple.rb +22 -6
  3. data/test/test_usage.rb +60 -0
  4. metadata +2 -2
data/README CHANGED
@@ -18,7 +18,8 @@ can be used to set an option that will be returned in the Result. The 'accumulat
18
18
  method can be used in the option and argument blocks to create a list of values, and in
19
19
  the flag block to increment a counter (with verbosity being the classic example).
20
20
 
21
- The number of arguments are determined by the 'arity' of the block.
21
+ The number of arguments are determined by the 'arity' of the block, or a variable
22
+ number if the splat operator is used.
22
23
 
23
24
  The order in which the parameters are defined dictate their order on the command line.
24
25
 
@@ -62,12 +63,18 @@ It is recommended to install OptSimple using RubyGems:
62
63
 
63
64
  option %w[-p --pattern --glob-pattern], "glob pattern","PATTERN"
64
65
 
66
+ # this block has arity 2, so it expects to args to follow
65
67
  option "--range", "range: min,max (both >0)" do | arg1,arg2 |
66
68
  min,max = [arg1.to_i,arg2.to_i]
67
69
  set_opt [min,max]
68
70
 
69
71
  error "max must be greater than min" unless max > min
70
72
  error "both must be >=0" if min < 0
73
+ end
74
+
75
+ # this block says that there are a variable number of arguments allowed
76
+ option %w[-t --things],"Some things - variable number allowed","THINGS" do | *args |
77
+ args.each {|a| accumulate_opt a }
71
78
  end
72
79
  end
73
80
 
@@ -94,7 +101,9 @@ It is recommended to install OptSimple using RubyGems:
94
101
 
95
102
  # Here, range was set to an Array.
96
103
  puts "Range: #{opts.range.first} #{opts.range.last}"
97
-
104
+
105
+ puts "Things: #{opts.things.inspect}"
106
+
98
107
  Which prints out an automatic usage statement:
99
108
  Usage: opt_ex.rb [options]
100
109
 
@@ -111,7 +120,9 @@ Which prints out an automatic usage statement:
111
120
  -p, --pattern, --glob-pattern PATTERN glob pattern (default is '*')
112
121
 
113
122
  --range ARG range: min,max (both >0) (default is '[5, 10]')
114
-
123
+
124
+ -t, --things THINGS Some things - variable number allowed
125
+
115
126
  -h, --help (for this help message)
116
127
 
117
128
  === A very simple example with no error checking. Use at your own risk!
data/lib/opt_simple.rb CHANGED
@@ -127,13 +127,27 @@ class OptSimple
127
127
  # of @args, so we don't disrupt the other locations, but put them into 'chunks'
128
128
  # backwards to restore their order.
129
129
  chunks = []
130
- arg_locations.sort.reverse.each do |loc|
131
- chunks.unshift @args.slice!(loc .. loc + parm.block.arity)[1..-1]
130
+
131
+ arg_locations.sort.reverse.each do |loc|
132
+ # use the block's arity (Proc.new {|a,b| ...} has arity 2)
133
+ # to determine how many args to slice off
134
+ num_to_get = parm.block.arity
135
+
136
+ # but blocks with arity of -1 take a variable number of args
137
+ # e.g. Proc.new {|*args| } has arity -1 (splat)
138
+ if num_to_get == -1
139
+ # so either go to the next switch, or if there is no switch, to the end of the @args list
140
+ num_to_get = @args[loc + 1 ..-1].index {|a| a.start_with?('-')} || @args[loc + 1 ..-1].length
141
+ end
142
+
143
+ end_loc = loc + num_to_get
144
+ chunks.unshift @args.slice!(loc .. end_loc)[1..-1]
132
145
  end
133
146
 
134
147
  chunks.each do | pieces |
135
- if pieces.length < parm.block.arity or
136
- pieces.any? {|p| p.start_with?('-')}
148
+ if parm.block.arity > 0 and
149
+ (pieces.length < parm.block.arity or
150
+ pieces.any? {|p| p.start_with?('-')})
137
151
  raise OptSimple::ParameterUsageError.new "Not enough args following #{intersection}",self
138
152
  end
139
153
 
@@ -394,7 +408,8 @@ class OptSimple
394
408
  if block_given?
395
409
  @param_options[names.first] = false
396
410
  else
397
- @block = Proc.new { @param_options[names.first] = true}
411
+ #@block = Proc.new { @param_options[names.first] = true}
412
+ @block = Proc.new { set_opt true }
398
413
  end
399
414
  end
400
415
 
@@ -424,7 +439,8 @@ class OptSimple
424
439
  super(switches,help,&block)
425
440
  @metavar = metavar
426
441
  unless block_given?
427
- @block = Proc.new {|arg| @param_options[names.first] = arg}
442
+ #@block = Proc.new {|arg| @param_options[names.first] = arg}
443
+ @block = Proc.new {|arg| set_opt arg}
428
444
  end
429
445
  @first_call = true # use this so we can accumulate non-defaults
430
446
  end
data/test/test_usage.rb CHANGED
@@ -26,6 +26,15 @@ class TestHelpStatement < Test::Unit::TestCase
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ os = OptSimple.new({},%w[--range 5])
31
+ assert_raise(OptSimple::ParameterUsageError) do
32
+ os.parse_opts! do
33
+ argument '--range' do |min,max|
34
+ nil
35
+ end
36
+ end
37
+ end
29
38
  end
30
39
 
31
40
  must "raise error when unknown option is given" do
@@ -135,5 +144,56 @@ class TestHelpStatement < Test::Unit::TestCase
135
144
 
136
145
  assert_equal o.some_stuff, %w[foo bar]
137
146
  end
147
+
148
+ must "successfully pull out two options following a switch when desired" do
149
+ o = OptSimple.new(nil,%w[--range 4 9]).parse_opts! do
150
+ argument "--range","min,max" do | arg1, arg2 |
151
+ set_opt [arg1.to_i,arg2.to_i]
152
+ end
153
+ end
154
+
155
+ assert_equal o.range.first, 4
156
+ assert_equal o.range.last, 9
157
+ end
158
+
159
+
160
+ must "allow for a variable number of options following a switch when a splat is used" do
161
+
162
+ arg_lists = [%w[--things foo --whatever],
163
+ %w[--things foo bar --whatever],
164
+ %w[--whatever --things foo bar baz]]
165
+
166
+ arg_lists.each do |args |
167
+ # Gotta account for '--whatever and --things switch in there'
168
+ expected_len = args.length - 2
169
+ os = OptSimple.new(nil,args).parse_opts! do
170
+ argument "--things" do | *arg |
171
+ arg.each do |a|
172
+ accumulate_opt a
173
+ end
174
+ end
175
+
176
+ flag '--whatever'
177
+
178
+ end
179
+ assert_equal expected_len, os.things.length
180
+ end
181
+
182
+ end
183
+
184
+ # Hmm ... just want to be flexible here. perhaps not necessary
185
+ # Note you still gotta initialize things to an empty list to make it not nil
186
+ must "allow for no options following an option defined with a splat" do
187
+ os = OptSimple.new({:things => [] },%w[--whatever --things]).parse_opts! do
188
+ option "--things" do | *arg |
189
+ arg.each do |a|
190
+ accumulate_opt a
191
+ end
192
+ end
193
+ flag '--whatever'
194
+ end
195
+ # Note that without the defaults, things is nil, but os still includes it
196
+ assert os.things.empty?
197
+ end
138
198
  end
139
199
 
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.10
5
+ version: 0.9.11
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-10-24 00:00:00 +01:00
13
+ date: 2011-11-20 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies: []
16
16