slop 1.6.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGES.md +6 -0
- data/LICENSE +1 -1
- data/README.md +10 -85
- data/Rakefile +4 -0
- data/lib/slop.rb +36 -55
- data/lib/slop/option.rb +13 -25
- data/lib/slop/options.rb +11 -5
- data/test/helper.rb +1 -0
- data/test/slop_test.rb +4 -3
- metadata +14 -3
data/.gitignore
CHANGED
data/CHANGES.md
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -20,10 +20,10 @@ Usage
|
|
20
20
|
-----
|
21
21
|
# parse assumes ARGV, otherwise you can pass it your own Array
|
22
22
|
opts = Slop.parse do
|
23
|
-
on :v, :verbose, 'Enable verbose mode'
|
24
|
-
on :n, :name, 'Your name', true
|
25
|
-
on :s, :sex, 'Your sex', :optional => false
|
26
|
-
on
|
23
|
+
on :v, :verbose, 'Enable verbose mode' # boolean value
|
24
|
+
on :n, :name, 'Your name', true # option requires a compulsory argument
|
25
|
+
on :s, :sex, 'Your sex', :optional => false # the same thing
|
26
|
+
on '-a', '--age', 'Your age', :optional => true # optional argument
|
27
27
|
end
|
28
28
|
|
29
29
|
# if ARGV is `-v --name 'lee jarvis' -s male`
|
@@ -37,25 +37,6 @@ You can also return your options as a Hash
|
|
37
37
|
|
38
38
|
opts.to_hash #=> {'name' => 'Lee Jarvis', 'verbose' => true, 'age' => nil, 'sex' => 'male'}
|
39
39
|
|
40
|
-
# Symbols
|
41
|
-
opts.to_hash(true) #=> {:name => 'Lee Jarvis', :verbose => true, :age => nil, :sex => 'male'}
|
42
|
-
|
43
|
-
If you don't like the method `on` (because it sounds like the option **expects**
|
44
|
-
a block), you can use the `opt` or `option` alternatives.
|
45
|
-
|
46
|
-
on :v, :verbose
|
47
|
-
opt :v, :verbose
|
48
|
-
option :v, :verbose
|
49
|
-
|
50
|
-
If you don't like that Slop evaluates your block, or you want slop access
|
51
|
-
inside of your block without referring to `self`, you can pass a block argument to
|
52
|
-
`parse`.
|
53
|
-
|
54
|
-
Slop.parse do |opts|
|
55
|
-
opts.on :v, :verbose
|
56
|
-
opts.on :n, :name, 'Your name', true
|
57
|
-
end
|
58
|
-
|
59
40
|
If you want some pretty output for the user to see your options, you can just
|
60
41
|
send the Slop object to `puts` or use the `help` method.
|
61
42
|
|
@@ -170,7 +151,7 @@ Slop also allows you to prefix `--no-` to an option which will force the option
|
|
170
151
|
to return a false value.
|
171
152
|
|
172
153
|
opts = Slop.parse do
|
173
|
-
|
154
|
+
on :v, :verbose, :default => true
|
174
155
|
end
|
175
156
|
|
176
157
|
# with no command line options
|
@@ -233,28 +214,6 @@ What would Slop be if it didn't know what ranges were?
|
|
233
214
|
# ARGV is `--range 1..10` or 1-10, or 1,10 (yes Slop supports them all)
|
234
215
|
opts[:range].to_a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
235
216
|
|
236
|
-
Ugh, Symbols
|
237
|
-
------------
|
238
|
-
|
239
|
-
Fine, don't use them
|
240
|
-
|
241
|
-
Slop.parse do
|
242
|
-
on :n, :name, 'Your name'
|
243
|
-
on 'n', 'name', 'Your name'
|
244
|
-
on '-n', '--name', 'Your name'
|
245
|
-
end
|
246
|
-
|
247
|
-
All of these options will do the same thing
|
248
|
-
|
249
|
-
Ugh, Blocks
|
250
|
-
-----------
|
251
|
-
|
252
|
-
C'mon man, this is Ruby, GTFO if you don't like blocks.
|
253
|
-
|
254
|
-
opts = Slop.new
|
255
|
-
opts.on :v, :verbose
|
256
|
-
opts.parse
|
257
|
-
|
258
217
|
Smart
|
259
218
|
-----
|
260
219
|
|
@@ -284,37 +243,6 @@ when an invalid option is found (`false` by default):
|
|
284
243
|
Slop.new(:strict => true).parse(%w/--foo/)
|
285
244
|
# => Slop::InvalidOptionError: Unknown option -- 'foo'
|
286
245
|
|
287
|
-
and it handles multiple invalid options with a sprinkling of pluralization:
|
288
|
-
|
289
|
-
Slop.new(:strict => true).parse(%w/--foo --bar -z/)
|
290
|
-
# => Slop::InvalidOptionError: Unknown options -- 'foo', 'bar', 'z'
|
291
|
-
|
292
|
-
Significantly, however, Slop will still parse the valid options:
|
293
|
-
|
294
|
-
slop = Slop.new(:strict => true, :help => true) do
|
295
|
-
banner "Usage:\n\t./awesome_sauce [options]\n\nOptions:"
|
296
|
-
on :n, :name, 'Your name'
|
297
|
-
end
|
298
|
-
|
299
|
-
begin
|
300
|
-
slop.parse(%w/--foo --bar -z/)
|
301
|
-
rescue Slop::InvalidOptionError => e
|
302
|
-
puts "\n#{e.message}\n\n"
|
303
|
-
puts slop
|
304
|
-
exit
|
305
|
-
end
|
306
|
-
|
307
|
-
yields:
|
308
|
-
|
309
|
-
Unknown options -- 'foo', 'bar', 'z'
|
310
|
-
|
311
|
-
Usage:
|
312
|
-
./awesome_sauce [options]
|
313
|
-
|
314
|
-
Options:
|
315
|
-
-n, --name Your name
|
316
|
-
-h, --help Print this help message
|
317
|
-
|
318
246
|
Commands
|
319
247
|
--------
|
320
248
|
|
@@ -347,11 +275,8 @@ Here's how commands might look:
|
|
347
275
|
end
|
348
276
|
end
|
349
277
|
|
350
|
-
*
|
351
|
-
*
|
352
|
-
|
353
|
-
* Run with: `run.rb clean -v`
|
354
|
-
* Output: `Enabled verbose mode for clean`
|
278
|
+
* `run.rb -v #=> version 1`
|
279
|
+
* `run.rb clean -v #=> Enabled verbose mode for clean`
|
355
280
|
|
356
281
|
Woah woah, why you hating on OptionParser?
|
357
282
|
------------------------------------------
|
@@ -370,7 +295,7 @@ gather a bunch of key/value options, usually you would do something like this:
|
|
370
295
|
end
|
371
296
|
|
372
297
|
opt.on('-a', '--age AGE', 'Your age') do |age|
|
373
|
-
|
298
|
+
things[:age] = age.to_i
|
374
299
|
end
|
375
300
|
|
376
301
|
# you get the point
|
@@ -379,14 +304,14 @@ gather a bunch of key/value options, usually you would do something like this:
|
|
379
304
|
opt.parse
|
380
305
|
things #=> { :name => 'lee', :age => 105 }
|
381
306
|
|
382
|
-
Which is all great and stuff, but it can lead to some repetition
|
307
|
+
Which is all great and stuff, but it can lead to some repetition. The same
|
383
308
|
thing in Slop:
|
384
309
|
|
385
310
|
require 'slop'
|
386
311
|
|
387
312
|
opts = Slop.parse do
|
388
313
|
on :n, :name, 'Your name', true
|
389
|
-
on :a, :age, 'Your age', true
|
314
|
+
on :a, :age, 'Your age', true, :as => :int
|
390
315
|
end
|
391
316
|
|
392
317
|
opts.to_hash(true) #=> { :name => 'lee', :age => 105 }
|
data/Rakefile
CHANGED
data/lib/slop.rb
CHANGED
@@ -16,7 +16,7 @@ class Slop
|
|
16
16
|
class InvalidOptionError < RuntimeError; end
|
17
17
|
|
18
18
|
# @return [String] The current version string
|
19
|
-
VERSION = '1.6.
|
19
|
+
VERSION = '1.6.1'
|
20
20
|
|
21
21
|
# Parses the items from a CLI format into a friendly object.
|
22
22
|
#
|
@@ -27,18 +27,17 @@ class Slop
|
|
27
27
|
# on :n, :name, 'Your name'
|
28
28
|
# on :a, :age, 'Your age'
|
29
29
|
# end
|
30
|
-
# -------
|
31
|
-
# program.rb --verbose -n 'Emily' -a 25
|
32
30
|
# @return [Slop] Returns an instance of Slop.
|
33
31
|
def self.parse(items=ARGV, options={}, &block)
|
34
|
-
initialize_and_parse
|
32
|
+
initialize_and_parse items, false, options, &block
|
35
33
|
end
|
36
34
|
|
37
|
-
# Identical to {Slop.parse}, but removes parsed options from the
|
35
|
+
# Identical to {Slop.parse}, but removes parsed options from the
|
36
|
+
# original Array.
|
38
37
|
#
|
39
38
|
# @return [Slop] Returns an instance of Slop.
|
40
39
|
def self.parse!(items=ARGV, options={}, &block)
|
41
|
-
initialize_and_parse
|
40
|
+
initialize_and_parse items, true, options, &block
|
42
41
|
end
|
43
42
|
|
44
43
|
# @return [Options]
|
@@ -73,9 +72,8 @@ class Slop
|
|
73
72
|
# @option opts [Proc, #call] :on_noopts Trigger an event when no options
|
74
73
|
# are found
|
75
74
|
def initialize(*opts, &block)
|
76
|
-
sloptions = {}
|
77
|
-
sloptions
|
78
|
-
sloptions[:banner] = opts.shift if opts[0].respond_to?(:to_str)
|
75
|
+
sloptions = opts.last.is_a?(Hash) ? opts.pop : {}
|
76
|
+
sloptions[:banner] = opts.shift if opts[0].respond_to? :to_str
|
79
77
|
opts.each { |o| sloptions[o] = true }
|
80
78
|
|
81
79
|
@options = Options.new
|
@@ -92,17 +90,14 @@ class Slop
|
|
92
90
|
@on_noopts = sloptions[:on_noopts] || sloptions[:on_optionless]
|
93
91
|
@sloptions = sloptions
|
94
92
|
|
95
|
-
io = sloptions[:io] || $stderr
|
96
|
-
eoh = true if sloptions[:exit_on_help].nil?
|
97
|
-
|
98
93
|
if block_given?
|
99
94
|
block.arity == 1 ? yield(self) : instance_eval(&block)
|
100
95
|
end
|
101
96
|
|
102
97
|
if sloptions[:help]
|
103
98
|
on :h, :help, 'Print this help message', :tail => true do
|
104
|
-
io.puts help
|
105
|
-
exit
|
99
|
+
(sloptions[:io] || $stderr).puts help
|
100
|
+
exit unless sloptions[:exit_on_help] == false
|
106
101
|
end
|
107
102
|
end
|
108
103
|
end
|
@@ -173,11 +168,10 @@ class Slop
|
|
173
168
|
# end
|
174
169
|
# @return [Slop::Option]
|
175
170
|
def option(*args, &block)
|
176
|
-
options = args.
|
177
|
-
options ||= {}
|
171
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
178
172
|
|
179
|
-
short, long, desc, arg = clean_options
|
180
|
-
option = Option.new
|
173
|
+
short, long, desc, arg = clean_options args
|
174
|
+
option = Option.new self, short, long, desc, arg, options, &block
|
181
175
|
@options << option
|
182
176
|
|
183
177
|
option
|
@@ -205,8 +199,7 @@ class Slop
|
|
205
199
|
raise ArgumentError, "command `#{label}` already exists"
|
206
200
|
end
|
207
201
|
|
208
|
-
|
209
|
-
slop = Slop.new(options)
|
202
|
+
slop = Slop.new @sloptions.merge options
|
210
203
|
@commands[label] = slop
|
211
204
|
|
212
205
|
if block_given?
|
@@ -218,7 +211,7 @@ class Slop
|
|
218
211
|
|
219
212
|
# Trigger an event when Slop has no values to parse
|
220
213
|
#
|
221
|
-
# @param [Object,
|
214
|
+
# @param [Object, #call] obj The object (which can be anything
|
222
215
|
# responding to `call`)
|
223
216
|
# @example
|
224
217
|
# Slop.parse do
|
@@ -232,7 +225,7 @@ class Slop
|
|
232
225
|
|
233
226
|
# Trigger an event when the arguments contain no options
|
234
227
|
#
|
235
|
-
# @param [Object,
|
228
|
+
# @param [Object, #call] obj The object to be triggered (anything
|
236
229
|
# responding to `call`)
|
237
230
|
# @example
|
238
231
|
# Slop.parse do
|
@@ -253,19 +246,21 @@ class Slop
|
|
253
246
|
# opts.to_hash(true) #=> { :name => 'Emily' }
|
254
247
|
# @return [Hash]
|
255
248
|
def to_hash(symbols=false)
|
256
|
-
@options.to_hash
|
249
|
+
@options.to_hash symbols
|
257
250
|
end
|
258
251
|
alias :to_h :to_hash
|
259
252
|
|
260
253
|
# Allows you to check whether an option was specified in the parsed list.
|
254
|
+
# Merely sugar for `present?`
|
261
255
|
#
|
262
256
|
# @example
|
263
257
|
# #== ruby foo.rb -v
|
264
258
|
# opts.verbose? #=> true
|
265
259
|
# opts.name? #=> false
|
266
|
-
# @
|
260
|
+
# @see Slop#present?
|
261
|
+
# @return [Boolean] true if this option is present, false otherwise
|
267
262
|
def method_missing(meth, *args, &block)
|
268
|
-
super unless meth.to_s
|
263
|
+
super unless meth.to_s[-1, 1] == '?'
|
269
264
|
present? meth.to_s.chomp '?'
|
270
265
|
end
|
271
266
|
|
@@ -274,7 +269,7 @@ class Slop
|
|
274
269
|
#
|
275
270
|
# @param [Object] The object name to check
|
276
271
|
# @since 1.5.0
|
277
|
-
# @return [Boolean] true if this option is present
|
272
|
+
# @return [Boolean] true if this option is present, false otherwise
|
278
273
|
def present?(option_name)
|
279
274
|
!!get(option_name)
|
280
275
|
end
|
@@ -323,10 +318,10 @@ class Slop
|
|
323
318
|
elsif !items.any? {|i| i.to_s[/\A--?/] } && @on_noopts.respond_to?(:call)
|
324
319
|
@on_noopts.call self
|
325
320
|
return items
|
321
|
+
elsif execute_command(items, delete)
|
322
|
+
return items
|
326
323
|
end
|
327
324
|
|
328
|
-
return if execute_command(items, delete)
|
329
|
-
|
330
325
|
trash = []
|
331
326
|
|
332
327
|
items.each_with_index do |item, index|
|
@@ -358,7 +353,7 @@ class Slop
|
|
358
353
|
option.call unless option.omit_exec?(items)
|
359
354
|
end
|
360
355
|
else
|
361
|
-
|
356
|
+
@invalid_options << flag if item[/\A--?/] && @strict
|
362
357
|
block.call(item) if block_given? && !trash.include?(index)
|
363
358
|
end
|
364
359
|
end
|
@@ -370,15 +365,13 @@ class Slop
|
|
370
365
|
|
371
366
|
def check_valid_argument!(option, argument)
|
372
367
|
if !option.accepts_optional_argument? && flag?(argument)
|
373
|
-
raise MissingArgumentError,
|
374
|
-
"'#{option.key}' expects an argument, none given"
|
368
|
+
raise MissingArgumentError, "'#{option.key}' expects an argument, none given"
|
375
369
|
end
|
376
370
|
end
|
377
371
|
|
378
372
|
def check_matching_argument!(option, argument)
|
379
373
|
if option.match && !argument.match(option.match)
|
380
|
-
raise InvalidArgumentError,
|
381
|
-
"'#{argument}' does not match #{option.match.inspect}"
|
374
|
+
raise InvalidArgumentError, "'#{argument}' does not match #{option.match.inspect}"
|
382
375
|
end
|
383
376
|
end
|
384
377
|
|
@@ -386,15 +379,10 @@ class Slop
|
|
386
379
|
if option.accepts_optional_argument?
|
387
380
|
option.call
|
388
381
|
else
|
389
|
-
raise MissingArgumentError,
|
390
|
-
"'#{flag}' expects an argument, none given"
|
382
|
+
raise MissingArgumentError, "'#{flag}' expects an argument, none given"
|
391
383
|
end
|
392
384
|
end
|
393
385
|
|
394
|
-
def check_invalid_option!(item, flag)
|
395
|
-
@invalid_options << flag if item[/\A--?/] && @strict
|
396
|
-
end
|
397
|
-
|
398
386
|
def raise_if_invalid_options!
|
399
387
|
return if !@strict || @invalid_options.empty?
|
400
388
|
message = "Unknown option"
|
@@ -404,18 +392,15 @@ class Slop
|
|
404
392
|
end
|
405
393
|
|
406
394
|
def enable_multiple_switches(item)
|
407
|
-
item[1..-1].
|
395
|
+
item[1..-1].each_char do |switch|
|
408
396
|
if option = @options[switch]
|
409
397
|
if option.expects_argument?
|
410
|
-
raise MissingArgumentError,
|
411
|
-
"'-#{switch}' expects an argument, used in multiple_switch context"
|
398
|
+
raise MissingArgumentError, "'-#{switch}' expects an argument, used in multiple_switch context"
|
412
399
|
else
|
413
400
|
option.argument_value = true
|
414
401
|
end
|
415
402
|
else
|
416
|
-
if @strict
|
417
|
-
raise InvalidOptionError, "Unknown option '-#{switch}'"
|
418
|
-
end
|
403
|
+
raise InvalidOptionError, "Unknown option '-#{switch}'" if @strict
|
419
404
|
end
|
420
405
|
end
|
421
406
|
end
|
@@ -423,9 +408,7 @@ class Slop
|
|
423
408
|
def extract_option(item, flag)
|
424
409
|
if item[0, 1] == '-'
|
425
410
|
option = @options[flag]
|
426
|
-
|
427
|
-
option = @options[flag.downcase]
|
428
|
-
end
|
411
|
+
option ||= @options[flag.downcase] if @ignore_case
|
429
412
|
end
|
430
413
|
unless option
|
431
414
|
case item
|
@@ -437,8 +420,7 @@ class Slop
|
|
437
420
|
option = @options[flag]
|
438
421
|
end
|
439
422
|
when /\A--([^=]+)=(.+)\z/
|
440
|
-
option = @options[$1]
|
441
|
-
argument = $2
|
423
|
+
option, argument = @options[$1], $2
|
442
424
|
when /\A--no-(.+)\z/
|
443
425
|
option = @options[$1]
|
444
426
|
option.force_argument_value(false) if option
|
@@ -448,9 +430,8 @@ class Slop
|
|
448
430
|
end
|
449
431
|
|
450
432
|
def execute_command(items, delete)
|
451
|
-
command = items[0]
|
452
|
-
|
453
|
-
if @commands.key?(command)
|
433
|
+
command = @commands.keys.find { |cmd| cmd.to_s == items[0].to_s }
|
434
|
+
if command
|
454
435
|
items.shift
|
455
436
|
opts = @commands[command]
|
456
437
|
delete ? opts.parse!(items) : opts.parse(items)
|
@@ -470,7 +451,7 @@ class Slop
|
|
470
451
|
end
|
471
452
|
|
472
453
|
long = args.first
|
473
|
-
boolean = [true, false].include?
|
454
|
+
boolean = [true, false].include? long
|
474
455
|
if !boolean && long.to_s =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]+\z/
|
475
456
|
options.push args.shift.to_s.sub(/\A--?/, '')
|
476
457
|
else
|
@@ -482,6 +463,6 @@ class Slop
|
|
482
463
|
end
|
483
464
|
|
484
465
|
def flag?(str)
|
485
|
-
str =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]
|
466
|
+
str =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
|
486
467
|
end
|
487
468
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -32,10 +32,6 @@ class Slop
|
|
32
32
|
# @return [Integer] The amount of times this option has been invoked
|
33
33
|
attr_accessor :count
|
34
34
|
|
35
|
-
# @return [Object] Omit execution of this Options block or callback if
|
36
|
-
# this object exists in the Array of items passed to `Slop.new`
|
37
|
-
attr_accessor :unless
|
38
|
-
|
39
35
|
# @param [Slop] slop
|
40
36
|
# @param [String, #to_s] short
|
41
37
|
# @param [String, #to_s] long
|
@@ -51,24 +47,20 @@ class Slop
|
|
51
47
|
# @option options [Boolean] :tail (false)
|
52
48
|
# @option options [Regexp] :match
|
53
49
|
# @option options [String, #to_s] :unless
|
54
|
-
# @option options [Boolean, String] :help
|
55
|
-
def initialize(slop, short, long, description, argument, options
|
50
|
+
# @option options [Boolean, String] :help (true)
|
51
|
+
def initialize(slop, short, long, description, argument, options, &blk)
|
56
52
|
@slop = slop
|
57
53
|
@short_flag = short
|
58
54
|
@long_flag = long
|
59
55
|
@description = description
|
56
|
+
@argument = argument
|
60
57
|
@options = options
|
61
58
|
|
62
|
-
@expects_argument = argument
|
63
|
-
@expects_argument = true if options[:optional] == false
|
64
|
-
|
65
59
|
@tail = options[:tail]
|
66
60
|
@match = options[:match]
|
67
|
-
@delimiter = options
|
68
|
-
@limit = options
|
69
|
-
@
|
70
|
-
@help = options[:help]
|
71
|
-
@help = true if @help.nil?
|
61
|
+
@delimiter = options.fetch(:delimiter, ',')
|
62
|
+
@limit = options.fetch(:limit, 0)
|
63
|
+
@help = options.fetch(:help, true)
|
72
64
|
|
73
65
|
@forced = false
|
74
66
|
@argument_value = nil
|
@@ -82,7 +74,7 @@ class Slop
|
|
82
74
|
|
83
75
|
# @return [Boolean] true if this option expects an argument
|
84
76
|
def expects_argument?
|
85
|
-
@
|
77
|
+
@argument || @options[:argument] || @options[:optional] == false
|
86
78
|
end
|
87
79
|
|
88
80
|
# @return [Boolean] true if this option accepts an optional argument
|
@@ -103,14 +95,12 @@ class Slop
|
|
103
95
|
return if value.nil?
|
104
96
|
|
105
97
|
case @options[:as].to_s.downcase
|
106
|
-
when 'array'
|
107
|
-
|
108
|
-
when '
|
109
|
-
value_to_range value
|
98
|
+
when 'array'; value.split @delimiter, @limit
|
99
|
+
when 'range'; value_to_range value
|
100
|
+
when 'float'; value.to_s.to_f
|
110
101
|
when 'string', 'str'; value.to_s
|
111
102
|
when 'symbol', 'sym'; value.to_s.to_sym
|
112
103
|
when 'integer', 'int'; value.to_s.to_i
|
113
|
-
when 'float'; value.to_s.to_f
|
114
104
|
else
|
115
105
|
value
|
116
106
|
end
|
@@ -136,7 +126,7 @@ class Slop
|
|
136
126
|
# @return [Boolean] true if this options `:unless` argument exists
|
137
127
|
# inside *items*
|
138
128
|
def omit_exec?(items)
|
139
|
-
string = @unless.to_s.sub(/\A--?/, '')
|
129
|
+
string = @options[:unless].to_s.sub(/\A--?/, '')
|
140
130
|
items.any? { |i| i.to_s.sub(/\A--?/, '') == string }
|
141
131
|
end
|
142
132
|
|
@@ -158,11 +148,9 @@ class Slop
|
|
158
148
|
size = @long_flag.size
|
159
149
|
end
|
160
150
|
diff = @slop.longest_flag - size
|
161
|
-
|
162
|
-
out += spaces
|
151
|
+
out += " " * (diff + 6)
|
163
152
|
else
|
164
|
-
|
165
|
-
out += spaces
|
153
|
+
out += " " * (@slop.longest_flag + 8)
|
166
154
|
end
|
167
155
|
|
168
156
|
"#{out}#{@description}"
|
data/lib/slop/options.rb
CHANGED
@@ -2,6 +2,7 @@ class Slop
|
|
2
2
|
class Options < Array
|
3
3
|
|
4
4
|
# @param [Boolean] symbols true to cast hash keys to symbols
|
5
|
+
# @see Slop#to_hash
|
5
6
|
# @return [Hash]
|
6
7
|
def to_hash(symbols)
|
7
8
|
reduce({}) do |hsh, option|
|
@@ -12,7 +13,13 @@ class Slop
|
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
# Fetch an Option object
|
16
|
+
# Fetch an Option object. This method overrides Array#[] to provide
|
17
|
+
# a nicer interface for fetching options via their short or long flag.
|
18
|
+
# The reason we don't use a Hash here is because an option cannot be
|
19
|
+
# identified by a single label. Instead this method tests against
|
20
|
+
# a short flag first, followed by a long flag. When passing this
|
21
|
+
# method an Integer, it will work as an Array usually would, fetching
|
22
|
+
# the Slop::Option at this index.
|
16
23
|
#
|
17
24
|
# @param [Object] flag The short/long flag representing the option
|
18
25
|
# @example
|
@@ -21,12 +28,11 @@ class Slop
|
|
21
28
|
# opts.options[:v].description #=> "Verbose mode"
|
22
29
|
# @return [Option] the option assoiated with this flag
|
23
30
|
def [](flag)
|
24
|
-
if flag.is_a?
|
25
|
-
|
31
|
+
if flag.is_a? Integer
|
32
|
+
super
|
26
33
|
else
|
27
|
-
item = flag.to_s
|
28
34
|
find do |option|
|
29
|
-
option.short_flag
|
35
|
+
[option.short_flag, option.long_flag].include? flag.to_s
|
30
36
|
end
|
31
37
|
end
|
32
38
|
end
|
data/test/helper.rb
CHANGED
data/test/slop_test.rb
CHANGED
@@ -59,7 +59,10 @@ class SlopTest < TestCase
|
|
59
59
|
end
|
60
60
|
|
61
61
|
assert_equal 'foo', item1
|
62
|
-
|
62
|
+
|
63
|
+
temp_argv([]) do
|
64
|
+
assert_equal [], Slop.new { on_empty {} }.parse
|
65
|
+
end
|
63
66
|
end
|
64
67
|
|
65
68
|
test 'callback when arguments contain no options' do
|
@@ -356,7 +359,6 @@ class SlopTest < TestCase
|
|
356
359
|
end
|
357
360
|
|
358
361
|
test 'custom IO object' do
|
359
|
-
require 'stringio'
|
360
362
|
io = StringIO.new
|
361
363
|
slop = Slop.new(:help => true, :io => io)
|
362
364
|
slop.on :f, :foo, 'something fooey'
|
@@ -368,7 +370,6 @@ class SlopTest < TestCase
|
|
368
370
|
end
|
369
371
|
|
370
372
|
test 'exiting when using :help option' do
|
371
|
-
require 'stringio'
|
372
373
|
io = StringIO.new
|
373
374
|
opts = Slop.new(:help => true, :io => io)
|
374
375
|
assert_raises(SystemExit) { opts.parse %w/--help/ }
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 13
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 6
|
9
|
+
- 1
|
10
|
+
version: 1.6.1
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Lee Jarvis
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-
|
18
|
+
date: 2011-06-01 00:00:00 +01:00
|
14
19
|
default_executable:
|
15
20
|
dependencies: []
|
16
21
|
|
@@ -52,17 +57,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
57
|
requirements:
|
53
58
|
- - ">="
|
54
59
|
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
55
63
|
version: "0"
|
56
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
65
|
none: false
|
58
66
|
requirements:
|
59
67
|
- - ">="
|
60
68
|
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
61
72
|
version: "0"
|
62
73
|
requirements: []
|
63
74
|
|
64
75
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.6.
|
76
|
+
rubygems_version: 1.6.0
|
66
77
|
signing_key:
|
67
78
|
specification_version: 3
|
68
79
|
summary: Option gathering made easy
|