slop 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGES.md +5 -0
  2. data/README.md +146 -108
  3. data/lib/slop.rb +5 -4
  4. data/slop.gemspec +1 -1
  5. data/test/option_test.rb +5 -0
  6. metadata +19 -38
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ 2.3.0
2
+ -----
3
+
4
+ * Allow flags to have suffixed `=` char for options which accept an argument
5
+
1
6
  2.2.0 (2011-11-02)
2
7
  ------------------
3
8
 
data/README.md CHANGED
@@ -18,20 +18,24 @@ Installation
18
18
 
19
19
  Usage
20
20
  -----
21
- # parse assumes ARGV, otherwise you can pass it your own Array
22
- opts = Slop.parse do
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
- end
28
-
29
- # if ARGV is `-v --name 'lee jarvis' -s male`
30
- opts.verbose? #=> true
31
- opts.name? #=> true
32
- opts[:name] #=> 'lee jarvis'
33
- opts.age? #=> false
34
- opts[:age] #=> nil
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
- opts.to_hash #=> {:name => 'Lee Jarvis', :verbose => true, :age => nil, :sex => 'male'}
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
- puts opts
48
- puts opts.help
53
+ ```ruby
54
+ puts opts
55
+ puts opts.help
56
+ ```
49
57
 
50
58
  Will output something like
51
59
 
52
- -v, --verbose Enable verbose mode
53
- -n, --name Your name
54
- -a, --age Your age
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
- opts = Slop.parse do
59
- banner "Usage: foo.rb [options]"
60
- end
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
- Slop.parse do
68
- ...
69
- on :h, :help, 'Print this help message', :tail => true do
70
- puts help
71
- exit
72
- end
73
- end
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
- Slop.parse :help => true do
91
+ ```ruby
92
+ Slop.parse :help => true do
78
93
  ...
79
- end
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
- Slop.parse(:multiple_switches => false) do
87
- on 's', 'server', true
88
- on 'p', 'port', true, :as => :integer
89
- on 'username', true, :matches => /[^a-zA-Z]+$/
90
- on 'password', true
91
- end
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
- -s ftp://foobar.com -p1234 --username=FooBar --password 'hello there'
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
- :server => "ftp://foobar.com",
101
- :port => 1234,
102
- :username => "FooBar",
103
- :password => "hello there"
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
- Slop.parse do
113
- on :V, :version, 'Print the version' do
114
- puts 'Version 1.0.0'
115
- exit
116
- end
117
- end
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
- opts = Slop.new do
131
- on :n, :name, :optional => false
132
- end
154
+ ```ruby
155
+ opts = Slop.new do
156
+ on :n, :name, :optional => false
157
+ end
133
158
 
134
- opts.parse do |arg|
135
- puts arg
136
- end
159
+ opts.parse do |arg|
160
+ puts arg
161
+ end
137
162
 
138
- # if ARGV is `foo --name Lee bar`
139
- foo
140
- bar
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
- opts = Slop.parse do
149
- on :v, :verbose, :default => true
150
- end
174
+ ```ruby
175
+ opts = Slop.parse do
176
+ on :v, :verbose, :default => true
177
+ end
151
178
 
152
- # with no command line options
153
- opts[:verbose] #=> true
179
+ # with no command line options
180
+ opts[:verbose] #=> true
154
181
 
155
- # with `--no-verbose`
156
- opts[:verbose] #=> false
157
- opts.verbose? #=> false
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
- Slop.parse do
169
- on :a, 'First switch'
170
- on :b, 'Second switch'
171
- on :c, 'Third switch'
172
- end
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
- # Using `-ac`
175
- opts[:a] #=> true
176
- opts[:b] #=> false
177
- opts[:c] #=> true
203
+ # Using `-ac`
204
+ opts[:a] #=> true
205
+ opts[:b] #=> false
206
+ opts[:c] #=> true
178
207
 
179
- Slop.parse(:multiple_switches => false) do
180
- on :a, 'Some switch', true
181
- end
208
+ Slop.parse(:multiple_switches => false) do
209
+ on :a, 'Some switch', true
210
+ end
182
211
 
183
- # Using `ahello`
184
- opts[:a] #=> 'hello'
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
- opts = Slop.parse do
192
- opt :people, true, :as => Array
193
- end
221
+ ```ruby
222
+ opts = Slop.parse do
223
+ opt :people, true, :as => Array
224
+ end
194
225
 
195
- # ARGV is `--people lee,john,bill`
196
- opts[:people] #=> ['lee', 'john', 'bill']
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
- Slop.new(:strict => true).parse(%w/--foo/)
208
- # => Slop::InvalidOptionError: Unknown option -- 'foo'
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
- require 'optparse'
260
+ ```ruby
261
+ require 'optparse'
227
262
 
228
- things = {}
263
+ things = {}
229
264
 
230
- opt = OptionParser.new do |opt|
231
- opt.on('-n', '--name NAME', 'Your name') do |name|
232
- things[:name] = name
233
- end
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
- opt.on('-a', '--age AGE', 'Your age') do |age|
236
- things[:age] = age.to_i
237
- end
270
+ opt.on('-a', '--age AGE', 'Your age') do |age|
271
+ things[:age] = age.to_i
272
+ end
238
273
 
239
- # you get the point
240
- end
274
+ # you get the point
275
+ end
241
276
 
242
- opt.parse
243
- things #=> { :name => 'lee', :age => 105 }
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
- require 'slop'
284
+ ```ruby
285
+ require 'slop'
249
286
 
250
- opts = Slop.parse do
251
- on :n, :name, 'Your name', true
252
- on :a, :age, 'Your age', true, :as => :int
253
- end
287
+ opts = Slop.parse do
288
+ on :n, :name=, 'Your name'
289
+ on :a, :age=, 'Your age', :as => :int
290
+ end
254
291
 
255
- opts.to_hash #=> { :name => 'lee', :age => 105 }
292
+ opts.to_hash #=> { :name => 'lee', :age => 105 }
293
+ ```
@@ -2,7 +2,7 @@ class Slop
2
2
  include Enumerable
3
3
 
4
4
  # @return [String] The current version string
5
- VERSION = '2.2.0'
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_-]+\z/
1012
- options.push args.shift.to_s.sub(/\A--?/, '')
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 ? true : (args.shift ? true : false)
1019
+ options.push (@arguments || extras[:argument]) ? true : (args.shift ? true : false)
1019
1020
  options.push extras
1020
1021
  end
1021
1022
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'slop'
3
- s.version = '2.2.0'
3
+ s.version = '2.3.0'
4
4
  s.summary = 'Option gathering made easy'
5
5
  s.description = 'A simple DSL for gathering options and parsing the command line'
6
6
  s.author = 'Lee Jarvis'
@@ -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
- hash: 7
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
- hash: 3
57
- segments:
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
- hash: 3
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.6
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