]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0040-parse-options.sh
Merge branch 'mp/rebase-label-length-limit' into maint-2.42
[thirdparty/git.git] / t / t0040-parse-options.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes Schindelin
4 #
5
6 test_description='our own option parser'
7
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 cat >expect <<\EOF
12 usage: test-tool parse-options <options>
13
14 A helper function for the parse-options API.
15
16 --yes get a boolean
17 -D, --no-doubt begins with 'no-'
18 -B, --no-fear be brave
19 -b, --boolean increment by one
20 -4, --or4 bitwise-or boolean with ...0100
21 --neg-or4 same as --no-or4
22
23 -i, --integer <n> get a integer
24 -j <n> get a integer, too
25 -m, --magnitude <n> get a magnitude
26 --set23 set integer to 23
27 --mode1 set integer to 1 (cmdmode option)
28 --mode2 set integer to 2 (cmdmode option)
29 -L, --length <str> get length of <str>
30 -F, --file <file> set file to <file>
31
32 String options
33 -s, --string <string> get a string
34 --string2 <str> get another string
35 --st <st> get another string (pervert ordering)
36 -o <str> get another string
37 --longhelp help text of this entry
38 spans multiple lines
39 --list <str> add str to list
40
41 Magic arguments
42 -NUM set integer to NUM
43 + same as -b
44 --ambiguous positive ambiguity
45 --no-ambiguous negative ambiguity
46
47 Standard options
48 --abbrev[=<n>] use <n> digits to display object names
49 -v, --verbose be verbose
50 -n, --dry-run dry run
51 -q, --quiet be quiet
52 --expect <string> expected output in the variable dump
53
54 Alias
55 -A, --alias-source <string>
56 get a string
57 -Z, --alias-target <string>
58 alias of --alias-source
59
60 EOF
61
62 test_expect_success 'test help' '
63 test_must_fail test-tool parse-options -h >output 2>output.err &&
64 test_must_be_empty output.err &&
65 test_cmp expect output
66 '
67
68 mv expect expect.err
69
70 check () {
71 what="$1" &&
72 shift &&
73 expect="$1" &&
74 shift &&
75 test-tool parse-options --expect="$what $expect" "$@"
76 }
77
78 check_unknown_i18n() {
79 case "$1" in
80 --*)
81 echo error: unknown option \`${1#--}\' >expect ;;
82 -*)
83 echo error: unknown switch \`${1#-}\' >expect ;;
84 esac &&
85 cat expect.err >>expect &&
86 test_must_fail test-tool parse-options $* >output 2>output.err &&
87 test_must_be_empty output &&
88 test_cmp expect output.err
89 }
90
91 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
92 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
93 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
94 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
95 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
96
97 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
98 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
99
100 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
101 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
102
103 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
104 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
105
106 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
107
108 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
109
110 test_expect_success 'OPT_MAGNITUDE() simple' '
111 check magnitude: 2345678 -m 2345678
112 '
113
114 test_expect_success 'OPT_MAGNITUDE() kilo' '
115 check magnitude: 239616 -m 234k
116 '
117
118 test_expect_success 'OPT_MAGNITUDE() mega' '
119 check magnitude: 104857600 -m 100m
120 '
121
122 test_expect_success 'OPT_MAGNITUDE() giga' '
123 check magnitude: 1073741824 -m 1g
124 '
125
126 test_expect_success 'OPT_MAGNITUDE() 3giga' '
127 check magnitude: 3221225472 -m 3g
128 '
129
130 cat >expect <<\EOF
131 boolean: 2
132 integer: 1729
133 magnitude: 16384
134 timestamp: 0
135 string: 123
136 abbrev: 7
137 verbose: 2
138 quiet: 0
139 dry run: yes
140 file: prefix/my.file
141 EOF
142
143 test_expect_success 'short options' '
144 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
145 >output 2>output.err &&
146 test_cmp expect output &&
147 test_must_be_empty output.err
148 '
149
150 cat >expect <<\EOF
151 boolean: 2
152 integer: 1729
153 magnitude: 16384
154 timestamp: 0
155 string: 321
156 abbrev: 10
157 verbose: 2
158 quiet: 0
159 dry run: no
160 file: prefix/fi.le
161 EOF
162
163 test_expect_success 'long options' '
164 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
165 --boolean --string2=321 --verbose --verbose --no-dry-run \
166 --abbrev=10 --file fi.le --obsolete \
167 >output 2>output.err &&
168 test_must_be_empty output.err &&
169 test_cmp expect output
170 '
171
172 test_expect_success 'missing required value' '
173 cat >expect <<-\EOF &&
174 error: switch `s'\'' requires a value
175 EOF
176 test_expect_code 129 test-tool parse-options -s 2>actual &&
177 test_cmp expect actual &&
178
179 cat >expect <<-\EOF &&
180 error: option `string'\'' requires a value
181 EOF
182 test_expect_code 129 test-tool parse-options --string 2>actual &&
183 test_cmp expect actual &&
184
185 cat >expect <<-\EOF &&
186 error: option `file'\'' requires a value
187 EOF
188 test_expect_code 129 test-tool parse-options --file 2>actual &&
189 test_cmp expect actual
190 '
191
192 test_expect_success 'superfluous value provided: boolean' '
193 cat >expect <<-\EOF &&
194 error: option `yes'\'' takes no value
195 EOF
196 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
197 test_cmp expect actual &&
198
199 cat >expect <<-\EOF &&
200 error: option `no-yes'\'' takes no value
201 EOF
202 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
203 test_cmp expect actual
204 '
205
206 test_expect_success 'superfluous value provided: cmdmode' '
207 cat >expect <<-\EOF &&
208 error: option `mode1'\'' takes no value
209 EOF
210 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
211 test_cmp expect actual
212 '
213
214 cat >expect <<\EOF
215 boolean: 1
216 integer: 13
217 magnitude: 0
218 timestamp: 0
219 string: 123
220 abbrev: 7
221 verbose: -1
222 quiet: 0
223 dry run: no
224 file: (not set)
225 arg 00: a1
226 arg 01: b1
227 arg 02: --boolean
228 EOF
229
230 test_expect_success 'intermingled arguments' '
231 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
232 >output 2>output.err &&
233 test_must_be_empty output.err &&
234 test_cmp expect output
235 '
236
237 cat >expect <<\EOF
238 boolean: 0
239 integer: 2
240 magnitude: 0
241 timestamp: 0
242 string: (not set)
243 abbrev: 7
244 verbose: -1
245 quiet: 0
246 dry run: no
247 file: (not set)
248 EOF
249
250 test_expect_success 'unambiguously abbreviated option' '
251 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
252 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
253 test_must_be_empty output.err &&
254 test_cmp expect output
255 '
256
257 test_expect_success 'unambiguously abbreviated option with "="' '
258 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
259 test-tool parse-options --expect="integer: 2" --int=2
260 '
261
262 test_expect_success 'ambiguously abbreviated option' '
263 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
264 test-tool parse-options --strin 123
265 '
266
267 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
268 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
269 test-tool parse-options --expect="string: 123" --st 123
270 '
271
272 test_expect_success 'Alias options do not contribute to abbreviation' '
273 test-tool parse-options --alias-source 123 >output &&
274 grep "^string: 123" output &&
275 test-tool parse-options --alias-target 123 >output &&
276 grep "^string: 123" output &&
277 test_must_fail test-tool parse-options --alias &&
278 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
279 test-tool parse-options --alias 123 >output &&
280 grep "^string: 123" output
281 '
282
283 cat >typo.err <<\EOF
284 error: did you mean `--boolean` (with two dashes)?
285 EOF
286
287 test_expect_success 'detect possible typos' '
288 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
289 test_must_be_empty output &&
290 test_cmp typo.err output.err
291 '
292
293 cat >typo.err <<\EOF
294 error: did you mean `--ambiguous` (with two dashes)?
295 EOF
296
297 test_expect_success 'detect possible typos' '
298 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
299 test_must_be_empty output &&
300 test_cmp typo.err output.err
301 '
302
303 cat >expect <<\EOF
304 Callback: "four", 0
305 boolean: 5
306 integer: 4
307 magnitude: 0
308 timestamp: 0
309 string: (not set)
310 abbrev: 7
311 verbose: -1
312 quiet: 0
313 dry run: no
314 file: (not set)
315 EOF
316
317 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
318 test-tool parse-options --length=four -b -4 >output 2>output.err &&
319 test_must_be_empty output.err &&
320 test_cmp expect output
321 '
322
323 test_expect_success 'OPT_CALLBACK() and callback errors work' '
324 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
325 test_must_be_empty output &&
326 test_must_be_empty output.err
327 '
328
329 cat >expect <<\EOF
330 boolean: 1
331 integer: 23
332 magnitude: 0
333 timestamp: 0
334 string: (not set)
335 abbrev: 7
336 verbose: -1
337 quiet: 0
338 dry run: no
339 file: (not set)
340 EOF
341
342 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
343 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
344 test_must_be_empty output.err &&
345 test_cmp expect output
346 '
347
348 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
349 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
350 test_must_be_empty output.err &&
351 test_cmp expect output
352 '
353
354 test_expect_success 'OPT_BIT() works' '
355 test-tool parse-options --expect="boolean: 6" -bb --or4
356 '
357
358 test_expect_success 'OPT_NEGBIT() works' '
359 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
360 '
361
362 test_expect_success 'OPT_CMDMODE() works' '
363 test-tool parse-options --expect="integer: 1" --mode1
364 '
365
366 test_expect_success 'OPT_CMDMODE() detects incompatibility' '
367 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
368 test_must_be_empty output &&
369 test_i18ngrep "incompatible with --mode" output.err
370 '
371
372 test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' '
373 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
374 test_must_be_empty output &&
375 test_i18ngrep "incompatible with something else" output.err
376 '
377
378 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
379 test-tool parse-options --expect="boolean: 6" + + + + + +
380 '
381
382 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
383 test-tool parse-options --expect="integer: 12345" -12345
384 '
385
386 cat >expect <<\EOF
387 boolean: 0
388 integer: 0
389 magnitude: 0
390 timestamp: 0
391 string: (not set)
392 abbrev: 7
393 verbose: -1
394 quiet: 0
395 dry run: no
396 file: (not set)
397 EOF
398
399 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
400 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
401 test-tool parse-options --no-ambig >output 2>output.err &&
402 test_must_be_empty output.err &&
403 test_cmp expect output
404 '
405
406 cat >>expect <<\EOF
407 list: foo
408 list: bar
409 list: baz
410 EOF
411 test_expect_success '--list keeps list of strings' '
412 test-tool parse-options --list foo --list=bar --list=baz >output &&
413 test_cmp expect output
414 '
415
416 test_expect_success '--no-list resets list' '
417 test-tool parse-options --list=other --list=irrelevant --list=options \
418 --no-list --list=foo --list=bar --list=baz >output &&
419 test_cmp expect output
420 '
421
422 test_expect_success 'multiple quiet levels' '
423 test-tool parse-options --expect="quiet: 3" -q -q -q
424 '
425
426 test_expect_success 'multiple verbose levels' '
427 test-tool parse-options --expect="verbose: 3" -v -v -v
428 '
429
430 test_expect_success '--no-quiet sets --quiet to 0' '
431 test-tool parse-options --expect="quiet: 0" --no-quiet
432 '
433
434 test_expect_success '--no-quiet resets multiple -q to 0' '
435 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
436 '
437
438 test_expect_success '--no-verbose sets verbose to 0' '
439 test-tool parse-options --expect="verbose: 0" --no-verbose
440 '
441
442 test_expect_success '--no-verbose resets multiple verbose to 0' '
443 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
444 '
445
446 test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
447 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
448 test-tool parse-options --ye &&
449 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
450 test-tool parse-options --ye
451 '
452
453 test_expect_success '--end-of-options treats remainder as args' '
454 test-tool parse-options \
455 --expect="verbose: -1" \
456 --expect="arg 00: --verbose" \
457 --end-of-options --verbose
458 '
459
460 test_expect_success 'KEEP_DASHDASH works' '
461 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
462 cat >expect <<-\EOF &&
463 opt: 1
464 arg 00: --
465 arg 01: --opt=2
466 arg 02: --unknown
467 EOF
468 test_cmp expect actual
469 '
470
471 test_expect_success 'KEEP_ARGV0 works' '
472 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
473 cat >expect <<-\EOF &&
474 opt: 3
475 arg 00: cmd
476 arg 01: arg0
477 EOF
478 test_cmp expect actual
479 '
480
481 test_expect_success 'STOP_AT_NON_OPTION works' '
482 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
483 cat >expect <<-\EOF &&
484 opt: 4
485 arg 00: arg0
486 arg 01: --opt=5
487 arg 02: --unknown
488 EOF
489 test_cmp expect actual
490 '
491
492 test_expect_success 'KEEP_UNKNOWN_OPT works' '
493 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
494 cat >expect <<-\EOF &&
495 opt: 6
496 arg 00: --unknown=1
497 arg 01: -u2
498 EOF
499 test_cmp expect actual
500 '
501
502 test_expect_success 'NO_INTERNAL_HELP works for -h' '
503 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
504 grep "^error: unknown switch \`h$SQ" err &&
505 grep "^usage: " err
506 '
507
508 for help_opt in help help-all
509 do
510 test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
511 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
512 grep '^error: unknown option \`'$help_opt\' err &&
513 grep '^usage: ' err
514 "
515 done
516
517 test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
518 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
519 cat >expect <<-\EOF &&
520 opt: 0
521 arg 00: -h
522 arg 01: --help
523 arg 02: --help-all
524 EOF
525 test_cmp expect actual
526 '
527
528 test_expect_success 'subcommand - no subcommand shows error and usage' '
529 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
530 grep "^error: need a subcommand" err &&
531 grep ^usage: err
532 '
533
534 test_expect_success 'subcommand - subcommand after -- shows error and usage' '
535 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
536 grep "^error: need a subcommand" err &&
537 grep ^usage: err
538 '
539
540 test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
541 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
542 grep "^error: need a subcommand" err &&
543 grep ^usage: err
544 '
545
546 test_expect_success 'subcommand - unknown subcommand shows error and usage' '
547 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
548 grep "^error: unknown subcommand: \`nope$SQ" err &&
549 grep ^usage: err
550 '
551
552 test_expect_success 'subcommand - subcommands cannot be abbreviated' '
553 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
554 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
555 grep ^usage: err
556 '
557
558 test_expect_success 'subcommand - no negated subcommands' '
559 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
560 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
561 grep ^usage: err
562 '
563
564 test_expect_success 'subcommand - simple' '
565 test-tool parse-subcommand cmd subcmd-two >actual &&
566 cat >expect <<-\EOF &&
567 opt: 0
568 fn: subcmd_two
569 arg 00: subcmd-two
570 EOF
571 test_cmp expect actual
572 '
573
574 test_expect_success 'subcommand - stop parsing at the first subcommand' '
575 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
576 cat >expect <<-\EOF &&
577 opt: 1
578 fn: subcmd_two
579 arg 00: subcmd-two
580 arg 01: subcmd-one
581 arg 02: --opt=2
582 EOF
583 test_cmp expect actual
584 '
585
586 test_expect_success 'subcommand - KEEP_ARGV0' '
587 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
588 cat >expect <<-\EOF &&
589 opt: 0
590 fn: subcmd_two
591 arg 00: cmd
592 arg 01: subcmd-two
593 EOF
594 test_cmp expect actual
595 '
596
597 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
598 test-tool parse-subcommand --subcommand-optional cmd >actual &&
599 cat >expect <<-\EOF &&
600 opt: 0
601 fn: subcmd_one
602 EOF
603 test_cmp expect actual
604 '
605
606 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
607 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
608 cat >expect <<-\EOF &&
609 opt: 0
610 fn: subcmd_two
611 arg 00: subcmd-two
612 arg 01: branch
613 arg 02: file
614 EOF
615 test_cmp expect actual
616 '
617
618 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
619 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
620 cat >expect <<-\EOF &&
621 opt: 0
622 fn: subcmd_one
623 arg 00: branch
624 arg 01: file
625 EOF
626 test_cmp expect actual
627 '
628
629 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
630 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
631 grep "^error: unknown option" err &&
632 grep ^usage: err
633 '
634
635 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
636 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
637 cat >expect <<-\EOF &&
638 opt: 0
639 fn: subcmd_one
640 arg 00: --subcommand-opt
641 EOF
642 test_cmp expect actual
643 '
644
645 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
646 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
647 cat >expect <<-\EOF &&
648 opt: 0
649 fn: subcmd_one
650 arg 00: --subcommand-opt
651 arg 01: subcmd-two
652 EOF
653 test_cmp expect actual
654 '
655
656 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
657 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
658 cat >expect <<-\EOF &&
659 opt: 0
660 fn: subcmd_one
661 arg 00: --subcommand-opt
662 arg 01: branch
663 arg 02: --opt=1
664 EOF
665 test_cmp expect actual
666 '
667
668 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
669 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
670 cat >expect <<-\EOF &&
671 opt: 0
672 fn: subcmd_one
673 arg 00: cmd
674 arg 01: --subcommand-opt
675 arg 02: branch
676 EOF
677 test_cmp expect actual
678 '
679
680 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
681 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
682 cat >expect <<-\EOF &&
683 opt: 0
684 fn: subcmd_one
685 arg 00: --
686 arg 01: --subcommand-opt
687 arg 02: file
688 EOF
689 test_cmp expect actual
690 '
691
692 test_expect_success 'subcommand - completion helper' '
693 test-tool parse-subcommand cmd --git-completion-helper >actual &&
694 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
695 test_cmp expect actual
696 '
697
698 test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
699 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
700 grep ^BUG err
701 '
702
703 test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
704 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
705 grep ^BUG err
706 '
707
708 test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
709 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
710 grep ^BUG err
711 '
712
713 test_expect_success 'negative magnitude' '
714 test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
715 grep "non-negative integer" err &&
716 test_must_be_empty out
717 '
718
719 test_expect_success 'magnitude with units but no numbers' '
720 test_must_fail test-tool parse-options --magnitude m >out 2>err &&
721 grep "non-negative integer" err &&
722 test_must_be_empty out
723 '
724
725 test_done