File: option_parser_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (791 lines) | stat: -rw-r--r-- 22,182 bytes parent folder | download | duplicates (2)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
require "spec"
require "option_parser"

private def expect_capture_option(args, option, value)
  flag = nil
  OptionParser.parse(args) do |opts|
    opts.on(option, "some flag") do |flag_value|
      flag = flag_value
    end
  end
  flag.should eq(value)
  args.size.should eq(0)
end

private def expect_doesnt_capture_option(args, option)
  flag = false
  OptionParser.parse(args) do |opts|
    opts.on(option, "some flag") do
      flag = true
    end
  end
  flag.should be_false
end

private def expect_missing_option(option)
  expect_raises OptionParser::MissingOption do
    OptionParser.parse([] of String) do |opts|
      opts.on(option, "some flag") do |flag_value|
      end
    end
  end
end

private def expect_missing_option(args, option, flag)
  expect_raises OptionParser::MissingOption, "Missing option: #{flag}" do
    OptionParser.parse(args) do |opts|
      opts.on(option, "some flag") do |flag_value|
      end
    end
  end
end

describe "OptionParser" do
  it "has flag" do
    expect_capture_option ["-f"], "-f", ""
  end

  it "has flag with many letters" do
    expect_capture_option ["-ll"], "-ll", "l"
  end

  it "doesn't have flag" do
    expect_doesnt_capture_option [] of String, "-f"
  end

  it "has flag with double dash" do
    expect_capture_option ["--flag"], "--flag", ""
  end

  it "doesn't have flag with double dash" do
    expect_doesnt_capture_option [] of String, "--flag"
  end

  it "has required option next to flag" do
    expect_capture_option ["-f123"], "-fFLAG", "123"
  end

  it "has required option next to flag but given separated" do
    expect_capture_option ["-f", "123"], "-fFLAG", "123"
  end

  it "raises if missing option next to flag" do
    expect_missing_option ["-f"], "-fFLAG", "-f"
  end

  it "has required option separated from flag" do
    expect_capture_option ["-f", "123"], "-f FLAG", "123"
  end

  it "has required option separated from flag but given together" do
    expect_capture_option ["-f123"], "-f FLAG", "123"
  end

  it "gets short option with value that looks like flag" do
    expect_capture_option ["-f", "-g -h"], "-f FLAG", "-g -h"
  end

  describe "Consumption of flags following an ungiven optional argument" do
    context "Given a short option with an optional value" do
      it "doesn't eat a following short option" do
        flag = nil
        not_eaten = [] of String
        args = ["-f", "-g", "-i"]
        OptionParser.parse(args) do |opts|
          opts.on("-f [FLAG]", "some flag") do |flag_value|
            flag = flag_value
          end
          opts.on("-g", "shouldn't be eaten") do
            not_eaten << "-g"
          end
          opts.on("-i", "shouldn't be eaten") do
            not_eaten << "-i"
          end
        end
        flag.should eq("")
        not_eaten.should eq(["-g", "-i"])
        args.size.should eq(0)
      end

      it "doesn't eat a following long option" do
        flag = nil
        not_eaten = [] of String
        args = ["-f", "--g-long", "-i"]
        OptionParser.parse(args) do |opts|
          opts.on("-f [FLAG]", "some flag") do |flag_value|
            flag = flag_value
          end
          opts.on("-g", "--g-long", "shouldn't be eaten") do
            not_eaten << "--g-long"
          end
          opts.on("-i", "shouldn't be eaten") do
            not_eaten << "-i"
          end
        end
        flag.should eq("")
        not_eaten.should eq(["--g-long", "-i"])
        args.size.should eq(0)
      end

      it "does eat a value that looks like an option" do
        flag = nil
        not_eaten = [] of String
        args = ["-f", "--not-an-option--", "-i"]
        OptionParser.parse(args) do |opts|
          opts.on("-f [FLAG]", "some flag") do |flag_value|
            flag = flag_value
          end
          opts.on("-i", "shouldn't be eaten") do
            not_eaten << "-i"
          end
        end
        flag.should eq("--not-an-option--")
        not_eaten.should eq(["-i"])
        args.size.should eq(0)
      end
    end

    context "Given a long option with an optional value" do
      it "doesn't eat further short options" do
        flag = nil
        not_eaten = [] of String
        args = ["--long-flag", "-g", "-i"]
        OptionParser.parse(args) do |opts|
          opts.on("--long-flag [FLAG]", "some flag") do |flag_value|
            flag = flag_value
          end
          opts.on("-g", "shouldn't be eaten") do
            not_eaten << "-g"
          end
          opts.on("-i", "shouldn't be eaten") do
            not_eaten << "-i"
          end
        end
        flag.should eq("")
        not_eaten.should eq(["-g", "-i"])
        args.size.should eq(0)
      end

      it "doesn't eat further long options" do
        flag = nil
        not_eaten = [] of String
        args = ["--long-flag", "--g-long", "-i"]
        OptionParser.parse(args) do |opts|
          opts.on("--long-flag [FLAG]", "some flag") do |flag_value|
            flag = flag_value
          end
          opts.on("--g-long", "shouldn't be eaten") do
            not_eaten << "--g-long"
          end
          opts.on("-i", "shouldn't be eaten") do
            not_eaten << "-i"
          end
        end
        flag.should eq("")
        not_eaten.should eq(["--g-long", "-i"])
        args.size.should eq(0)
      end
    end
  end

  it "raises if missing required option with space" do
    expect_missing_option ["-f"], "-f FLAG", "-f"
  end

  it "has required option separated from long flag" do
    expect_capture_option ["--flag", "123"], "--flag FLAG", "123"
  end

  it "has required option with =" do
    expect_capture_option ["--flag=123"], "--flag FLAG", "123"
  end

  it "has required option with = (2)" do
    expect_capture_option ["--flag=123"], "--flag=FLAG", "123"
  end

  it "has required option with = (3) handles empty" do
    expect_capture_option ["--flag="], "--flag=FLAG", ""
  end

  it "raises if missing required argument separated from long flag" do
    expect_missing_option ["--flag"], "--flag FLAG", "--flag"
  end

  it "has required option with space" do
    expect_capture_option ["-f", "123"], "-f ", "123"
  end

  it "has required option with long flag space" do
    expect_capture_option ["--flag", "123"], "--flag ", "123"
  end

  it "gets short option with value -- (#8937)" do
    expect_capture_option ["-f", "--"], "-f ARG", "--"
  end

  it "gets long option with value -- (#8937)" do
    expect_capture_option ["--flag", "--"], "--flag [ARG]", "--"
  end

  it "doesn't raise if required option is not specified" do
    expect_doesnt_capture_option [] of String, "-f "
  end

  it "doesn't raise if optional option is not specified with short flag" do
    expect_doesnt_capture_option [] of String, "-f[FLAG]"
  end

  it "doesn't raise if optional option is not specified with long flag" do
    expect_doesnt_capture_option [] of String, "--flag [FLAG]"
  end

  it "doesn't raise if optional option is not specified with separated short flag" do
    expect_doesnt_capture_option [] of String, "-f [FLAG]"
  end

  it "doesn't raise if required option is not specified with separated short flag" do
    expect_doesnt_capture_option [] of String, "-f FLAG"
  end

  describe "gnu_optional_args" do
    it "doesn't get optional argument for short flag after space" do
      flag = nil
      args = %w(-f 123)
      OptionParser.parse(args, gnu_optional_args: true) do |opts|
        opts.on("-f [FLAG]", "some flag") do |flag_value|
          flag = flag_value
        end
      end
      flag.should eq("")
      args.should eq(%w(123))
    end

    it "doesn't get optional argument for long flag after space" do
      flag = nil
      args = %w(--f 123)
      OptionParser.parse(args, gnu_optional_args: true) do |opts|
        opts.on("--f [FLAG]", "some flag") do |flag_value|
          flag = flag_value
        end
      end
      flag.should eq("")
      args.should eq(%w(123))
    end
  end

  it "parses argument when only referenced in long flag" do
    captured = ""
    parser = OptionParser.parse([] of String) do |opts|
      opts.on("-f", "--flag X", "some flag") { |x| captured = x }
    end
    parser.parse(["-f", "12"])
    captured.should eq "12"
    parser.to_s.should contain "   -f, --flag X"
  end

  it "parses argument when referenced in long and short flag" do
    captured = ""
    parser = OptionParser.parse([] of String) do |opts|
      opts.on("-f X", "--flag X", "some flag") { |x| captured = x }
    end
    parser.parse(["-f", "12"])
    captured.should eq "12"
    parser.to_s.should contain "   -f X, --flag X"
  end

  it "does to_s with banner" do
    parser = OptionParser.parse([] of String) do |opts|
      opts.banner = "Usage: foo"
      opts.on("-f", "--flag", "some flag") do
      end
      opts.on("-g[FLAG]", "some other flag") do
      end
    end
    parser.to_s.should eq <<-USAGE
      Usage: foo
          -f, --flag                       some flag
          -g[FLAG]                         some other flag
      USAGE
  end

  it "does to_s with separators" do
    parser = OptionParser.parse([] of String) do |opts|
      opts.banner = "Usage: foo"
      opts.separator
      opts.separator "Type F flags:"
      opts.on("-f", "--flag", "some flag") do
      end
      opts.separator
      opts.separator "Type G flags:"
      opts.on("-g[FLAG]", "some other flag") do
      end
    end
    parser.to_s.should eq <<-USAGE
      Usage: foo

      Type F flags:
          -f, --flag                       some flag

      Type G flags:
          -g[FLAG]                         some other flag
      USAGE
  end

  it "does to_s with very long flag (#3305)" do
    parser = OptionParser.parse([] of String) do |opts|
      opts.banner = "Usage: foo"
      opts.on("--very_long_option_kills=formatter", "long") do
      end
      opts.on("-f", "--flag", "some flag") do
      end
      opts.on("-g[FLAG]", "some other flag") do
      end
    end
    parser.to_s.should eq <<-USAGE
      Usage: foo
          --very_long_option_kills=formatter
                                           long
          -f, --flag                       some flag
          -g[FLAG]                         some other flag
      USAGE
  end

  it "does to_s with multi line description (#5832)" do
    parser = OptionParser.parse([] of String) do |opts|
      opts.banner = "Usage: foo"
      opts.on("--very_long_option_kills=formatter", "long flag with\nmultiline description") do
      end
      opts.on("-f", "--flag", "some flag with\nmultiline description") do
      end
      opts.on("-g[FLAG]", "some other flag") do
      end
    end
    parser.to_s.should eq <<-USAGE
      Usage: foo
          --very_long_option_kills=formatter
                                           long flag with
                                           multiline description
          -f, --flag                       some flag with
                                           multiline description
          -g[FLAG]                         some other flag
      USAGE
  end

  it "raises on invalid option" do
    expect_raises OptionParser::InvalidOption, "Invalid option: -j" do
      OptionParser.parse(["-f", "-j"]) do |opts|
        opts.on("-f", "some flag") { }
      end
    end
  end

  it "raises on invalid option if value is given to none value handler (short flag, #9553) " do
    expect_raises OptionParser::InvalidOption, "Invalid option: -foo" do
      OptionParser.parse(["-foo"]) do |opts|
        opts.on("-f", "some flag") { }
      end
    end
  end

  it "raises on invalid option if value is given to none value handler (long flag, #9553)" do
    expect_raises OptionParser::InvalidOption, "Invalid option: --foo=bar" do
      OptionParser.parse(["--foo=bar"]) do |opts|
        opts.on("-foo", "some flag") { }
      end
    end
  end

  it "calls the handler for invalid options" do
    called = false
    OptionParser.parse(["-f", "-j"]) do |opts|
      opts.on("-f", "some flag") { }
      opts.invalid_option do |flag|
        flag.should eq("-j")
        called = true
      end
    end

    called.should be_true
  end

  it "calls the handler for missing options" do
    called = false
    OptionParser.parse(["-f"]) do |opts|
      opts.on("-f FOO", "some flag") { }
      opts.missing_option do |flag|
        flag.should eq("-f")
        called = true
      end
    end

    called.should be_true
  end

  describe "multiple times" do
    it "gets an existence flag multiple times" do
      args = %w(-f -f -f)
      count = 0
      OptionParser.parse(args) do |opts|
        opts.on("-f", "some flag") do
          count += 1
        end
      end
      count.should eq(3)
    end

    it "gets a single flag option multiple times" do
      args = %w(-f 1 -f 2)
      values = [] of String
      OptionParser.parse(args) do |opts|
        opts.on("-f VALUE", "some flag") do |value|
          values << value
        end
      end
      values.should eq(%w(1 2))
    end

    it "gets a double flag option multiple times" do
      args = %w(--f 1 --f 2)
      values = [] of String
      OptionParser.parse(args) do |opts|
        opts.on("--f VALUE", "some flag") do |value|
          values << value
        end
      end
      values.should eq(%w(1 2))
    end
  end

  describe "--" do
    it "ignores everything after -- with bool flag" do
      args = ["-f", "bar", "--", "baz", "qux", "-g"]
      f = false
      g = false
      OptionParser.parse(args) do |opts|
        opts.on("-f", "some flag") do
          f = true
        end
        opts.on("-g", "some flag") do
          g = true
        end
      end
      f.should be_true
      g.should be_false
      args.should eq(["bar", "baz", "qux", "-g"])
    end

    it "ignores everything after -- with single flag)" do
      args = ["-f", "bar", "x", "--", "baz", "qux", "-g", "lala"]
      f = nil
      g = nil
      OptionParser.parse(args) do |opts|
        opts.on("-f FLAG", "some flag") do |v|
          f = v
        end
        opts.on("-g FLAG", "some flag") do |v|
          g = v
        end
      end
      f.should eq("bar")
      g.should be_nil
      args.should eq(["x", "baz", "qux", "-g", "lala"])
    end

    it "ignores everything after -- with double flag" do
      args = ["--f", "bar", "x", "--", "baz", "qux", "--g", "lala"]
      f = nil
      g = nil
      OptionParser.parse(args) do |opts|
        opts.on("--f FLAG", "some flag") do |v|
          f = v
        end
        opts.on("--g FLAG", "some flag") do |v|
          g = v
        end
      end
      f.should eq("bar")
      g.should be_nil
      args.should eq(["x", "baz", "qux", "--g", "lala"])
    end

    it "returns a pair with things coming before and after --" do
      args = %w(--f bar baz -- qux)
      f = nil
      unknown_args = nil
      OptionParser.parse(args) do |opts|
        opts.on("--f FLAG", "some flag") do |v|
          f = v
        end
        opts.unknown_args do |before_dash, after_dash|
          unknown_args = {before_dash, after_dash}
        end
      end
      f.should eq("bar")
      args.should eq(["baz", "qux"])
      unknown_args.should eq({["baz"], ["qux"]})
    end

    it "returns a pair with things coming before and after --, without --" do
      args = %w(--f bar baz)
      f = nil
      unknown_args = nil
      OptionParser.parse(args) do |opts|
        opts.on("--f FLAG", "some flag") do |v|
          f = v
        end
        opts.unknown_args do |before_dash, after_dash|
          unknown_args = {before_dash, after_dash}
        end
      end
      f.should eq("bar")
      args.should eq(["baz"])
      unknown_args.should eq({["baz"], [] of String})
    end

    it "initializes without block and does parse!" do
      old_argv = ARGV.dup
      begin
        ARGV.clear
        ARGV.concat %w(--f hi)
        f = nil
        OptionParser.new do |opts|
          opts.on("--f FLAG", "some flag") do |v|
            f = v
          end
        end.parse
        f.should eq("hi")
      ensure
        ARGV.clear
        ARGV.concat old_argv
      end
    end

    it "gets `-` as argument" do
      args = %w(-)
      OptionParser.parse(args) do |opts|
      end
      args.should eq(%w(-))
    end
  end

  describe "forward-match" do
    it "distinguishes between '--lamb VALUE' and '--lambda VALUE'" do
      args = %w(--lamb value1 --lambda value2)
      value1 = nil
      value2 = nil
      OptionParser.parse(args) do |opts|
        opts.on("--lamb VALUE", "") { |v| value1 = v }
        opts.on("--lambda VALUE", "") { |v| value2 = v }
      end
      value1.should eq("value1")
      value2.should eq("value2")
    end

    it "distinguishes between '--lamb=VALUE' and '--lambda=VALUE'" do
      args = %w(--lamb=value1 --lambda=value2)
      value1 = nil
      value2 = nil
      OptionParser.parse(args) do |opts|
        opts.on("--lamb=VALUE", "") { |v| value1 = v }
        opts.on("--lambda=VALUE", "") { |v| value2 = v }
      end
      value1.should eq("value1")
      value2.should eq("value2")
    end
  end

  it "raises if flag pair doesn't start with dash (#4001)" do
    OptionParser.parse([] of String) do |opts|
      expect_raises ArgumentError, %(Argument 'short_flag' ("foo") must start with a dash) do
        opts.on("foo", "bar", "baz") { }
      end

      expect_raises ArgumentError, %(Argument 'long_flag' ("bar") must start with a dash) do
        opts.on("-foo", "bar", "baz") { }
      end

      opts.on("", "-bar", "baz") { }
    end
  end

  it "handles subcommands" do
    args = %w(--verbose subcommand --foo 1 --bar sub2 -z)
    verbose = false
    subcommand = false
    foo = nil
    bar = false
    sub2 = false
    z = false
    OptionParser.parse(args) do |opts|
      opts.on("subcommand", "") do
        subcommand = true
        opts.on("--foo arg", "") { |v| foo = v }
        opts.on("--bar", "") { bar = true }
        opts.on("sub2", "") { sub2 = true }
      end
      opts.on("--verbose", "") { verbose = true }
      opts.on("-z", "--baz", "") { z = true }
    end

    verbose.should be_true
    subcommand.should be_true
    foo.should be("1")
    bar.should be_true
    sub2.should be_true
    z.should be_true
  end

  it "parses with subcommands twice" do
    args = %w(--verbose subcommand --foo 1 --bar sub2 -z)
    verbose = false
    subcommand = false
    foo = nil
    bar = false
    sub2 = false
    z = false

    parser = OptionParser.new do |opts|
      opts.on("subcommand", "") do
        subcommand = true
        opts.on("--foo arg", "") { |v| foo = v }
        opts.on("--bar", "") { bar = true }
        opts.on("sub2", "") { sub2 = true }
      end
      opts.on("--verbose", "") { verbose = true }
      opts.on("-z", "--baz", "") { z = true }
    end

    parser.parse args

    verbose.should be_true
    subcommand.should be_true
    foo.should be("1")
    bar.should be_true
    sub2.should be_true
    z.should be_true

    args = %w(--verbose subcommand --foo 1 --bar sub2 -z)
    verbose = false
    subcommand = false
    foo = nil
    bar = false
    sub2 = false
    z = false

    parser.parse args

    verbose.should be_true
    subcommand.should be_true
    foo.should be("1")
    bar.should be_true
    sub2.should be_true
    z.should be_true
  end

  it "unregisters subcommands on call" do
    foo = false
    bar = false
    baz = false
    OptionParser.parse(%w(foo baz)) do |opts|
      opts.on("foo", "") do
        foo = true
        opts.on("bar", "") { bar = true }
      end
      opts.on("baz", "") { baz = true }
    end
    foo.should be_true
    bar.should be_false
    baz.should be_false
  end

  it "handles subcommand --help well (top level)" do
    help = nil
    OptionParser.parse(%w(--help)) do |opts|
      opts.banner = "Usage: foo"
      opts.on("subcommand", "Subcommand Description") do
        opts.on("-f", "--foo", "Foo") { }
      end
      opts.on("--verbose", "Verbose mode") { }
      opts.on("--help", "Help") { help = opts.to_s }
    end

    help.should eq <<-USAGE
      Usage: foo
          subcommand                       Subcommand Description
          --verbose                        Verbose mode
          --help                           Help
      USAGE
  end

  it "handles subcommand --help well (subcommand)" do
    help = nil
    OptionParser.parse(%w(subcommand --help)) do |opts|
      opts.banner = "Usage: foo"
      opts.on("subcommand", "Subcommand Description") do
        opts.banner = "Usage: foo subcommand"
        opts.on("-f", "--foo", "Foo") { }
      end
      opts.on("--verbose", "Verbose mode") { }
      opts.on("--help", "Help") { help = opts.to_s }
    end

    help.should eq <<-USAGE
      Usage: foo subcommand
          --verbose                        Verbose mode
          --help                           Help
          -f, --foo                        Foo
      USAGE
  end

  it "handles subcommands with hyphen" do
    subcommand = false
    OptionParser.parse(%w(sub-command)) do |opts|
      opts.banner = "Usage: foo"
      opts.on("sub-command", "Subcommand description") { subcommand = true }
    end

    subcommand.should be_true
  end

  it "stops when asked" do
    args = %w(--foo --stop --bar)
    foo = false
    bar = false
    OptionParser.parse(args) do |opts|
      opts.on("--foo", "") { foo = true }
      opts.on("--bar", "") { bar = true }
      opts.on("--stop", "") { opts.stop }
      opts.unknown_args do |before, after|
        before.should eq(%w())
        after.should eq(%w(--bar))
      end
    end
    foo.should be_true
    bar.should be_false
    args.should eq(%w(--bar))
  end

  it "can run a callback on every argument" do
    args = %w(--foo file --bar)
    foo = false
    bar = false
    OptionParser.parse(args) do |opts|
      opts.on("--foo", "") { foo = true }
      opts.on("--bar", "") { bar = true }
      opts.before_each do |arg|
        if arg == "file"
          opts.stop
        end
      end
      opts.unknown_args do |before, after|
        before.should eq(%w(file))
        after.should eq(%w(--bar))
      end
    end

    foo.should be_true
    bar.should be_false
    args.should eq(%w(file --bar))
  end
end