slop 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.md +5 -0
- data/README.md +146 -108
- data/lib/slop.rb +5 -4
- data/slop.gemspec +1 -1
- data/test/option_test.rb +5 -0
- metadata +19 -38
data/CHANGES.md
CHANGED
data/README.md
CHANGED
|
@@ -18,20 +18,24 @@ Installation
|
|
|
18
18
|
|
|
19
19
|
Usage
|
|
20
20
|
-----
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
# parse assumes ARGV, otherwise you can pass it your own Array
|
|
24
|
+
opts = Slop.parse do
|
|
25
|
+
on :v, :verbose, 'Enable verbose mode' # A boolean option
|
|
26
|
+
on :n, :name=, 'Your name' # This option requires an argument
|
|
27
|
+
on :s, :sex, 'Your sex', true # So does this one
|
|
28
|
+
on :a, :age, 'Your age', optional: true # This one accepts an optional argument
|
|
29
|
+
on '-D', '--debug', 'Enable debug' # The prefixed -'s are optional
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# if ARGV is `-v --name 'lee jarvis' -s male`
|
|
33
|
+
opts.verbose? #=> true
|
|
34
|
+
opts.name? #=> true
|
|
35
|
+
opts[:name] #=> 'lee jarvis'
|
|
36
|
+
opts.age? #=> false
|
|
37
|
+
opts[:age] #=> nil
|
|
38
|
+
```
|
|
35
39
|
|
|
36
40
|
For more information about creating options, see the
|
|
37
41
|
[Creating Options](https://github.com/injekt/slop/wiki/Creating-Options)
|
|
@@ -39,69 +43,87 @@ wiki page.
|
|
|
39
43
|
|
|
40
44
|
You can also return your options as a Hash
|
|
41
45
|
|
|
42
|
-
|
|
46
|
+
```ruby
|
|
47
|
+
opts.to_hash #=> { :name => 'Lee Jarvis', :verbose => true, :age => nil, :sex => 'male' }
|
|
48
|
+
```
|
|
43
49
|
|
|
44
50
|
If you want some pretty output for the user to see your options, you can just
|
|
45
51
|
send the Slop object to `puts` or use the `help` method.
|
|
46
52
|
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
```ruby
|
|
54
|
+
puts opts
|
|
55
|
+
puts opts.help
|
|
56
|
+
```
|
|
49
57
|
|
|
50
58
|
Will output something like
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
60
|
+
```
|
|
61
|
+
-v, --verbose Enable verbose mode
|
|
62
|
+
-n, --name Your name
|
|
63
|
+
-a, --age Your age
|
|
64
|
+
```
|
|
55
65
|
|
|
56
66
|
You can also add a banner using the `banner` method
|
|
57
67
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
```ruby
|
|
69
|
+
opts = Slop.parse do
|
|
70
|
+
banner "Usage: foo.rb [options]"
|
|
71
|
+
end
|
|
72
|
+
```
|
|
61
73
|
|
|
62
74
|
Helpful Help
|
|
63
75
|
------------
|
|
64
76
|
|
|
65
77
|
Long form:
|
|
66
78
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
```ruby
|
|
80
|
+
Slop.parse do
|
|
81
|
+
...
|
|
82
|
+
on :h, :help, 'Print this help message', :tail => true do
|
|
83
|
+
puts help
|
|
84
|
+
exit
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
```
|
|
74
88
|
|
|
75
89
|
Shortcut:
|
|
76
90
|
|
|
77
|
-
|
|
91
|
+
```ruby
|
|
92
|
+
Slop.parse :help => true do
|
|
78
93
|
...
|
|
79
|
-
|
|
94
|
+
end
|
|
95
|
+
```
|
|
80
96
|
|
|
81
97
|
Parsing
|
|
82
98
|
-------
|
|
83
99
|
|
|
84
100
|
Slop's pretty good at parsing, let's take a look at what it'll extract for you
|
|
85
101
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
102
|
+
```ruby
|
|
103
|
+
Slop.parse(:multiple_switches => false) do
|
|
104
|
+
on 's', 'server='
|
|
105
|
+
on 'p', 'port=', :as => :integer
|
|
106
|
+
on 'username=', :matches => /[^a-zA-Z]+$/
|
|
107
|
+
on 'password='
|
|
108
|
+
end
|
|
109
|
+
```
|
|
92
110
|
|
|
93
111
|
Now throw some options at it:
|
|
94
112
|
|
|
95
|
-
|
|
113
|
+
```
|
|
114
|
+
-s ftp://foobar.com -p1234 --username=FooBar --password 'hello there'
|
|
115
|
+
```
|
|
96
116
|
|
|
97
117
|
Here's what we'll get back
|
|
98
118
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
```
|
|
120
|
+
{
|
|
121
|
+
:server => "ftp://foobar.com",
|
|
122
|
+
:port => 1234,
|
|
123
|
+
:username => "FooBar",
|
|
124
|
+
:password => "hello there"
|
|
125
|
+
}
|
|
126
|
+
```
|
|
105
127
|
|
|
106
128
|
Events
|
|
107
129
|
------
|
|
@@ -109,12 +131,14 @@ Events
|
|
|
109
131
|
If you'd like to trigger an event when an option is used, you can pass a
|
|
110
132
|
block to your option. Here's how:
|
|
111
133
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
134
|
+
```ruby
|
|
135
|
+
Slop.parse do
|
|
136
|
+
on :V, :version, 'Print the version' do
|
|
137
|
+
puts 'Version 1.0.0'
|
|
138
|
+
exit
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
```
|
|
118
142
|
|
|
119
143
|
Now when using the `--version` option on the command line, the trigger will
|
|
120
144
|
be called and its contents executed.
|
|
@@ -127,17 +151,19 @@ they're found, just like
|
|
|
127
151
|
[OptionParser](http://rubydoc.info/stdlib/optparse/1.9.2/OptionParser:order)
|
|
128
152
|
does it.
|
|
129
153
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
154
|
+
```ruby
|
|
155
|
+
opts = Slop.new do
|
|
156
|
+
on :n, :name, :optional => false
|
|
157
|
+
end
|
|
133
158
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
159
|
+
opts.parse do |arg|
|
|
160
|
+
puts arg
|
|
161
|
+
end
|
|
137
162
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
163
|
+
# if ARGV is `foo --name Lee bar`
|
|
164
|
+
foo
|
|
165
|
+
bar
|
|
166
|
+
```
|
|
141
167
|
|
|
142
168
|
Negative Options
|
|
143
169
|
----------------
|
|
@@ -145,16 +171,18 @@ Negative Options
|
|
|
145
171
|
Slop also allows you to prefix `--no-` to an option which will force the option
|
|
146
172
|
to return a false value.
|
|
147
173
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
174
|
+
```ruby
|
|
175
|
+
opts = Slop.parse do
|
|
176
|
+
on :v, :verbose, :default => true
|
|
177
|
+
end
|
|
151
178
|
|
|
152
|
-
|
|
153
|
-
|
|
179
|
+
# with no command line options
|
|
180
|
+
opts[:verbose] #=> true
|
|
154
181
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
182
|
+
# with `--no-verbose`
|
|
183
|
+
opts[:verbose] #=> false
|
|
184
|
+
opts.verbose? #=> false
|
|
185
|
+
```
|
|
158
186
|
|
|
159
187
|
Short Switches
|
|
160
188
|
--------------
|
|
@@ -165,35 +193,39 @@ you would like to disable this, you can pass `multiple_switches => false` to
|
|
|
165
193
|
a new Slop object. In which case Slop will then parse `-fbar` as the option
|
|
166
194
|
`f` with the argument value `bar`.
|
|
167
195
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
196
|
+
```ruby
|
|
197
|
+
Slop.parse do
|
|
198
|
+
on :a, 'First switch'
|
|
199
|
+
on :b, 'Second switch'
|
|
200
|
+
on :c, 'Third switch'
|
|
201
|
+
end
|
|
173
202
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
203
|
+
# Using `-ac`
|
|
204
|
+
opts[:a] #=> true
|
|
205
|
+
opts[:b] #=> false
|
|
206
|
+
opts[:c] #=> true
|
|
178
207
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
208
|
+
Slop.parse(:multiple_switches => false) do
|
|
209
|
+
on :a, 'Some switch', true
|
|
210
|
+
end
|
|
182
211
|
|
|
183
|
-
|
|
184
|
-
|
|
212
|
+
# Using `ahello`
|
|
213
|
+
opts[:a] #=> 'hello'
|
|
214
|
+
```
|
|
185
215
|
|
|
186
216
|
Lists
|
|
187
217
|
-----
|
|
188
218
|
|
|
189
219
|
You can of course also parse lists into options. Here's how:
|
|
190
220
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
221
|
+
```ruby
|
|
222
|
+
opts = Slop.parse do
|
|
223
|
+
opt :people, true, :as => Array
|
|
224
|
+
end
|
|
194
225
|
|
|
195
|
-
|
|
196
|
-
|
|
226
|
+
# ARGV is `--people lee,john,bill`
|
|
227
|
+
opts[:people] #=> ['lee', 'john', 'bill']
|
|
228
|
+
```
|
|
197
229
|
|
|
198
230
|
Slop supports a few styles of list parsing. Check out
|
|
199
231
|
[this wiki page](https://github.com/injekt/slop/wiki/Lists) for more info.
|
|
@@ -204,8 +236,10 @@ Strict Mode
|
|
|
204
236
|
Passing `strict => true` to `Slop.parse` causes it to raise a `Slop::InvalidOptionError`
|
|
205
237
|
when an invalid option is found (`false` by default):
|
|
206
238
|
|
|
207
|
-
|
|
208
|
-
|
|
239
|
+
```ruby
|
|
240
|
+
Slop.new(:strict => true).parse(%w/--foo/)
|
|
241
|
+
# => Slop::InvalidOptionError: Unknown option -- 'foo'
|
|
242
|
+
```
|
|
209
243
|
|
|
210
244
|
Features
|
|
211
245
|
--------
|
|
@@ -223,33 +257,37 @@ I'm not, honestly! I love OptionParser. I really do, it's a fantastic library.
|
|
|
223
257
|
So why did I build Slop? Well, I find myself using OptionParser to simply
|
|
224
258
|
gather a bunch of key/value options, usually you would do something like this:
|
|
225
259
|
|
|
226
|
-
|
|
260
|
+
```ruby
|
|
261
|
+
require 'optparse'
|
|
227
262
|
|
|
228
|
-
|
|
263
|
+
things = {}
|
|
229
264
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
265
|
+
opt = OptionParser.new do |opt|
|
|
266
|
+
opt.on('-n', '--name NAME', 'Your name') do |name|
|
|
267
|
+
things[:name] = name
|
|
268
|
+
end
|
|
234
269
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
270
|
+
opt.on('-a', '--age AGE', 'Your age') do |age|
|
|
271
|
+
things[:age] = age.to_i
|
|
272
|
+
end
|
|
238
273
|
|
|
239
|
-
|
|
240
|
-
|
|
274
|
+
# you get the point
|
|
275
|
+
end
|
|
241
276
|
|
|
242
|
-
|
|
243
|
-
|
|
277
|
+
opt.parse
|
|
278
|
+
things #=> { :name => 'lee', :age => 105 }
|
|
279
|
+
```
|
|
244
280
|
|
|
245
281
|
Which is all great and stuff, but it can lead to some repetition. The same
|
|
246
282
|
thing in Slop:
|
|
247
283
|
|
|
248
|
-
|
|
284
|
+
```ruby
|
|
285
|
+
require 'slop'
|
|
249
286
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
287
|
+
opts = Slop.parse do
|
|
288
|
+
on :n, :name=, 'Your name'
|
|
289
|
+
on :a, :age=, 'Your age', :as => :int
|
|
290
|
+
end
|
|
254
291
|
|
|
255
|
-
|
|
292
|
+
opts.to_hash #=> { :name => 'lee', :age => 105 }
|
|
293
|
+
```
|
data/lib/slop.rb
CHANGED
|
@@ -2,7 +2,7 @@ class Slop
|
|
|
2
2
|
include Enumerable
|
|
3
3
|
|
|
4
4
|
# @return [String] The current version string
|
|
5
|
-
VERSION = '2.
|
|
5
|
+
VERSION = '2.3.0'
|
|
6
6
|
|
|
7
7
|
# Slops standard Error class. All exception classes should
|
|
8
8
|
# inherit from this class
|
|
@@ -1008,14 +1008,15 @@ class Slop
|
|
|
1008
1008
|
options.push arg.sub(/\A--?/, '')
|
|
1009
1009
|
extras[:optional] = help[0, 1] == '[' && help[-1, 1] == ']'
|
|
1010
1010
|
extras[:help] = help
|
|
1011
|
-
elsif not boolean and long.to_s =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]
|
|
1012
|
-
|
|
1011
|
+
elsif not boolean and long.to_s =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]+\=?\z/
|
|
1012
|
+
extras[:argument] = true if long[-1, 1] == '='
|
|
1013
|
+
options.push args.shift.to_s.sub(/\A--?/, '').sub(/\=\z/, '')
|
|
1013
1014
|
else
|
|
1014
1015
|
options.push nil
|
|
1015
1016
|
end
|
|
1016
1017
|
|
|
1017
1018
|
options.push args.first.respond_to?(:to_sym) ? args.shift : nil
|
|
1018
|
-
options.push @arguments ?
|
|
1019
|
+
options.push (@arguments || extras[:argument]) ? true : (args.shift ? true : false)
|
|
1019
1020
|
options.push extras
|
|
1020
1021
|
end
|
|
1021
1022
|
end
|
data/slop.gemspec
CHANGED
data/test/option_test.rb
CHANGED
|
@@ -25,6 +25,11 @@ class OptionTest < TestCase
|
|
|
25
25
|
refute option(:f, :foo).expects_argument?
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
test 'expects an argument if long option is suffixed with =' do
|
|
29
|
+
assert option(:f, :foo=).expects_argument?
|
|
30
|
+
assert option('f', 'foo=').expects_argument?
|
|
31
|
+
end
|
|
32
|
+
|
|
28
33
|
test 'accepts an optional argument if optional is true' do
|
|
29
34
|
assert option(:f, :optional => true).accepts_optional_argument?
|
|
30
35
|
assert option(:f, false, :optional => true).accepts_optional_argument?
|
metadata
CHANGED
|
@@ -1,32 +1,22 @@
|
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: slop
|
|
3
|
-
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 2.3.0
|
|
5
5
|
prerelease:
|
|
6
|
-
segments:
|
|
7
|
-
- 2
|
|
8
|
-
- 2
|
|
9
|
-
- 0
|
|
10
|
-
version: 2.2.0
|
|
11
6
|
platform: ruby
|
|
12
|
-
authors:
|
|
7
|
+
authors:
|
|
13
8
|
- Lee Jarvis
|
|
14
9
|
autorequire:
|
|
15
10
|
bindir: bin
|
|
16
11
|
cert_chain: []
|
|
17
|
-
|
|
18
|
-
date: 2011-11-02 00:00:00 Z
|
|
12
|
+
date: 2011-11-05 00:00:00.000000000Z
|
|
19
13
|
dependencies: []
|
|
20
|
-
|
|
21
14
|
description: A simple DSL for gathering options and parsing the command line
|
|
22
15
|
email: lee@jarvis.co
|
|
23
16
|
executables: []
|
|
24
|
-
|
|
25
17
|
extensions: []
|
|
26
|
-
|
|
27
18
|
extra_rdoc_files: []
|
|
28
|
-
|
|
29
|
-
files:
|
|
19
|
+
files:
|
|
30
20
|
- .gemtest
|
|
31
21
|
- .gitignore
|
|
32
22
|
- .yardopts
|
|
@@ -42,38 +32,29 @@ files:
|
|
|
42
32
|
- test/slop_test.rb
|
|
43
33
|
homepage: http://github.com/injekt/slop
|
|
44
34
|
licenses: []
|
|
45
|
-
|
|
46
35
|
post_install_message:
|
|
47
36
|
rdoc_options: []
|
|
48
|
-
|
|
49
|
-
require_paths:
|
|
37
|
+
require_paths:
|
|
50
38
|
- lib
|
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
40
|
none: false
|
|
53
|
-
requirements:
|
|
54
|
-
- -
|
|
55
|
-
- !ruby/object:Gem::Version
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
- 0
|
|
59
|
-
version: "0"
|
|
60
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ! '>='
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
46
|
none: false
|
|
62
|
-
requirements:
|
|
63
|
-
- -
|
|
64
|
-
- !ruby/object:Gem::Version
|
|
65
|
-
|
|
66
|
-
segments:
|
|
67
|
-
- 0
|
|
68
|
-
version: "0"
|
|
47
|
+
requirements:
|
|
48
|
+
- - ! '>='
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
69
51
|
requirements: []
|
|
70
|
-
|
|
71
52
|
rubyforge_project:
|
|
72
|
-
rubygems_version: 1.8.
|
|
53
|
+
rubygems_version: 1.8.11
|
|
73
54
|
signing_key:
|
|
74
55
|
specification_version: 3
|
|
75
56
|
summary: Option gathering made easy
|
|
76
|
-
test_files:
|
|
57
|
+
test_files:
|
|
77
58
|
- test/commands_test.rb
|
|
78
59
|
- test/helper.rb
|
|
79
60
|
- test/option_test.rb
|