google 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1094 @@
1
+ ## test/test_trollop.rb -- unit tests for trollop
2
+ ## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
3
+ ## Copyright:: Copyright 2007 William Morgan
4
+ ## License:: GNU GPL version 2
5
+
6
+ require 'test/unit'
7
+ require 'stringio'
8
+ require 'trollop'
9
+
10
+ module Trollop
11
+ module Test
12
+
13
+ class Trollop < ::Test::Unit::TestCase
14
+ def setup
15
+ @p = Parser.new
16
+ end
17
+
18
+ def test_unknown_arguments
19
+ assert_raise(CommandlineError) { @p.parse(%w(--arg)) }
20
+ @p.opt "arg"
21
+ assert_nothing_raised { @p.parse(%w(--arg)) }
22
+ assert_raise(CommandlineError) { @p.parse(%w(--arg2)) }
23
+ end
24
+
25
+ def test_syntax_check
26
+ @p.opt "arg"
27
+
28
+ assert_nothing_raised { @p.parse(%w(--arg)) }
29
+ assert_nothing_raised { @p.parse(%w(arg)) }
30
+ assert_raise(CommandlineError) { @p.parse(%w(---arg)) }
31
+ assert_raise(CommandlineError) { @p.parse(%w(-arg)) }
32
+ end
33
+
34
+ def test_required_flags_are_required
35
+ @p.opt "arg", "desc", :required => true
36
+ @p.opt "arg2", "desc", :required => false
37
+ @p.opt "arg3", "desc", :required => false
38
+
39
+ assert_nothing_raised { @p.parse(%w(--arg)) }
40
+ assert_nothing_raised { @p.parse(%w(--arg --arg2)) }
41
+ assert_raise(CommandlineError) { @p.parse(%w(--arg2)) }
42
+ assert_raise(CommandlineError) { @p.parse(%w(--arg2 --arg3)) }
43
+ end
44
+
45
+ ## flags that take an argument error unless given one
46
+ def test_argflags_demand_args
47
+ @p.opt "goodarg", "desc", :type => String
48
+ @p.opt "goodarg2", "desc", :type => String
49
+
50
+ assert_nothing_raised { @p.parse(%w(--goodarg goat)) }
51
+ assert_raise(CommandlineError) { @p.parse(%w(--goodarg --goodarg2 goat)) }
52
+ assert_raise(CommandlineError) { @p.parse(%w(--goodarg)) }
53
+ end
54
+
55
+ ## flags that don't take arguments ignore them
56
+ def test_arglessflags_refuse_args
57
+ @p.opt "goodarg"
58
+ @p.opt "goodarg2"
59
+ assert_nothing_raised { @p.parse(%w(--goodarg)) }
60
+ assert_nothing_raised { @p.parse(%w(--goodarg --goodarg2)) }
61
+ opts = @p.parse %w(--goodarg a)
62
+ assert_equal true, opts["goodarg"]
63
+ assert_equal ["a"], @p.leftovers
64
+ end
65
+
66
+ ## flags that require args of a specific type refuse args of other
67
+ ## types
68
+ def test_typed_args_refuse_args_of_other_types
69
+ assert_nothing_raised { @p.opt "goodarg", "desc", :type => :int }
70
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :type => :asdf }
71
+
72
+ assert_nothing_raised { @p.parse(%w(--goodarg 3)) }
73
+ assert_raise(CommandlineError) { @p.parse(%w(--goodarg 4.2)) }
74
+ assert_raise(CommandlineError) { @p.parse(%w(--goodarg hello)) }
75
+ end
76
+
77
+ ## type is correctly derived from :default
78
+ def test_type_correctly_derived_from_default
79
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :default => [] }
80
+
81
+ opts = nil
82
+
83
+ # single arg: int
84
+ assert_nothing_raised { @p.opt "argsi", "desc", :default => 0 }
85
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
86
+ assert_equal 0, opts["argsi"]
87
+ assert_nothing_raised { opts = @p.parse(%w(--argsi 4)) }
88
+ assert_equal 4, opts["argsi"]
89
+ assert_raise(CommandlineError) { @p.parse(%w(--argsi 4.2)) }
90
+ assert_raise(CommandlineError) { @p.parse(%w(--argsi hello)) }
91
+
92
+ # single arg: float
93
+ assert_nothing_raised { @p.opt "argsf", "desc", :default => 3.14 }
94
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
95
+ assert_equal 3.14, opts["argsf"]
96
+ assert_nothing_raised { opts = @p.parse(%w(--argsf 2.41)) }
97
+ assert_equal 2.41, opts["argsf"]
98
+ assert_nothing_raised { opts = @p.parse(%w(--argsf 2)) }
99
+ assert_equal 2, opts["argsf"]
100
+ assert_nothing_raised { opts = @p.parse(%w(--argsf 1.0e-2)) }
101
+ assert_equal 1.0e-2, opts["argsf"]
102
+ assert_raise(CommandlineError) { @p.parse(%w(--argsf hello)) }
103
+
104
+ # single arg: date
105
+ date = Date.today
106
+ assert_nothing_raised { @p.opt "argsd", "desc", :default => date }
107
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
108
+ assert_equal Date.today, opts["argsd"]
109
+ assert_nothing_raised { opts = @p.parse(['--argsd', 'Jan 4, 2007']) }
110
+ assert_equal Date.civil(2007, 1, 4), opts["argsd"]
111
+ assert_raise(CommandlineError) { @p.parse(%w(--argsd hello)) }
112
+
113
+ # single arg: string
114
+ assert_nothing_raised { @p.opt "argss", "desc", :default => "foobar" }
115
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
116
+ assert_equal "foobar", opts["argss"]
117
+ assert_nothing_raised { opts = @p.parse(%w(--argss 2.41)) }
118
+ assert_equal "2.41", opts["argss"]
119
+ assert_nothing_raised { opts = @p.parse(%w(--argss hello)) }
120
+ assert_equal "hello", opts["argss"]
121
+
122
+ # multi args: ints
123
+ assert_nothing_raised { @p.opt "argmi", "desc", :default => [3, 5] }
124
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
125
+ assert_equal [3, 5], opts["argmi"]
126
+ assert_nothing_raised { opts = @p.parse(%w(--argmi 4)) }
127
+ assert_equal [4], opts["argmi"]
128
+ assert_raise(CommandlineError) { @p.parse(%w(--argmi 4.2)) }
129
+ assert_raise(CommandlineError) { @p.parse(%w(--argmi hello)) }
130
+
131
+ # multi args: floats
132
+ assert_nothing_raised { @p.opt "argmf", "desc", :default => [3.34, 5.21] }
133
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
134
+ assert_equal [3.34, 5.21], opts["argmf"]
135
+ assert_nothing_raised { opts = @p.parse(%w(--argmf 2)) }
136
+ assert_equal [2], opts["argmf"]
137
+ assert_nothing_raised { opts = @p.parse(%w(--argmf 4.0)) }
138
+ assert_equal [4.0], opts["argmf"]
139
+ assert_raise(CommandlineError) { @p.parse(%w(--argmf hello)) }
140
+
141
+ # multi args: dates
142
+ dates = [Date.today, Date.civil(2007, 1, 4)]
143
+ assert_nothing_raised { @p.opt "argmd", "desc", :default => dates }
144
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
145
+ assert_equal dates, opts["argmd"]
146
+ assert_nothing_raised { opts = @p.parse(['--argmd', 'Jan 4, 2007']) }
147
+ assert_equal [Date.civil(2007, 1, 4)], opts["argmd"]
148
+ assert_raise(CommandlineError) { @p.parse(%w(--argmd hello)) }
149
+
150
+ # multi args: strings
151
+ assert_nothing_raised { @p.opt "argmst", "desc", :default => %w(hello world) }
152
+ assert_nothing_raised { opts = @p.parse(%w(--)) }
153
+ assert_equal %w(hello world), opts["argmst"]
154
+ assert_nothing_raised { opts = @p.parse(%w(--argmst 3.4)) }
155
+ assert_equal ["3.4"], opts["argmst"]
156
+ assert_nothing_raised { opts = @p.parse(%w(--argmst goodbye)) }
157
+ assert_equal ["goodbye"], opts["argmst"]
158
+ end
159
+
160
+ ## :type and :default must match if both are specified
161
+ def test_type_and_default_must_match
162
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :type => :int, :default => "hello" }
163
+ assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => 4 }
164
+ assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => ["hi"] }
165
+ assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :ints, :default => [3.14] }
166
+
167
+ assert_nothing_raised { @p.opt "argsi", "desc", :type => :int, :default => 4 }
168
+ assert_nothing_raised { @p.opt "argsf", "desc", :type => :float, :default => 3.14 }
169
+ assert_nothing_raised { @p.opt "argsd", "desc", :type => :date, :default => Date.today }
170
+ assert_nothing_raised { @p.opt "argss", "desc", :type => :string, :default => "yo" }
171
+ assert_nothing_raised { @p.opt "argmi", "desc", :type => :ints, :default => [4] }
172
+ assert_nothing_raised { @p.opt "argmf", "desc", :type => :floats, :default => [3.14] }
173
+ assert_nothing_raised { @p.opt "argmd", "desc", :type => :dates, :default => [Date.today] }
174
+ assert_nothing_raised { @p.opt "argmst", "desc", :type => :strings, :default => ["yo"] }
175
+ end
176
+
177
+ def test_long_detects_bad_names
178
+ assert_nothing_raised { @p.opt "goodarg", "desc", :long => "none" }
179
+ assert_nothing_raised { @p.opt "goodarg2", "desc", :long => "--two" }
180
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :long => "" }
181
+ assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :long => "--" }
182
+ assert_raise(ArgumentError) { @p.opt "badarg3", "desc", :long => "-one" }
183
+ assert_raise(ArgumentError) { @p.opt "badarg4", "desc", :long => "---toomany" }
184
+ end
185
+
186
+ def test_short_detects_bad_names
187
+ assert_nothing_raised { @p.opt "goodarg", "desc", :short => "a" }
188
+ assert_nothing_raised { @p.opt "goodarg2", "desc", :short => "-b" }
189
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :short => "" }
190
+ assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :short => "-ab" }
191
+ assert_raise(ArgumentError) { @p.opt "badarg3", "desc", :short => "--t" }
192
+ end
193
+
194
+ def test_short_names_created_automatically
195
+ @p.opt "arg"
196
+ @p.opt "arg2"
197
+ @p.opt "arg3"
198
+ opts = @p.parse %w(-a -g)
199
+ assert_equal true, opts["arg"]
200
+ assert_equal false, opts["arg2"]
201
+ assert_equal true, opts["arg3"]
202
+ end
203
+
204
+ def test_short_autocreation_skips_dashes_and_numbers
205
+ @p.opt :arg # auto: a
206
+ @p.opt :arg_potato # auto: r
207
+ @p.opt :arg_muffin # auto: g
208
+ assert_nothing_raised { @p.opt :arg_daisy } # auto: d (not _)!
209
+ assert_nothing_raised { @p.opt :arg_r2d2f } # auto: f (not 2)!
210
+
211
+ opts = @p.parse %w(-f -d)
212
+ assert_equal true, opts[:arg_daisy]
213
+ assert_equal true, opts[:arg_r2d2f]
214
+ assert_equal false, opts[:arg]
215
+ assert_equal false, opts[:arg_potato]
216
+ assert_equal false, opts[:arg_muffin]
217
+ end
218
+
219
+ def test_short_autocreation_is_ok_with_running_out_of_chars
220
+ @p.opt :arg1 # auto: a
221
+ @p.opt :arg2 # auto: r
222
+ @p.opt :arg3 # auto: g
223
+ @p.opt :arg4 # auto: uh oh!
224
+ assert_nothing_raised { @p.parse [] }
225
+ end
226
+
227
+ def test_short_can_be_nothing
228
+ assert_nothing_raised do
229
+ @p.opt "arg", "desc", :short => :none
230
+ @p.parse []
231
+ end
232
+
233
+ sio = StringIO.new "w"
234
+ @p.educate sio
235
+ assert sio.string =~ /--arg:\s+desc/
236
+
237
+ assert_raise(CommandlineError) { @p.parse %w(-a) }
238
+ end
239
+
240
+ ## two args can't have the same name
241
+ def test_conflicting_names_are_detected
242
+ assert_nothing_raised { @p.opt "goodarg" }
243
+ assert_raise(ArgumentError) { @p.opt "goodarg" }
244
+ end
245
+
246
+ ## two args can't have the same :long
247
+ def test_conflicting_longs_detected
248
+ assert_nothing_raised { @p.opt "goodarg", "desc", :long => "--goodarg" }
249
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :long => "--goodarg" }
250
+ end
251
+
252
+ ## two args can't have the same :short
253
+ def test_conflicting_shorts_detected
254
+ assert_nothing_raised { @p.opt "goodarg", "desc", :short => "-g" }
255
+ assert_raise(ArgumentError) { @p.opt "badarg", "desc", :short => "-g" }
256
+ end
257
+
258
+ def test_flag_defaults
259
+ @p.opt "defaultfalse", "desc"
260
+ @p.opt "defaulttrue", "desc", :default => true
261
+ opts = @p.parse []
262
+ assert_equal false, opts["defaultfalse"]
263
+ assert_equal true, opts["defaulttrue"]
264
+
265
+ opts = @p.parse %w(--defaultfalse --defaulttrue)
266
+ assert_equal true, opts["defaultfalse"]
267
+ assert_equal false, opts["defaulttrue"]
268
+ end
269
+
270
+ def test_special_flags_work
271
+ @p.version "asdf fdas"
272
+ assert_raise(VersionNeeded) { @p.parse(%w(-v)) }
273
+ assert_raise(HelpNeeded) { @p.parse(%w(-h)) }
274
+ end
275
+
276
+ def test_short_options_combine
277
+ @p.opt :arg1, "desc", :short => "a"
278
+ @p.opt :arg2, "desc", :short => "b"
279
+ @p.opt :arg3, "desc", :short => "c", :type => :int
280
+
281
+ opts = nil
282
+ assert_nothing_raised { opts = @p.parse %w(-a -b) }
283
+ assert_equal true, opts[:arg1]
284
+ assert_equal true, opts[:arg2]
285
+ assert_equal nil, opts[:arg3]
286
+
287
+ assert_nothing_raised { opts = @p.parse %w(-ab) }
288
+ assert_equal true, opts[:arg1]
289
+ assert_equal true, opts[:arg2]
290
+ assert_equal nil, opts[:arg3]
291
+
292
+ assert_nothing_raised { opts = @p.parse %w(-ac 4 -b) }
293
+ assert_equal true, opts[:arg1]
294
+ assert_equal true, opts[:arg2]
295
+ assert_equal 4, opts[:arg3]
296
+
297
+ assert_raises(CommandlineError) { @p.parse %w(-cab 4) }
298
+ assert_raises(CommandlineError) { @p.parse %w(-cba 4) }
299
+ end
300
+
301
+ def test_version_only_appears_if_set
302
+ @p.opt "arg"
303
+ assert_raise(CommandlineError) { @p.parse %w(-v) }
304
+ @p.version "trollop 1.2.3.4"
305
+ assert_raise(VersionNeeded) { @p.parse %w(-v) }
306
+ end
307
+
308
+ def test_doubledash_ends_option_processing
309
+ @p.opt :arg1, "desc", :short => "a", :default => 0
310
+ @p.opt :arg2, "desc", :short => "b", :default => 0
311
+ opts = nil
312
+ assert_nothing_raised { opts = @p.parse %w(-- -a 3 -b 2) }
313
+ assert_equal opts[:arg1], 0
314
+ assert_equal opts[:arg2], 0
315
+ assert_equal %w(-a 3 -b 2), @p.leftovers
316
+ assert_nothing_raised { opts = @p.parse %w(-a 3 -- -b 2) }
317
+ assert_equal opts[:arg1], 3
318
+ assert_equal opts[:arg2], 0
319
+ assert_equal %w(-b 2), @p.leftovers
320
+ assert_nothing_raised { opts = @p.parse %w(-a 3 -b 2 --) }
321
+ assert_equal opts[:arg1], 3
322
+ assert_equal opts[:arg2], 2
323
+ assert_equal %w(), @p.leftovers
324
+ end
325
+
326
+ def test_wrap
327
+ assert_equal [""], @p.wrap("")
328
+ assert_equal ["a"], @p.wrap("a")
329
+ assert_equal ["one two", "three"], @p.wrap("one two three", :width => 8)
330
+ assert_equal ["one two three"], @p.wrap("one two three", :width => 80)
331
+ assert_equal ["one", "two", "three"], @p.wrap("one two three", :width => 3)
332
+ assert_equal ["onetwothree"], @p.wrap("onetwothree", :width => 3)
333
+ assert_equal [
334
+ "Test is an awesome program that does something very, very important.",
335
+ "",
336
+ "Usage:",
337
+ " test [options] <filenames>+",
338
+ "where [options] are:"], @p.wrap(<<EOM, :width => 100)
339
+ Test is an awesome program that does something very, very important.
340
+
341
+ Usage:
342
+ test [options] <filenames>+
343
+ where [options] are:
344
+ EOM
345
+ end
346
+
347
+ def test_floating_point_formatting
348
+ @p.opt :arg, "desc", :type => :float, :short => "f"
349
+ opts = nil
350
+ assert_nothing_raised { opts = @p.parse %w(-f 1) }
351
+ assert_equal 1.0, opts[:arg]
352
+ assert_nothing_raised { opts = @p.parse %w(-f 1.0) }
353
+ assert_equal 1.0, opts[:arg]
354
+ assert_nothing_raised { opts = @p.parse %w(-f 0.1) }
355
+ assert_equal 0.1, opts[:arg]
356
+ assert_nothing_raised { opts = @p.parse %w(-f .1) }
357
+ assert_equal 0.1, opts[:arg]
358
+ assert_nothing_raised { opts = @p.parse %w(-f .99999999999999999999) }
359
+ assert_equal 1.0, opts[:arg]
360
+ assert_nothing_raised { opts = @p.parse %w(-f -1) }
361
+ assert_equal(-1.0, opts[:arg])
362
+ assert_nothing_raised { opts = @p.parse %w(-f -1.0) }
363
+ assert_equal(-1.0, opts[:arg])
364
+ assert_nothing_raised { opts = @p.parse %w(-f -0.1) }
365
+ assert_equal(-0.1, opts[:arg])
366
+ assert_nothing_raised { opts = @p.parse %w(-f -.1) }
367
+ assert_equal(-0.1, opts[:arg])
368
+ assert_raises(CommandlineError) { @p.parse %w(-f a) }
369
+ assert_raises(CommandlineError) { @p.parse %w(-f 1a) }
370
+ assert_raises(CommandlineError) { @p.parse %w(-f 1.a) }
371
+ assert_raises(CommandlineError) { @p.parse %w(-f a.1) }
372
+ assert_raises(CommandlineError) { @p.parse %w(-f 1.0.0) }
373
+ assert_raises(CommandlineError) { @p.parse %w(-f .) }
374
+ assert_raises(CommandlineError) { @p.parse %w(-f -.) }
375
+ end
376
+
377
+ def test_date_formatting
378
+ @p.opt :arg, "desc", :type => :date, :short => 'd'
379
+ opts = nil
380
+ assert_nothing_raised { opts = @p.parse(['-d', 'Jan 4, 2007']) }
381
+ assert_equal Date.civil(2007, 1, 4), opts[:arg]
382
+ begin
383
+ require 'chronic'
384
+ assert_nothing_raised { opts = @p.parse(['-d', 'today']) }
385
+ assert_equal Date.today, opts[:arg]
386
+ rescue LoadError
387
+ # chronic is not available
388
+ end
389
+ end
390
+
391
+ def test_short_options_cant_be_numeric
392
+ assert_raises(ArgumentError) { @p.opt :arg, "desc", :short => "-1" }
393
+ @p.opt :a1b, "desc"
394
+ @p.opt :a2b, "desc"
395
+ assert_not_equal "2", @p.specs[:a2b][:short]
396
+ end
397
+
398
+ def test_short_options_can_be_weird
399
+ assert_nothing_raised { @p.opt :arg1, "desc", :short => "#" }
400
+ assert_nothing_raised { @p.opt :arg2, "desc", :short => "." }
401
+ assert_raises(ArgumentError) { @p.opt :arg3, "desc", :short => "-" }
402
+ end
403
+
404
+ def test_options_cant_be_set_multiple_times_if_not_specified
405
+ @p.opt :arg, "desc", :short => "-x"
406
+ assert_nothing_raised { @p.parse %w(-x) }
407
+ assert_raises(CommandlineError) { @p.parse %w(-x -x) }
408
+ assert_raises(CommandlineError) { @p.parse %w(-xx) }
409
+ end
410
+
411
+ def test_options_can_be_set_multiple_times_if_specified
412
+ assert_nothing_raised do
413
+ @p.opt :arg, "desc", :short => "-x", :multi => true
414
+ end
415
+ assert_nothing_raised { @p.parse %w(-x) }
416
+ assert_nothing_raised { @p.parse %w(-x -x) }
417
+ assert_nothing_raised { @p.parse %w(-xx) }
418
+ end
419
+
420
+ def test_short_options_with_multiple_options
421
+ opts = nil
422
+
423
+ assert_nothing_raised do
424
+ @p.opt :xarg, "desc", :short => "-x", :type => String, :multi => true
425
+ end
426
+ assert_nothing_raised { opts = @p.parse %w(-x a -x b) }
427
+ assert_equal %w(a b), opts[:xarg]
428
+ assert_equal [], @p.leftovers
429
+ end
430
+
431
+ def short_options_with_multiple_options_does_not_affect_flags_type
432
+ opts = nil
433
+
434
+ assert_nothing_raised do
435
+ @p.opt :xarg, "desc", :short => "-x", :type => :flag, :multi => true
436
+ end
437
+
438
+ assert_nothing_raised { opts = @p.parse %w(-x a) }
439
+ assert_equal true, opts[:xarg]
440
+ assert_equal %w(a), @p.leftovers
441
+
442
+ assert_nothing_raised { opts = @p.parse %w(-x a -x b) }
443
+ assert_equal true, opts[:xarg]
444
+ assert_equal %w(a b), @p.leftovers
445
+
446
+ assert_nothing_raised { opts = @p.parse %w(-xx a -x b) }
447
+ assert_equal true, opts[:xarg]
448
+ assert_equal %w(a b), @p.leftovers
449
+ end
450
+
451
+ def test_short_options_with_multiple_arguments
452
+ opts = nil
453
+
454
+ @p.opt :xarg, "desc", :type => :ints
455
+ assert_nothing_raised { opts = @p.parse %w(-x 3 4 0) }
456
+ assert_equal [3, 4, 0], opts[:xarg]
457
+ assert_equal [], @p.leftovers
458
+
459
+ @p.opt :yarg, "desc", :type => :floats
460
+ assert_nothing_raised { opts = @p.parse %w(-y 3.14 4.21 0.66) }
461
+ assert_equal [3.14, 4.21, 0.66], opts[:yarg]
462
+ assert_equal [], @p.leftovers
463
+
464
+ @p.opt :zarg, "desc", :type => :strings
465
+ assert_nothing_raised { opts = @p.parse %w(-z a b c) }
466
+ assert_equal %w(a b c), opts[:zarg]
467
+ assert_equal [], @p.leftovers
468
+ end
469
+
470
+ def test_short_options_with_multiple_options_and_arguments
471
+ opts = nil
472
+
473
+ @p.opt :xarg, "desc", :type => :ints, :multi => true
474
+ assert_nothing_raised { opts = @p.parse %w(-x 3 4 5 -x 6 7) }
475
+ assert_equal [[3, 4, 5], [6, 7]], opts[:xarg]
476
+ assert_equal [], @p.leftovers
477
+
478
+ @p.opt :yarg, "desc", :type => :floats, :multi => true
479
+ assert_nothing_raised { opts = @p.parse %w(-y 3.14 4.21 5.66 -y 6.99 7.01) }
480
+ assert_equal [[3.14, 4.21, 5.66], [6.99, 7.01]], opts[:yarg]
481
+ assert_equal [], @p.leftovers
482
+
483
+ @p.opt :zarg, "desc", :type => :strings, :multi => true
484
+ assert_nothing_raised { opts = @p.parse %w(-z a b c -z d e) }
485
+ assert_equal [%w(a b c), %w(d e)], opts[:zarg]
486
+ assert_equal [], @p.leftovers
487
+ end
488
+
489
+ def test_combined_short_options_with_multiple_arguments
490
+ @p.opt :arg1, "desc", :short => "a"
491
+ @p.opt :arg2, "desc", :short => "b"
492
+ @p.opt :arg3, "desc", :short => "c", :type => :ints
493
+ @p.opt :arg4, "desc", :short => "d", :type => :floats
494
+
495
+ opts = nil
496
+
497
+ assert_nothing_raised { opts = @p.parse %w(-abc 4 6 9) }
498
+ assert_equal true, opts[:arg1]
499
+ assert_equal true, opts[:arg2]
500
+ assert_equal [4, 6, 9], opts[:arg3]
501
+
502
+ assert_nothing_raised { opts = @p.parse %w(-ac 4 6 9 -bd 3.14 2.41) }
503
+ assert_equal true, opts[:arg1]
504
+ assert_equal true, opts[:arg2]
505
+ assert_equal [4, 6, 9], opts[:arg3]
506
+ assert_equal [3.14, 2.41], opts[:arg4]
507
+
508
+ assert_raises(CommandlineError) { opts = @p.parse %w(-abcd 3.14 2.41) }
509
+ end
510
+
511
+ def test_long_options_with_multiple_options
512
+ @p.opt :xarg, "desc", :type => String, :multi => true
513
+ opts = nil
514
+ assert_nothing_raised { opts = @p.parse %w(--xarg=a --xarg=b) }
515
+ assert_equal %w(a b), opts[:xarg]
516
+ assert_equal [], @p.leftovers
517
+ assert_nothing_raised { opts = @p.parse %w(--xarg a --xarg b) }
518
+ assert_equal %w(a b), opts[:xarg]
519
+ assert_equal [], @p.leftovers
520
+ end
521
+
522
+ def test_long_options_with_multiple_arguments
523
+ opts = nil
524
+
525
+ @p.opt :xarg, "desc", :type => :ints
526
+ assert_nothing_raised { opts = @p.parse %w(--xarg 3 2 5) }
527
+ assert_equal [3, 2, 5], opts[:xarg]
528
+ assert_equal [], @p.leftovers
529
+ assert_nothing_raised { opts = @p.parse %w(--xarg=3) }
530
+ assert_equal [3], opts[:xarg]
531
+ assert_equal [], @p.leftovers
532
+
533
+ @p.opt :yarg, "desc", :type => :floats
534
+ assert_nothing_raised { opts = @p.parse %w(--yarg 3.14 2.41 5.66) }
535
+ assert_equal [3.14, 2.41, 5.66], opts[:yarg]
536
+ assert_equal [], @p.leftovers
537
+ assert_nothing_raised { opts = @p.parse %w(--yarg=3.14) }
538
+ assert_equal [3.14], opts[:yarg]
539
+ assert_equal [], @p.leftovers
540
+
541
+ @p.opt :zarg, "desc", :type => :strings
542
+ assert_nothing_raised { opts = @p.parse %w(--zarg a b c) }
543
+ assert_equal %w(a b c), opts[:zarg]
544
+ assert_equal [], @p.leftovers
545
+ assert_nothing_raised { opts = @p.parse %w(--zarg=a) }
546
+ assert_equal %w(a), opts[:zarg]
547
+ assert_equal [], @p.leftovers
548
+ end
549
+
550
+ def test_long_options_with_multiple_options_and_arguments
551
+ opts = nil
552
+
553
+ @p.opt :xarg, "desc", :type => :ints, :multi => true
554
+ assert_nothing_raised { opts = @p.parse %w(--xarg 3 2 5 --xarg 2 1) }
555
+ assert_equal [[3, 2, 5], [2, 1]], opts[:xarg]
556
+ assert_equal [], @p.leftovers
557
+ assert_nothing_raised { opts = @p.parse %w(--xarg=3 --xarg=2) }
558
+ assert_equal [[3], [2]], opts[:xarg]
559
+ assert_equal [], @p.leftovers
560
+
561
+ @p.opt :yarg, "desc", :type => :floats, :multi => true
562
+ assert_nothing_raised { opts = @p.parse %w(--yarg 3.14 2.72 5 --yarg 2.41 1.41) }
563
+ assert_equal [[3.14, 2.72, 5], [2.41, 1.41]], opts[:yarg]
564
+ assert_equal [], @p.leftovers
565
+ assert_nothing_raised { opts = @p.parse %w(--yarg=3.14 --yarg=2.41) }
566
+ assert_equal [[3.14], [2.41]], opts[:yarg]
567
+ assert_equal [], @p.leftovers
568
+
569
+ @p.opt :zarg, "desc", :type => :strings, :multi => true
570
+ assert_nothing_raised { opts = @p.parse %w(--zarg a b c --zarg d e) }
571
+ assert_equal [%w(a b c), %w(d e)], opts[:zarg]
572
+ assert_equal [], @p.leftovers
573
+ assert_nothing_raised { opts = @p.parse %w(--zarg=a --zarg=d) }
574
+ assert_equal [%w(a), %w(d)], opts[:zarg]
575
+ assert_equal [], @p.leftovers
576
+ end
577
+
578
+ def test_long_options_also_take_equals
579
+ @p.opt :arg, "desc", :long => "arg", :type => String, :default => "hello"
580
+ opts = nil
581
+ assert_nothing_raised { opts = @p.parse %w() }
582
+ assert_equal "hello", opts[:arg]
583
+ assert_nothing_raised { opts = @p.parse %w(--arg goat) }
584
+ assert_equal "goat", opts[:arg]
585
+ assert_nothing_raised { opts = @p.parse %w(--arg=goat) }
586
+ assert_equal "goat", opts[:arg]
587
+ ## actually, this next one is valid. empty string for --arg, and goat as a
588
+ ## leftover.
589
+ ## assert_raises(CommandlineError) { opts = @p.parse %w(--arg= goat) }
590
+ end
591
+
592
+ def test_auto_generated_long_names_convert_underscores_to_hyphens
593
+ @p.opt :hello_there
594
+ assert_equal "hello-there", @p.specs[:hello_there][:long]
595
+ end
596
+
597
+ def test_arguments_passed_through_block
598
+ @goat = 3
599
+ boat = 4
600
+ Parser.new(@goat) do |goat|
601
+ boat = goat
602
+ end
603
+ assert_equal @goat, boat
604
+ end
605
+
606
+ def test_help_has_default_banner
607
+ @p = Parser.new
608
+ sio = StringIO.new "w"
609
+ @p.parse []
610
+ @p.educate sio
611
+ help = sio.string.split "\n"
612
+ assert help[0] =~ /options/i
613
+ assert_equal 2, help.length # options, then -h
614
+
615
+ @p = Parser.new
616
+ @p.version "my version"
617
+ sio = StringIO.new "w"
618
+ @p.parse []
619
+ @p.educate sio
620
+ help = sio.string.split "\n"
621
+ assert help[0] =~ /my version/i
622
+ assert_equal 4, help.length # version, options, -h, -v
623
+
624
+ @p = Parser.new
625
+ @p.banner "my own banner"
626
+ sio = StringIO.new "w"
627
+ @p.parse []
628
+ @p.educate sio
629
+ help = sio.string.split "\n"
630
+ assert help[0] =~ /my own banner/i
631
+ assert_equal 2, help.length # banner, -h
632
+ end
633
+
634
+ def test_help_preserves_positions
635
+ @p.opt :zzz, "zzz"
636
+ @p.opt :aaa, "aaa"
637
+ sio = StringIO.new "w"
638
+ @p.educate sio
639
+
640
+ help = sio.string.split "\n"
641
+ assert help[1] =~ /zzz/
642
+ assert help[2] =~ /aaa/
643
+ end
644
+
645
+ def test_version_and_help_short_args_can_be_overridden
646
+ @p.opt :verbose, "desc", :short => "-v"
647
+ @p.opt :hello, "desc", :short => "-h"
648
+ @p.version "version"
649
+
650
+ assert_nothing_raised { @p.parse(%w(-v)) }
651
+ assert_raises(VersionNeeded) { @p.parse(%w(--version)) }
652
+ assert_nothing_raised { @p.parse(%w(-h)) }
653
+ assert_raises(HelpNeeded) { @p.parse(%w(--help)) }
654
+ end
655
+
656
+ def test_version_and_help_long_args_can_be_overridden
657
+ @p.opt :asdf, "desc", :long => "help"
658
+ @p.opt :asdf2, "desc2", :long => "version"
659
+ assert_nothing_raised { @p.parse %w() }
660
+ assert_nothing_raised { @p.parse %w(--help) }
661
+ assert_nothing_raised { @p.parse %w(--version) }
662
+ assert_nothing_raised { @p.parse %w(-h) }
663
+ assert_nothing_raised { @p.parse %w(-v) }
664
+ end
665
+
666
+ def test_version_and_help_override_errors
667
+ @p.opt :asdf, "desc", :type => String
668
+ @p.version "version"
669
+ assert_nothing_raised { @p.parse %w(--asdf goat) }
670
+ assert_raises(CommandlineError) { @p.parse %w(--asdf) }
671
+ assert_raises(HelpNeeded) { @p.parse %w(--asdf --help) }
672
+ assert_raises(VersionNeeded) { @p.parse %w(--asdf --version) }
673
+ end
674
+
675
+ def test_conflicts
676
+ @p.opt :one
677
+ assert_raises(ArgumentError) { @p.conflicts :one, :two }
678
+ @p.opt :two
679
+ assert_nothing_raised { @p.conflicts :one, :two }
680
+ assert_nothing_raised { @p.parse %w(--one) }
681
+ assert_nothing_raised { @p.parse %w(--two) }
682
+ assert_raises(CommandlineError) { @p.parse %w(--one --two) }
683
+
684
+ @p.opt :hello
685
+ @p.opt :yellow
686
+ @p.opt :mellow
687
+ @p.opt :jello
688
+ @p.conflicts :hello, :yellow, :mellow, :jello
689
+ assert_raises(CommandlineError) { @p.parse %w(--hello --yellow --mellow --jello) }
690
+ assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
691
+ assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
692
+
693
+ assert_nothing_raised { @p.parse %w(--hello) }
694
+ assert_nothing_raised { @p.parse %w(--jello) }
695
+ assert_nothing_raised { @p.parse %w(--yellow) }
696
+ assert_nothing_raised { @p.parse %w(--mellow) }
697
+
698
+ assert_nothing_raised { @p.parse %w(--mellow --one) }
699
+ assert_nothing_raised { @p.parse %w(--mellow --two) }
700
+
701
+ assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
702
+ assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
703
+ end
704
+
705
+ def test_conflict_error_messages
706
+ @p.opt :one
707
+ @p.opt "two"
708
+ @p.conflicts :one, "two"
709
+
710
+ begin
711
+ @p.parse %w(--one --two)
712
+ flunk "no error thrown"
713
+ rescue CommandlineError => e
714
+ assert_match(/--one/, e.message)
715
+ assert_match(/--two/, e.message)
716
+ end
717
+ end
718
+
719
+ def test_depends
720
+ @p.opt :one
721
+ assert_raises(ArgumentError) { @p.depends :one, :two }
722
+ @p.opt :two
723
+ assert_nothing_raised { @p.depends :one, :two }
724
+ assert_nothing_raised { @p.parse %w(--one --two) }
725
+ assert_raises(CommandlineError) { @p.parse %w(--one) }
726
+ assert_raises(CommandlineError) { @p.parse %w(--two) }
727
+
728
+ @p.opt :hello
729
+ @p.opt :yellow
730
+ @p.opt :mellow
731
+ @p.opt :jello
732
+ @p.depends :hello, :yellow, :mellow, :jello
733
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello) }
734
+ assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
735
+ assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
736
+
737
+ assert_raises(CommandlineError) { @p.parse %w(--hello) }
738
+ assert_raises(CommandlineError) { @p.parse %w(--mellow) }
739
+
740
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello --one --two) }
741
+ assert_nothing_raised { @p.parse %w(--hello --yellow --mellow --jello --one --two a b c) }
742
+
743
+ assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello --one) }
744
+ end
745
+
746
+ def test_depend_error_messages
747
+ @p.opt :one
748
+ @p.opt "two"
749
+ @p.depends :one, "two"
750
+
751
+ assert_nothing_raised { @p.parse %w(--one --two) }
752
+
753
+ begin
754
+ @p.parse %w(--one)
755
+ flunk "no error thrown"
756
+ rescue CommandlineError => e
757
+ assert_match(/--one/, e.message)
758
+ assert_match(/--two/, e.message)
759
+ end
760
+
761
+ begin
762
+ @p.parse %w(--two)
763
+ flunk "no error thrown"
764
+ rescue CommandlineError => e
765
+ assert_match(/--one/, e.message)
766
+ assert_match(/--two/, e.message)
767
+ end
768
+ end
769
+
770
+ ## courtesy neill zero
771
+ def test_two_required_one_missing_accuses_correctly
772
+ @p.opt "arg1", "desc1", :required => true
773
+ @p.opt "arg2", "desc2", :required => true
774
+
775
+ begin
776
+ @p.parse(%w(--arg1))
777
+ flunk "should have failed on a missing req"
778
+ rescue CommandlineError => e
779
+ assert e.message =~ /arg2/, "didn't mention arg2 in the error msg: #{e.message}"
780
+ end
781
+
782
+ begin
783
+ @p.parse(%w(--arg2))
784
+ flunk "should have failed on a missing req"
785
+ rescue CommandlineError => e
786
+ assert e.message =~ /arg1/, "didn't mention arg1 in the error msg: #{e.message}"
787
+ end
788
+
789
+ assert_nothing_raised { @p.parse(%w(--arg1 --arg2)) }
790
+ end
791
+
792
+ def test_stopwords_mixed
793
+ @p.opt "arg1", :default => false
794
+ @p.opt "arg2", :default => false
795
+ @p.stop_on %w(happy sad)
796
+
797
+ opts = @p.parse %w(--arg1 happy --arg2)
798
+ assert_equal true, opts["arg1"]
799
+ assert_equal false, opts["arg2"]
800
+
801
+ ## restart parsing
802
+ @p.leftovers.shift
803
+ opts = @p.parse @p.leftovers
804
+ assert_equal false, opts["arg1"]
805
+ assert_equal true, opts["arg2"]
806
+ end
807
+
808
+ def test_stopwords_no_stopwords
809
+ @p.opt "arg1", :default => false
810
+ @p.opt "arg2", :default => false
811
+ @p.stop_on %w(happy sad)
812
+
813
+ opts = @p.parse %w(--arg1 --arg2)
814
+ assert_equal true, opts["arg1"]
815
+ assert_equal true, opts["arg2"]
816
+
817
+ ## restart parsing
818
+ @p.leftovers.shift
819
+ opts = @p.parse @p.leftovers
820
+ assert_equal false, opts["arg1"]
821
+ assert_equal false, opts["arg2"]
822
+ end
823
+
824
+ def test_stopwords_multiple_stopwords
825
+ @p.opt "arg1", :default => false
826
+ @p.opt "arg2", :default => false
827
+ @p.stop_on %w(happy sad)
828
+
829
+ opts = @p.parse %w(happy sad --arg1 --arg2)
830
+ assert_equal false, opts["arg1"]
831
+ assert_equal false, opts["arg2"]
832
+
833
+ ## restart parsing
834
+ @p.leftovers.shift
835
+ opts = @p.parse @p.leftovers
836
+ assert_equal false, opts["arg1"]
837
+ assert_equal false, opts["arg2"]
838
+
839
+ ## restart parsing again
840
+ @p.leftovers.shift
841
+ opts = @p.parse @p.leftovers
842
+ assert_equal true, opts["arg1"]
843
+ assert_equal true, opts["arg2"]
844
+ end
845
+
846
+ def test_stopwords_with_short_args
847
+ @p.opt :global_option, "This is a global option", :short => "-g"
848
+ @p.stop_on %w(sub-command-1 sub-command-2)
849
+
850
+ global_opts = @p.parse %w(-g sub-command-1 -c)
851
+ cmd = @p.leftovers.shift
852
+
853
+ @q = Parser.new
854
+ @q.opt :cmd_option, "This is an option only for the subcommand", :short => "-c"
855
+ cmd_opts = @q.parse @p.leftovers
856
+
857
+ assert_equal true, global_opts[:global_option]
858
+ assert_nil global_opts[:cmd_option]
859
+
860
+ assert_equal true, cmd_opts[:cmd_option]
861
+ assert_nil cmd_opts[:global_option]
862
+
863
+ assert_equal cmd, "sub-command-1"
864
+ assert_equal @q.leftovers, []
865
+ end
866
+
867
+ def assert_parses_correctly(parser, commandline, expected_opts,
868
+ expected_leftovers)
869
+ opts = parser.parse commandline
870
+ assert_equal expected_opts, opts
871
+ assert_equal expected_leftovers, parser.leftovers
872
+ end
873
+
874
+ def test_unknown_subcommand
875
+ @p.opt :global_flag, "Global flag", :short => "-g", :type => :flag
876
+ @p.opt :global_param, "Global parameter", :short => "-p", :default => 5
877
+ @p.stop_on_unknown
878
+
879
+ expected_opts = { :global_flag => true, :help => false, :global_param => 5, :global_flag_given => true }
880
+ expected_leftovers = [ "my_subcommand", "-c" ]
881
+
882
+ assert_parses_correctly @p, %w(--global-flag my_subcommand -c), \
883
+ expected_opts, expected_leftovers
884
+ assert_parses_correctly @p, %w(-g my_subcommand -c), \
885
+ expected_opts, expected_leftovers
886
+
887
+ expected_opts = { :global_flag => false, :help => false, :global_param => 5, :global_param_given => true }
888
+ expected_leftovers = [ "my_subcommand", "-c" ]
889
+
890
+ assert_parses_correctly @p, %w(-p 5 my_subcommand -c), \
891
+ expected_opts, expected_leftovers
892
+ assert_parses_correctly @p, %w(--global-param 5 my_subcommand -c), \
893
+ expected_opts, expected_leftovers
894
+ end
895
+
896
+ def test_alternate_args
897
+ args = %w(-a -b -c)
898
+
899
+ opts = ::Trollop.options(args) do
900
+ opt :alpher, "Ralph Alpher", :short => "-a"
901
+ opt :bethe, "Hans Bethe", :short => "-b"
902
+ opt :gamow, "George Gamow", :short => "-c"
903
+ end
904
+
905
+ physicists_with_humor = [:alpher, :bethe, :gamow]
906
+ physicists_with_humor.each do |physicist|
907
+ assert_equal true, opts[physicist]
908
+ end
909
+ end
910
+
911
+ def test_io_arg_type
912
+ @p.opt :arg, "desc", :type => :io
913
+ @p.opt :arg2, "desc", :type => IO
914
+ @p.opt :arg3, "desc", :default => $stdout
915
+
916
+ opts = nil
917
+ assert_nothing_raised { opts = @p.parse() }
918
+ assert_equal $stdout, opts[:arg3]
919
+
920
+ assert_nothing_raised { opts = @p.parse %w(--arg /dev/null) }
921
+ assert_kind_of File, opts[:arg]
922
+ assert_equal "/dev/null", opts[:arg].path
923
+
924
+ #TODO: move to mocks
925
+ #assert_nothing_raised { opts = @p.parse %w(--arg2 http://google.com/) }
926
+ #assert_kind_of StringIO, opts[:arg2]
927
+
928
+ assert_nothing_raised { opts = @p.parse %w(--arg3 stdin) }
929
+ assert_equal $stdin, opts[:arg3]
930
+
931
+ assert_raises(CommandlineError) { opts = @p.parse %w(--arg /fdasfasef/fessafef/asdfasdfa/fesasf) }
932
+ end
933
+
934
+ def test_openstruct_style_access
935
+ @p.opt "arg1", "desc", :type => :int
936
+ @p.opt :arg2, "desc", :type => :int
937
+
938
+ opts = @p.parse(%w(--arg1 3 --arg2 4))
939
+
940
+ assert_nothing_raised { opts.arg1 }
941
+ assert_nothing_raised { opts.arg2 }
942
+ assert_equal 3, opts.arg1
943
+ assert_equal 4, opts.arg2
944
+ end
945
+
946
+ def test_multi_args_autobox_defaults
947
+ @p.opt :arg1, "desc", :default => "hello", :multi => true
948
+ @p.opt :arg2, "desc", :default => ["hello"], :multi => true
949
+
950
+ opts = @p.parse
951
+ assert_equal ["hello"], opts[:arg1]
952
+ assert_equal ["hello"], opts[:arg2]
953
+
954
+ opts = @p.parse %w(--arg1 hello)
955
+ assert_equal ["hello"], opts[:arg1]
956
+ assert_equal ["hello"], opts[:arg2]
957
+
958
+ opts = @p.parse %w(--arg1 hello --arg1 there)
959
+ assert_equal ["hello", "there"], opts[:arg1]
960
+ end
961
+
962
+ def test_ambigious_multi_plus_array_default_resolved_as_specified_by_documentation
963
+ @p.opt :arg1, "desc", :default => ["potato"], :multi => true
964
+ @p.opt :arg2, "desc", :default => ["potato"], :multi => true, :type => :strings
965
+ @p.opt :arg3, "desc", :default => ["potato"]
966
+ @p.opt :arg4, "desc", :default => ["potato", "rhubarb"], :short => :none, :multi => true
967
+
968
+ ## arg1 should be multi-occurring but not multi-valued
969
+ opts = @p.parse %w(--arg1 one two)
970
+ assert_equal ["one"], opts[:arg1]
971
+ assert_equal ["two"], @p.leftovers
972
+
973
+ opts = @p.parse %w(--arg1 one --arg1 two)
974
+ assert_equal ["one", "two"], opts[:arg1]
975
+ assert_equal [], @p.leftovers
976
+
977
+ ## arg2 should be multi-valued and multi-occurring
978
+ opts = @p.parse %w(--arg2 one two)
979
+ assert_equal [["one", "two"]], opts[:arg2]
980
+ assert_equal [], @p.leftovers
981
+
982
+ ## arg3 should be multi-valued but not multi-occurring
983
+ opts = @p.parse %w(--arg3 one two)
984
+ assert_equal ["one", "two"], opts[:arg3]
985
+ assert_equal [], @p.leftovers
986
+
987
+ ## arg4 should be multi-valued but not multi-occurring
988
+ opts = @p.parse %w()
989
+ assert_equal ["potato", "rhubarb"], opts[:arg4]
990
+ end
991
+
992
+ def test_given_keys
993
+ @p.opt :arg1
994
+ @p.opt :arg2
995
+
996
+ opts = @p.parse %w(--arg1)
997
+ assert opts[:arg1_given]
998
+ assert !opts[:arg2_given]
999
+
1000
+ opts = @p.parse %w(--arg2)
1001
+ assert !opts[:arg1_given]
1002
+ assert opts[:arg2_given]
1003
+
1004
+ opts = @p.parse []
1005
+ assert !opts[:arg1_given]
1006
+ assert !opts[:arg2_given]
1007
+
1008
+ opts = @p.parse %w(--arg1 --arg2)
1009
+ assert opts[:arg1_given]
1010
+ assert opts[:arg2_given]
1011
+ end
1012
+
1013
+ def test_default_shorts_assigned_only_after_user_shorts
1014
+ @p.opt :aab, "aaa" # should be assigned to -b
1015
+ @p.opt :ccd, "bbb" # should be assigned to -d
1016
+ @p.opt :user1, "user1", :short => 'a'
1017
+ @p.opt :user2, "user2", :short => 'c'
1018
+
1019
+ opts = @p.parse %w(-a -b)
1020
+ assert opts[:user1]
1021
+ assert !opts[:user2]
1022
+ assert opts[:aab]
1023
+ assert !opts[:ccd]
1024
+
1025
+ opts = @p.parse %w(-c -d)
1026
+ assert !opts[:user1]
1027
+ assert opts[:user2]
1028
+ assert !opts[:aab]
1029
+ assert opts[:ccd]
1030
+ end
1031
+
1032
+ def test_accepts_arguments_with_spaces
1033
+ @p.opt :arg1, "arg", :type => String
1034
+ @p.opt :arg2, "arg2", :type => String
1035
+
1036
+ opts = @p.parse ["--arg1", "hello there", "--arg2=hello there"]
1037
+ assert_equal "hello there", opts[:arg1]
1038
+ assert_equal "hello there", opts[:arg2]
1039
+ assert_equal 0, @p.leftovers.size
1040
+ end
1041
+
1042
+ def test_multi_args_default_to_empty_array
1043
+ @p.opt :arg1, "arg", :multi => true
1044
+ opts = @p.parse []
1045
+ assert_equal [], opts[:arg1]
1046
+ end
1047
+
1048
+ def test_simple_interface_handles_help
1049
+ ARGV.clear
1050
+ ARGV.unshift "-h"
1051
+ assert_raises(SystemExit) do
1052
+ ::Trollop::options do
1053
+ opt :potato
1054
+ end
1055
+ raise "broken"
1056
+ end
1057
+ end
1058
+
1059
+ def test_simple_interface_handles_version
1060
+ ARGV.clear
1061
+ ARGV.unshift "-v"
1062
+ assert_raises(SystemExit) do
1063
+ ::Trollop::options do
1064
+ version "1.2"
1065
+ opt :potato
1066
+ end
1067
+ raise "broken"
1068
+ end
1069
+ end
1070
+
1071
+ def test_simple_interface_handles_regular_usage
1072
+ ARGV.clear
1073
+ ARGV.unshift "--potato"
1074
+ opts = nil
1075
+ assert_nothing_raised do
1076
+ opts = ::Trollop::options do
1077
+ opt :potato
1078
+ end
1079
+ end
1080
+ assert opts[:potato]
1081
+ end
1082
+
1083
+ def test_simple_interface_handles_die
1084
+ ARGV.clear
1085
+ ARGV.unshift "--potato"
1086
+ ::Trollop::options do
1087
+ opt :potato
1088
+ end
1089
+ assert_raises(SystemExit) { ::Trollop::die :potato, "is invalid" }
1090
+ end
1091
+ end
1092
+
1093
+ end
1094
+ end