trollop-ghetto 1.16.3

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