slop 1.6.1 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.md +7 -0
- data/README.md +11 -21
- data/lib/slop.rb +12 -1
- data/lib/slop/option.rb +23 -9
- data/slop.gemspec +1 -4
- data/test/helper.rb +0 -5
- data/test/slop_test.rb +23 -0
- metadata +3 -16
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -193,26 +193,8 @@ You can of course also parse lists into options. Here's how:
|
|
193
193
|
# ARGV is `--people lee,john,bill`
|
194
194
|
opts[:people] #=> ['lee', 'john', 'bill']
|
195
195
|
|
196
|
-
|
197
|
-
|
198
|
-
opts = Slop.parse do
|
199
|
-
opt :people, true, :as => Array, :delimiter => ':', :limit => 2)
|
200
|
-
end
|
201
|
-
|
202
|
-
# ARGV is `--people lee:injekt:bob`
|
203
|
-
opts[:people] #=> ["lee", "injekt:bob"]
|
204
|
-
|
205
|
-
Ranges
|
206
|
-
------
|
207
|
-
|
208
|
-
What would Slop be if it didn't know what ranges were?
|
209
|
-
|
210
|
-
opts = Slop.parse do
|
211
|
-
opt :r, :range, true, :as => Range
|
212
|
-
end
|
213
|
-
|
214
|
-
# ARGV is `--range 1..10` or 1-10, or 1,10 (yes Slop supports them all)
|
215
|
-
opts[:range].to_a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
196
|
+
Slop supports a few styles of list parsing. Check out
|
197
|
+
[this wiki page](https://github.com/injekt/slop/wiki/Lists) for more info.
|
216
198
|
|
217
199
|
Smart
|
218
200
|
-----
|
@@ -278,6 +260,14 @@ Here's how commands might look:
|
|
278
260
|
* `run.rb -v #=> version 1`
|
279
261
|
* `run.rb clean -v #=> Enabled verbose mode for clean`
|
280
262
|
|
263
|
+
Features
|
264
|
+
--------
|
265
|
+
|
266
|
+
Check out the following wiki pages for more features:
|
267
|
+
|
268
|
+
* [Ranges](https://github.com/injekt/slop/wiki/Ranges)
|
269
|
+
* [Auto Create](https://github.com/injekt/slop/wiki/Auto-Create)
|
270
|
+
|
281
271
|
Woah woah, why you hating on OptionParser?
|
282
272
|
------------------------------------------
|
283
273
|
|
@@ -314,4 +304,4 @@ thing in Slop:
|
|
314
304
|
on :a, :age, 'Your age', true, :as => :int
|
315
305
|
end
|
316
306
|
|
317
|
-
opts.to_hash(true) #=> { :name => 'lee', :age => 105 }
|
307
|
+
opts.to_hash(true) #=> { :name => 'lee', :age => 105 }
|
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.
|
19
|
+
VERSION = '1.7.0'
|
20
20
|
|
21
21
|
# Parses the items from a CLI format into a friendly object.
|
22
22
|
#
|
@@ -71,6 +71,8 @@ class Slop
|
|
71
71
|
# @option opts [Boolean] :ignore_case (false) Ignore options case
|
72
72
|
# @option opts [Proc, #call] :on_noopts Trigger an event when no options
|
73
73
|
# are found
|
74
|
+
# @option opts [Boolean] :autocreate (false) Autocreate options depending
|
75
|
+
# on the Array passed to {parse}
|
74
76
|
def initialize(*opts, &block)
|
75
77
|
sloptions = opts.last.is_a?(Hash) ? opts.pop : {}
|
76
78
|
sloptions[:banner] = opts.shift if opts[0].respond_to? :to_str
|
@@ -86,6 +88,7 @@ class Slop
|
|
86
88
|
@strict = sloptions[:strict]
|
87
89
|
@ignore_case = sloptions[:ignore_case]
|
88
90
|
@multiple_switches = sloptions[:multiple_switches]
|
91
|
+
@autocreate = sloptions[:autocreate]
|
89
92
|
@on_empty = sloptions[:on_empty]
|
90
93
|
@on_noopts = sloptions[:on_noopts] || sloptions[:on_optionless]
|
91
94
|
@sloptions = sloptions
|
@@ -327,6 +330,7 @@ class Slop
|
|
327
330
|
items.each_with_index do |item, index|
|
328
331
|
item = item.to_s
|
329
332
|
flag = item.sub(/\A--?/, '')
|
333
|
+
autocreate(flag, index, items) if @autocreate
|
330
334
|
option, argument = extract_option(item, flag)
|
331
335
|
next if @multiple_switches
|
332
336
|
|
@@ -439,6 +443,13 @@ class Slop
|
|
439
443
|
end
|
440
444
|
end
|
441
445
|
|
446
|
+
def autocreate(flag, index, items)
|
447
|
+
return if present? flag
|
448
|
+
short, long = clean_options Array(flag)
|
449
|
+
arg = (items[index + 1] && items[index + 1] !~ /\A--?/)
|
450
|
+
@options << Option.new(self, short, long, nil, arg, {})
|
451
|
+
end
|
452
|
+
|
442
453
|
def clean_options(args)
|
443
454
|
options = []
|
444
455
|
|
data/lib/slop/option.rb
CHANGED
@@ -22,12 +22,7 @@ class Slop
|
|
22
22
|
attr_reader :help
|
23
23
|
|
24
24
|
# @return [Boolean] true if this options argument value has been forced
|
25
|
-
|
26
|
-
|
27
|
-
# @overload argument_value=(value)
|
28
|
-
# Set this options argument value
|
29
|
-
# @param [Object] value The value you'd like applied to this option
|
30
|
-
attr_writer :argument_value
|
25
|
+
attr_accessor :forced
|
31
26
|
|
32
27
|
# @return [Integer] The amount of times this option has been invoked
|
33
28
|
attr_accessor :count
|
@@ -61,6 +56,7 @@ class Slop
|
|
61
56
|
@delimiter = options.fetch(:delimiter, ',')
|
62
57
|
@limit = options.fetch(:limit, 0)
|
63
58
|
@help = options.fetch(:help, true)
|
59
|
+
@argument_type = options[:as].to_s.downcase
|
64
60
|
|
65
61
|
@forced = false
|
66
62
|
@argument_value = nil
|
@@ -87,6 +83,24 @@ class Slop
|
|
87
83
|
@long_flag || @short_flag
|
88
84
|
end
|
89
85
|
|
86
|
+
# Set this options argument value.
|
87
|
+
#
|
88
|
+
# If this options argument type is expected to be an Array, this
|
89
|
+
# method will split the value and concat elements into the original
|
90
|
+
# argument value
|
91
|
+
#
|
92
|
+
# @param [Object] value The value to set this options argument to
|
93
|
+
def argument_value=(value)
|
94
|
+
if @argument_type == 'array'
|
95
|
+
@argument_value ||= []
|
96
|
+
if value.respond_to?(:to_str)
|
97
|
+
@argument_value.concat value.split(@delimiter, @limit)
|
98
|
+
end
|
99
|
+
else
|
100
|
+
@argument_value = value
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
90
104
|
# @return [Object] the argument value after it's been cast
|
91
105
|
# according to the `:as` option
|
92
106
|
def argument_value
|
@@ -94,8 +108,8 @@ class Slop
|
|
94
108
|
value = @argument_value || @options[:default]
|
95
109
|
return if value.nil?
|
96
110
|
|
97
|
-
case @
|
98
|
-
when 'array';
|
111
|
+
case @argument_type
|
112
|
+
when 'array'; @argument_value
|
99
113
|
when 'range'; value_to_range value
|
100
114
|
when 'float'; value.to_s.to_f
|
101
115
|
when 'string', 'str'; value.to_s
|
@@ -159,7 +173,7 @@ class Slop
|
|
159
173
|
# @return [String]
|
160
174
|
def inspect
|
161
175
|
"#<Slop::Option short_flag=#{@short_flag.inspect} " +
|
162
|
-
"long_flag=#{@long_flag.inspect} " +
|
176
|
+
"long_flag=#{@long_flag.inspect} argument=#{@argument.inspect} " +
|
163
177
|
"description=#{@description.inspect}>"
|
164
178
|
end
|
165
179
|
|
data/slop.gemspec
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
$:.push File.expand_path('../lib', __FILE__)
|
2
|
-
require 'slop'
|
3
|
-
|
4
1
|
Gem::Specification.new do |s|
|
5
2
|
s.name = 'slop'
|
6
|
-
s.version =
|
3
|
+
s.version = '1.7.0'
|
7
4
|
s.summary = 'Option gathering made easy'
|
8
5
|
s.description = 'A simple DSL for gathering options and parsing the command line'
|
9
6
|
s.author = 'Lee Jarvis'
|
data/test/helper.rb
CHANGED
data/test/slop_test.rb
CHANGED
@@ -384,4 +384,27 @@ class SlopTest < TestCase
|
|
384
384
|
opts.parse %w/--NAME lee/
|
385
385
|
assert_equal 'lee', opts[:name]
|
386
386
|
end
|
387
|
+
|
388
|
+
test 'autocreating options' do
|
389
|
+
opts = Slop.new(:autocreate => true) do |o|
|
390
|
+
o.on '--lorem', true
|
391
|
+
end
|
392
|
+
opts.parse %w/--hello --foo bar -a --lorem ipsum/
|
393
|
+
|
394
|
+
assert opts.hello?
|
395
|
+
assert opts.foo?
|
396
|
+
assert_equal 'bar', opts[:foo]
|
397
|
+
assert opts.a?
|
398
|
+
assert_equal 'ipsum', opts[:lorem]
|
399
|
+
end
|
400
|
+
|
401
|
+
test 'multiple elements for array option' do
|
402
|
+
opts = Slop.new do
|
403
|
+
on :a, true, :as => Array
|
404
|
+
end
|
405
|
+
opts.parse %w/-a foo -a bar baz -a etc/
|
406
|
+
|
407
|
+
assert_equal %w/foo bar etc/, opts[:a]
|
408
|
+
end
|
409
|
+
|
387
410
|
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 13
|
5
4
|
prerelease:
|
6
|
-
|
7
|
-
- 1
|
8
|
-
- 6
|
9
|
-
- 1
|
10
|
-
version: 1.6.1
|
5
|
+
version: 1.7.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Lee Jarvis
|
@@ -15,8 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-06-
|
19
|
-
default_executable:
|
13
|
+
date: 2011-06-06 00:00:00 Z
|
20
14
|
dependencies: []
|
21
15
|
|
22
16
|
description: A simple DSL for gathering options and parsing the command line
|
@@ -43,7 +37,6 @@ files:
|
|
43
37
|
- test/helper.rb
|
44
38
|
- test/option_test.rb
|
45
39
|
- test/slop_test.rb
|
46
|
-
has_rdoc: true
|
47
40
|
homepage: http://github.com/injekt/slop
|
48
41
|
licenses: []
|
49
42
|
|
@@ -57,23 +50,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
50
|
requirements:
|
58
51
|
- - ">="
|
59
52
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
|
-
segments:
|
62
|
-
- 0
|
63
53
|
version: "0"
|
64
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
55
|
none: false
|
66
56
|
requirements:
|
67
57
|
- - ">="
|
68
58
|
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
|
-
segments:
|
71
|
-
- 0
|
72
59
|
version: "0"
|
73
60
|
requirements: []
|
74
61
|
|
75
62
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.8.2
|
77
64
|
signing_key:
|
78
65
|
specification_version: 3
|
79
66
|
summary: Option gathering made easy
|