1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
require File.dirname(__FILE__) + '/helper'
class SlopTest < TestCase
def clean_options(*args)
Slop.new.send(:clean_options, args)
end
def temp_argv(items)
old_argv = ARGV.clone
ARGV.replace items
yield
ensure
ARGV.replace old_argv
end
test 'includes Enumerable' do
assert Slop.included_modules.include?(Enumerable)
end
test 'new accepts a hash or array of symbols' do
slop = Slop.new :strict, :multiple_switches => true
[ :@multiple_switches, :@strict ].each do |var|
assert slop.instance_variable_get var
end
end
test 'parse returns a Slop object' do
slop = Slop.parse([])
assert_kind_of Slop, slop
end
test 'enumerating options' do
slop = Slop.new
slop.opt(:f, :foo, 'foo')
slop.opt(:b, :bar, 'bar')
slop.each { |option| assert option }
end
test 'defaulting to ARGV' do
temp_argv(%w/--name lee/) do
assert_equal('lee', Slop.parse { on :name, true }[:name])
end
end
test 'callback when option array is empty' do
item1 = item2 = nil
Slop.new { on_empty { item1 = 'foo' } }.parse
assert_equal 'foo', item1
assert_nil item2
end
test 'multiple switches with the :multiple_switches flag' do
slop = Slop.new :multiple_switches => true, :strict => true
%w/a b c/.each { |f| slop.on f }
slop.on :z, true
slop.parse %w/-abc/
%w/a b c/.each do |flag|
assert slop[flag]
assert slop.send(flag + '?')
end
assert_raises(Slop::InvalidOptionError, /d/) { slop.parse %w/-abcd/ }
assert_raises(Slop::MissingArgumentError, /z/) { slop.parse %w/-abcz/ }
end
test 'passing a block' do
assert Slop.new {}
slop = nil
assert Slop.new {|s| slop = s }
assert_kind_of Slop, slop
end
test 'automatically adding the help option' do
slop = Slop.new
assert_empty slop.options
slop = Slop.new :help => true
refute_empty slop.options
assert_equal 'Print this help message', slop.options[:help].description
end
test 'yielding non-options when a block is passed to "parse"' do
opts = Slop.new do
on :name, true
end
opts.parse(%w/--name lee a/) do |v|
assert_equal 'a', v
end
end
test 'preserving order when yielding non-options' do
items = []
slop = Slop.new { on(:name, true) { |name| items << name } }
slop.parse(%w/foo --name bar baz/) { |value| items << value }
assert_equal %w/foo bar baz/, items
end
test 'setting the banner' do
slop = Slop.new
slop.banner = "foo bar"
assert_equal "foo bar", slop.banner
assert slop.to_s =~ /^foo bar/
slop.banner = nil
assert_equal "", slop.to_s
slop = Slop.new "foo bar"
assert_equal "foo bar", slop.banner
end
test 'storing long option lengths' do
slop = Slop.new
assert_equal 0, slop.longest_flag
slop.opt(:name)
assert_equal 4, slop.longest_flag
slop.opt(:username)
assert_equal 8, slop.longest_flag
end
test 'parse returning the list of arguments left after parsing' do
opts = Slop.new do
on :name, true
end
assert_equal %w/a/, opts.parse!(%w/--name lee a/)
assert_equal %w/--name lee a/, opts.parse(%w/--name lee a/)
end
test '#parse does not remove parsed items' do
items = %w/--foo/
Slop.new { |opt| opt.on :foo }.parse(items)
assert_equal %w/--foo/, items
end
test '#parse! removes parsed items' do
items = %w/--foo/
Slop.new { |opt| opt.on :foo }.parse!(items)
assert_empty items
end
test '#parse! removes parsed items prefixed with --no-' do
items = %w/--no-foo/
Slop.new { |opt| opt.on :foo }.parse!(items)
assert_empty items
end
test 'the shit out of clean_options' do
assert_equal(
['s', 'short', 'short option', false],
clean_options('-s', '--short', 'short option')
)
assert_equal(
[nil, 'long', 'long option only', true],
clean_options('--long', 'long option only', true)
)
assert_equal(
['S', 'symbol', 'symbolize', false],
clean_options(:S, :symbol, 'symbolize')
)
assert_equal(
['a', nil, 'alphabetical only', true],
clean_options('a', 'alphabetical only', true)
)
assert_equal( # for description-less options
[nil, 'optiononly', nil, false],
clean_options('--optiononly')
)
assert_equal(['c', nil, nil, true], clean_options(:c, true))
assert_equal(['c', nil, nil, false], clean_options(:c, false))
end
test '[] returns an options argument value or nil' do
slop = Slop.new
slop.opt :n, :name, true
slop.parse %w/--name lee/
assert_equal 'lee', slop[:name]
assert_equal 'lee', slop[:n]
end
test 'arguments ending ? test for option existance' do
slop = Slop.new
slop.opt :v, :verbose
slop.opt :d, :debug
slop.parse %w/--verbose/
assert slop[:verbose]
assert slop.verbose?
refute slop[:debug]
refute slop.debug?
end
test 'raises if an option expects an argument and none is given' do
slop = Slop.new
slop.opt :name, true
slop.opt :age, :optional => true
assert_raises(Slop::MissingArgumentError, /name/) { slop.parse %w/--name/ }
assert slop.parse %w/--name 'foo'/
end
test 'returning a hash of options' do
slop = Slop.new
slop.opt :name, true
slop.opt :version
slop.opt :V, :verbose, :default => false
slop.parse %w/--name lee --version/
assert_equal({'name' => 'lee', 'version' => true, 'verbose' => false}, slop.to_hash)
assert_equal({:name => 'lee', :version => true, :verbose => false}, slop.to_hash(true))
end
test 'iterating options' do
slop = Slop.new
slop.opt :a, :abc
slop.opt :f, :foo
assert_equal 2, slop.count
slop.each {|opt| assert_kind_of Slop::Option, opt }
end
test 'fetching options and option values' do
slop = Slop.new
slop.opt :foo, true
slop.parse %w/--foo bar/
assert_kind_of Slop::Option, slop.options[:foo]
assert_equal "bar", slop[:foo]
assert_equal "bar", slop['foo']
end
test 'printing help' do
slop = Slop.new
slop.banner = 'Usage: foo [options]'
slop.parse
assert slop.to_s =~ /^Usage: foo/
end
test 'passing argument values to blocks' do
name = nil
opts = Slop.new
opts.on :name, true, :callback => proc {|n| name = n}
opts.parse %w/--name lee/
assert_equal 'lee', name
end
test 'strict mode' do
strict = Slop.new :strict => true
totallynotstrict = Slop.new
assert_raises(Slop::InvalidOptionError, /--foo/) { strict.parse %w/--foo/ }
assert totallynotstrict.parse %w/--foo/
end
test 'strict mode parses options before raising Slop::InvalidOptionError' do
strict = Slop.new :strict => true
strict.opt :n, :name, true
assert_raises(Slop::InvalidOptionError, /--foo/) { strict.parse %w/--foo --name nelson/ }
assert_equal 'nelson', strict[:name]
end
test 'short option flag with no space between flag and argument' do
slop = Slop.new
slop.opt :p, :password, true
slop.opt :s, :shortpass, true
slop.parse %w/-p foo -sbar/
assert_equal 'foo', slop[:password]
assert_equal 'bar', slop[:shortpass]
end
test 'prefixing --no- onto options for a negative result' do
slop = Slop.new
slop.opt :d, :debug
slop.opt :v, :verbose, :default => true
slop.parse %w/--no-debug --no-verbose --no-nothing/
refute slop.verbose?
refute slop.debug?
refute slop[:verbose]
refute slop[:debug]
end
test 'option=value' do
slop = Slop.new
slop.opt :n, :name, true
slop.parse %w/--name=lee/
assert_equal 'lee', slop[:name]
assert slop.name?
end
test 'parsing options with options as arguments' do
slop = Slop.new { on :f, :foo, true }
assert_raises(Slop::MissingArgumentError) { slop.parse %w/-f --bar/ }
end
test 'commands' do
slop = Slop.new do
command :foo do on :f, :foo, 'foo option' end
command :bar do on :f, :foo; on :b, :bar, true end
end
slop.commands.each_value do |command|
assert_kind_of Slop, command
end
assert 'foo option', slop.commands[:foo].options[:foo].description
slop.parse %w/bar --bar baz/
assert 'baz', slop.commands[:bar][:bar]
assert_nil slop.commands['bar']
end
test 'repeating existing commands' do
slop = Slop.new
slop.command :foo
assert_raises(ArgumentError) { slop.command :foo }
end
test 'commands inheriting options' do
slop = Slop.new :strict do
command :foo do end
end
assert slop.commands[:foo].instance_variable_get(:@strict)
end
test 'commands setting options' do
slop = Slop.new :strict => false do
command :foo, :strict => true do end
end
assert slop.commands[:foo].instance_variable_get(:@strict)
end
test 'inception' do
slop = Slop.new do
command(:foo) { command(:bar) { command(:baz) { on :f, 'D:' } } }
end
desc = slop.commands[:foo].commands[:bar].commands[:baz].options[:f].description
assert_equal 'D:', desc
end
end
|