slop 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +1 -1
- data/README.md +33 -7
- data/lib/slop.rb +35 -44
- data/lib/slop/option.rb +11 -9
- data/lib/slop/version.rb +1 -1
- data/test/slop_test.rb +15 -0
- metadata +2 -2
data/.yardopts
CHANGED
data/README.md
CHANGED
@@ -98,6 +98,31 @@ Shortcut:
|
|
98
98
|
...
|
99
99
|
end
|
100
100
|
|
101
|
+
Parsing
|
102
|
+
-------
|
103
|
+
|
104
|
+
Slop's pretty good at parsing, let's take a look at what it'll extract for you
|
105
|
+
|
106
|
+
Slop.parse do
|
107
|
+
on 's', 'server', true
|
108
|
+
on 'p', 'port', true, :as => :integer
|
109
|
+
on 'username', true, :matches => /[^a-zA-Z]+$/
|
110
|
+
on 'password', true
|
111
|
+
end
|
112
|
+
|
113
|
+
Now throw some options at it:
|
114
|
+
|
115
|
+
-s ftp://foobar.com -p1234 --username=FooBar --password 'hello there'
|
116
|
+
|
117
|
+
Here's what we'll get back
|
118
|
+
|
119
|
+
{
|
120
|
+
:server=>"ftp://foobar.com",
|
121
|
+
:port=>1234,
|
122
|
+
:username=>"FooBar",
|
123
|
+
:password=>"hello there"
|
124
|
+
}
|
125
|
+
|
101
126
|
Callbacks
|
102
127
|
---------
|
103
128
|
|
@@ -217,7 +242,7 @@ You can also change both the split delimiter and limit
|
|
217
242
|
Strict Mode
|
218
243
|
-----------
|
219
244
|
|
220
|
-
Passing `strict => true` to `Slop.parse` causes it to raise a `Slop::InvalidOptionError`
|
245
|
+
Passing `strict => true` to `Slop.parse` causes it to raise a `Slop::InvalidOptionError`
|
221
246
|
when an invalid option is found (`false` by default):
|
222
247
|
|
223
248
|
Slop.new(:strict => true).parse(%w/--foo/)
|
@@ -230,12 +255,13 @@ and it handles multiple invalid options with a sprinkling of pluralization:
|
|
230
255
|
|
231
256
|
Significantly, however, Slop will still parse the valid options:
|
232
257
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
258
|
+
slop = Slop.new(:strict => true, :help => true) do
|
259
|
+
banner "Usage:\n\t./awesome_sauce [options]\n\nOptions:"
|
260
|
+
on :n, :name, 'Your name'
|
261
|
+
end
|
262
|
+
|
263
|
+
begin
|
264
|
+
slop.parse(%w/--foo --bar -z/)
|
239
265
|
rescue Slop::InvalidOptionError => e
|
240
266
|
puts "\n#{e.message}\n\n"
|
241
267
|
puts slop
|
data/lib/slop.rb
CHANGED
@@ -11,8 +11,6 @@ class Slop
|
|
11
11
|
# Parses the items from a CLI format into a friendly object.
|
12
12
|
#
|
13
13
|
# @param [Array] items Items to parse into options.
|
14
|
-
# @yield Specify available CLI arguments using Slop# methods such as Slop#banner and Slop#option
|
15
|
-
# @return [Slop] Returns an instance of Slop.
|
16
14
|
# @example Specifying three options to parse:
|
17
15
|
# opts = Slops.parse do
|
18
16
|
# on :v, :verbose, 'Enable verbose mode'
|
@@ -21,15 +19,13 @@ class Slop
|
|
21
19
|
# end
|
22
20
|
# -------
|
23
21
|
# program.rb --verbose -n 'Emily' -a 25
|
24
|
-
# @
|
25
|
-
# @see Slop#option
|
22
|
+
# @return [Slop] Returns an instance of Slop.
|
26
23
|
def self.parse(items=ARGV, options={}, &block)
|
27
24
|
initialize_and_parse(items, false, options, &block)
|
28
25
|
end
|
29
26
|
|
30
27
|
# Identical to {Slop.parse}, but removes parsed options from the original Array.
|
31
28
|
#
|
32
|
-
# @yield Specify available CLI arguments using Slop# methods such as Slop#banner and Slop#option
|
33
29
|
# @return [Slop] Returns an instance of Slop.
|
34
30
|
def self.parse!(items=ARGV, options={}, &block)
|
35
31
|
initialize_and_parse(items, true, options, &block)
|
@@ -67,12 +63,11 @@ class Slop
|
|
67
63
|
# Set or return banner text.
|
68
64
|
#
|
69
65
|
# @param [String] text Displayed banner text.
|
70
|
-
# @return [String] Returns current banner.
|
71
66
|
# @example
|
72
67
|
# opts = Slop.parse do
|
73
68
|
# banner "Usage - ruby foo.rb [arguments]"
|
74
69
|
# end
|
75
|
-
# @
|
70
|
+
# @return [String] Returns current banner.
|
76
71
|
def banner(text=nil)
|
77
72
|
@banner = text if text
|
78
73
|
@banner
|
@@ -101,14 +96,12 @@ class Slop
|
|
101
96
|
# Return the value of an option via the subscript operator.
|
102
97
|
#
|
103
98
|
# @param [Symbol] key Option symbol.
|
104
|
-
# @return [Object] Returns the object associated with that option.
|
105
99
|
# @example
|
106
|
-
# opts[:name]
|
107
|
-
#
|
108
|
-
# @see Slop#method_missing
|
100
|
+
# opts[:name] #=> "Emily"
|
101
|
+
# @return [Object] Returns the value associated with that option.
|
109
102
|
def [](key)
|
110
103
|
option = @options[key]
|
111
|
-
option
|
104
|
+
option.argument_value if option
|
112
105
|
end
|
113
106
|
|
114
107
|
# Specify an option with a short or long version, description and type.
|
@@ -122,14 +115,15 @@ class Slop
|
|
122
115
|
# @example
|
123
116
|
# opts = Slop.parse do
|
124
117
|
# on :n, :name, 'Your username', true # Required argument
|
125
|
-
# on :a, :age, 'Your age (optional)', :optional => true
|
126
|
-
# on :g, :gender, 'Your gender', :optional => false
|
127
|
-
# on :V, :verbose, 'Run in verbose mode', :default => true
|
128
|
-
# on :P, :people, 'Your friends', true, :as => Array
|
118
|
+
# on :a, :age, 'Your age (optional)', :optional => true
|
119
|
+
# on :g, :gender, 'Your gender', :optional => false
|
120
|
+
# on :V, :verbose, 'Run in verbose mode', :default => true
|
121
|
+
# on :P, :people, 'Your friends', true, :as => Array
|
129
122
|
# on :h, :help, 'Print this help screen' do
|
130
123
|
# puts help
|
131
|
-
# end
|
124
|
+
# end
|
132
125
|
# end
|
126
|
+
# @return [Slop::Option]
|
133
127
|
def option(*args, &block)
|
134
128
|
options = args.pop if args.last.is_a?(Hash)
|
135
129
|
options ||= {}
|
@@ -145,14 +139,12 @@ class Slop
|
|
145
139
|
|
146
140
|
# Returns the parsed list into a option/value hash.
|
147
141
|
#
|
148
|
-
# @return [Hash] Returns a hash with each specified option as a symbolic key with an associated value.
|
149
142
|
# @example
|
150
|
-
# opts.to_hash
|
151
|
-
# #=> { 'name' => 'Emily' }
|
143
|
+
# opts.to_hash #=> { 'name' => 'Emily' }
|
152
144
|
#
|
153
145
|
# # symbols!
|
154
|
-
# opts.to_hash(true)
|
155
|
-
#
|
146
|
+
# opts.to_hash(true) #=> { :name => 'Emily' }
|
147
|
+
# @return [Hash]
|
156
148
|
def to_hash(symbols=nil)
|
157
149
|
@options.to_hash(symbols)
|
158
150
|
end
|
@@ -160,29 +152,25 @@ class Slop
|
|
160
152
|
|
161
153
|
# Allows you to check whether an option was specified in the parsed list.
|
162
154
|
#
|
163
|
-
# @return [Boolean] Whether the desired option was specified.
|
164
155
|
# @example
|
165
156
|
# #== ruby foo.rb -v
|
166
|
-
# opts.verbose?
|
167
|
-
# #=>
|
168
|
-
#
|
169
|
-
# #=> false
|
170
|
-
# @see Slop#[]
|
157
|
+
# opts.verbose? #=> true
|
158
|
+
# opts.name? #=> false
|
159
|
+
# @return [Boolean] Whether the desired option was specified.
|
171
160
|
def method_missing(meth, *args, &block)
|
172
|
-
|
161
|
+
super unless meth.to_s =~ /\?\z/
|
162
|
+
!!self[meth.to_s.chomp '?']
|
173
163
|
end
|
174
164
|
|
175
165
|
# Returns the banner followed by available options listed on the next line.
|
176
166
|
#
|
177
|
-
# @return [String] Help text.
|
178
167
|
# @example
|
179
168
|
# opts = Slop.parse do
|
180
169
|
# banner "Usage - ruby foo.rb [arguments]"
|
181
170
|
# on :v, :verbose, "Enable verbose mode"
|
182
171
|
# end
|
183
|
-
# opts
|
184
|
-
#
|
185
|
-
# @see Slop#banner
|
172
|
+
# puts opts
|
173
|
+
# @return [String] Help text.
|
186
174
|
def to_s
|
187
175
|
banner = "#{@banner}\n" if @banner
|
188
176
|
(banner || '') + options.to_help
|
@@ -210,18 +198,22 @@ private
|
|
210
198
|
flag = item.sub(/^--?/, '')
|
211
199
|
option = @options[flag]
|
212
200
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
201
|
+
unless option
|
202
|
+
case item
|
203
|
+
when /\A-[^-]/
|
204
|
+
flag, argument = flag.split('', 2)
|
205
|
+
option = @options[flag]
|
206
|
+
when /\A--([^=]+)=(.+)\z/
|
207
|
+
option = @options[$1]
|
208
|
+
argument = $2
|
209
|
+
when /\A--no-(.+)\z/
|
210
|
+
option.force_argument_value(false) if option = @options[$1]
|
220
211
|
end
|
221
212
|
end
|
222
213
|
|
223
214
|
if option
|
224
215
|
trash << item
|
216
|
+
next if option.forced?
|
225
217
|
option.argument_value = true
|
226
218
|
|
227
219
|
if option.expects_argument? || option.accepts_optional_argument?
|
@@ -267,13 +259,13 @@ private
|
|
267
259
|
end
|
268
260
|
|
269
261
|
def check_invalid_option(item, flag)
|
270
|
-
@invalid_options << flag if item[
|
262
|
+
@invalid_options << flag if item[/\A--?/] && @strict
|
271
263
|
end
|
272
264
|
|
273
265
|
def clean_options(args)
|
274
266
|
options = []
|
275
267
|
|
276
|
-
short = args.first.to_s.sub(
|
268
|
+
short = args.first.to_s.sub(/\A--?/, '')
|
277
269
|
if short.size == 1
|
278
270
|
options.push short
|
279
271
|
args.shift
|
@@ -284,7 +276,7 @@ private
|
|
284
276
|
long = args.first
|
285
277
|
boolean = long.is_a?(TrueClass) || long.is_a?(FalseClass)
|
286
278
|
if !boolean && long.to_s =~ /\A(--?)?[a-zA-Z0-9_-]+\z/
|
287
|
-
options.push args.shift.to_s.sub(
|
279
|
+
options.push args.shift.to_s.sub(/\A--?/, '')
|
288
280
|
else
|
289
281
|
options.push nil
|
290
282
|
end
|
@@ -302,5 +294,4 @@ private
|
|
302
294
|
message << ' -- ' << @invalid_options.map { |o| "'#{o}'" }.join(', ')
|
303
295
|
raise InvalidOptionError, message
|
304
296
|
end
|
305
|
-
|
306
297
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -17,7 +17,7 @@ class Slop
|
|
17
17
|
# @return [Option] the option assoiated with this flag
|
18
18
|
def [](flag)
|
19
19
|
item = flag.to_s
|
20
|
-
if item =~
|
20
|
+
if item =~ /\A\d+\z/
|
21
21
|
slice item.to_i
|
22
22
|
else
|
23
23
|
find do |option|
|
@@ -28,7 +28,7 @@ class Slop
|
|
28
28
|
|
29
29
|
# @return [String]
|
30
30
|
def to_help
|
31
|
-
heads =
|
31
|
+
heads = reject {|x| x.tail }
|
32
32
|
tails = select {|x| x.tail }
|
33
33
|
(heads + tails).map(&:to_s).join("\n")
|
34
34
|
end
|
@@ -76,11 +76,15 @@ class Slop
|
|
76
76
|
@long_flag = long
|
77
77
|
@description = description
|
78
78
|
@options = options
|
79
|
+
|
79
80
|
@expects_argument = argument
|
80
81
|
@expects_argument = true if options[:optional] == false
|
82
|
+
|
81
83
|
@tail = options[:tail]
|
82
84
|
@match = options[:match]
|
85
|
+
|
83
86
|
@forced = false
|
87
|
+
@argument_value = nil
|
84
88
|
|
85
89
|
@delimiter = options[:delimiter] || ','
|
86
90
|
@limit = options[:limit] || 0
|
@@ -91,7 +95,6 @@ class Slop
|
|
91
95
|
|
92
96
|
@callback = blk if block_given?
|
93
97
|
@callback ||= options[:callback]
|
94
|
-
@argument_value = nil
|
95
98
|
end
|
96
99
|
|
97
100
|
# @return [Boolean] true if this option expects an argument
|
@@ -109,16 +112,11 @@ class Slop
|
|
109
112
|
@long_flag || @short_flag
|
110
113
|
end
|
111
114
|
|
112
|
-
# @return [Object]
|
113
|
-
def default
|
114
|
-
@options[:default]
|
115
|
-
end
|
116
|
-
|
117
115
|
# @return [Object] the argument value after it's been case
|
118
116
|
# according to the `:as` option
|
119
117
|
def argument_value
|
120
118
|
return @argument_value if @forced
|
121
|
-
value = @argument_value || default
|
119
|
+
value = @argument_value || @options[:default]
|
122
120
|
|
123
121
|
case @options[:as].to_s.downcase
|
124
122
|
when 'array'
|
@@ -141,6 +139,10 @@ class Slop
|
|
141
139
|
@forced = true
|
142
140
|
end
|
143
141
|
|
142
|
+
def forced?
|
143
|
+
@forced
|
144
|
+
end
|
145
|
+
|
144
146
|
def to_s
|
145
147
|
out = " "
|
146
148
|
out += @short_flag ? "-#{@short_flag}, " : ' ' * 4
|
data/lib/slop/version.rb
CHANGED
data/test/slop_test.rb
CHANGED
@@ -101,6 +101,12 @@ class SlopTest < TestCase
|
|
101
101
|
assert_empty items
|
102
102
|
end
|
103
103
|
|
104
|
+
test '#parse! removes parsed items prefixed with --no-' do
|
105
|
+
items = %w/--no-foo/
|
106
|
+
Slop.new { |opt| opt.on :foo }.parse!(items)
|
107
|
+
assert_empty items
|
108
|
+
end
|
109
|
+
|
104
110
|
test 'the shit out of clean_options' do
|
105
111
|
assert_equal(
|
106
112
|
['s', 'short', 'short option', false],
|
@@ -245,4 +251,13 @@ class SlopTest < TestCase
|
|
245
251
|
refute slop[:verbose]
|
246
252
|
refute slop[:debug]
|
247
253
|
end
|
254
|
+
|
255
|
+
test 'option=value' do
|
256
|
+
slop = Slop.new
|
257
|
+
slop.opt :n, :name, true
|
258
|
+
slop.parse %w/--name=lee/
|
259
|
+
|
260
|
+
assert_equal 'lee', slop[:name]
|
261
|
+
assert slop.name?
|
262
|
+
end
|
248
263
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: slop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Lee Jarvis
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-28 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|