Merge branch 'js/add-p-diff-parsing-fix'
[git/debian.git] / t / t0040-parse-options.sh
blobb19b8d3486f774ccc280255fd55f2157aea85b1e
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 cat >expect <<\EOF
12 usage: test-tool parse-options <options>
14 A helper function for the parse-options API.
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
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>
32 String options
33 -s, --string <string>
34 get a string
35 --string2 <str> get another string
36 --st <st> get another string (pervert ordering)
37 -o <str> get another string
38 --list <str> add str to list
40 Magic arguments
41 -NUM set integer to NUM
42 + same as -b
43 --ambiguous positive ambiguity
44 --no-ambiguous negative ambiguity
46 Standard options
47 --abbrev[=<n>] use <n> digits to display object names
48 -v, --verbose be verbose
49 -n, --dry-run dry run
50 -q, --quiet be quiet
51 --expect <string> expected output in the variable dump
53 Alias
54 -A, --alias-source <string>
55 get a string
56 -Z, --alias-target <string>
57 alias of --alias-source
59 EOF
61 test_expect_success 'test help' '
62 test_must_fail test-tool parse-options -h >output 2>output.err &&
63 test_must_be_empty output.err &&
64 test_cmp expect output
67 mv expect expect.err
69 check () {
70 what="$1" &&
71 shift &&
72 expect="$1" &&
73 shift &&
74 test-tool parse-options --expect="$what $expect" "$@"
77 check_unknown_i18n() {
78 case "$1" in
79 --*)
80 echo error: unknown option \`${1#--}\' >expect ;;
81 -*)
82 echo error: unknown switch \`${1#-}\' >expect ;;
83 esac &&
84 cat expect.err >>expect &&
85 test_must_fail test-tool parse-options $* >output 2>output.err &&
86 test_must_be_empty output &&
87 test_cmp expect output.err
90 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
91 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
92 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
93 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
94 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
96 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
97 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
99 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
100 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
102 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
103 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
105 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
107 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
109 test_expect_success 'OPT_MAGNITUDE() simple' '
110 check magnitude: 2345678 -m 2345678
113 test_expect_success 'OPT_MAGNITUDE() kilo' '
114 check magnitude: 239616 -m 234k
117 test_expect_success 'OPT_MAGNITUDE() mega' '
118 check magnitude: 104857600 -m 100m
121 test_expect_success 'OPT_MAGNITUDE() giga' '
122 check magnitude: 1073741824 -m 1g
125 test_expect_success 'OPT_MAGNITUDE() 3giga' '
126 check magnitude: 3221225472 -m 3g
129 cat >expect <<\EOF
130 boolean: 2
131 integer: 1729
132 magnitude: 16384
133 timestamp: 0
134 string: 123
135 abbrev: 7
136 verbose: 2
137 quiet: 0
138 dry run: yes
139 file: prefix/my.file
142 test_expect_success 'short options' '
143 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
144 >output 2>output.err &&
145 test_cmp expect output &&
146 test_must_be_empty output.err
149 cat >expect <<\EOF
150 boolean: 2
151 integer: 1729
152 magnitude: 16384
153 timestamp: 0
154 string: 321
155 abbrev: 10
156 verbose: 2
157 quiet: 0
158 dry run: no
159 file: prefix/fi.le
162 test_expect_success 'long options' '
163 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
164 --boolean --string2=321 --verbose --verbose --no-dry-run \
165 --abbrev=10 --file fi.le --obsolete \
166 >output 2>output.err &&
167 test_must_be_empty output.err &&
168 test_cmp expect output
171 test_expect_success 'missing required value' '
172 cat >expect <<-\EOF &&
173 error: switch `s'\'' requires a value
175 test_expect_code 129 test-tool parse-options -s 2>actual &&
176 test_cmp expect actual &&
178 cat >expect <<-\EOF &&
179 error: option `string'\'' requires a value
181 test_expect_code 129 test-tool parse-options --string 2>actual &&
182 test_cmp expect actual &&
184 cat >expect <<-\EOF &&
185 error: option `file'\'' requires a value
187 test_expect_code 129 test-tool parse-options --file 2>actual &&
188 test_cmp expect actual
191 test_expect_success 'superfluous value provided: boolean' '
192 cat >expect <<-\EOF &&
193 error: option `yes'\'' takes no value
195 test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
196 test_cmp expect actual &&
198 cat >expect <<-\EOF &&
199 error: option `no-yes'\'' takes no value
201 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
202 test_cmp expect actual
205 test_expect_success 'superfluous value provided: cmdmode' '
206 cat >expect <<-\EOF &&
207 error: option `mode1'\'' takes no value
209 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
210 test_cmp expect actual
213 cat >expect <<\EOF
214 boolean: 1
215 integer: 13
216 magnitude: 0
217 timestamp: 0
218 string: 123
219 abbrev: 7
220 verbose: -1
221 quiet: 0
222 dry run: no
223 file: (not set)
224 arg 00: a1
225 arg 01: b1
226 arg 02: --boolean
229 test_expect_success 'intermingled arguments' '
230 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
231 >output 2>output.err &&
232 test_must_be_empty output.err &&
233 test_cmp expect output
236 cat >expect <<\EOF
237 boolean: 0
238 integer: 2
239 magnitude: 0
240 timestamp: 0
241 string: (not set)
242 abbrev: 7
243 verbose: -1
244 quiet: 0
245 dry run: no
246 file: (not set)
249 test_expect_success 'unambiguously abbreviated option' '
250 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
251 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
252 test_must_be_empty output.err &&
253 test_cmp expect output
256 test_expect_success 'unambiguously abbreviated option with "="' '
257 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
258 test-tool parse-options --expect="integer: 2" --int=2
261 test_expect_success 'ambiguously abbreviated option' '
262 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
263 test-tool parse-options --strin 123
266 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
267 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
268 test-tool parse-options --expect="string: 123" --st 123
271 test_expect_success 'Alias options do not contribute to abbreviation' '
272 test-tool parse-options --alias-source 123 >output &&
273 grep "^string: 123" output &&
274 test-tool parse-options --alias-target 123 >output &&
275 grep "^string: 123" output &&
276 test_must_fail test-tool parse-options --alias &&
277 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
278 test-tool parse-options --alias 123 >output &&
279 grep "^string: 123" output
282 cat >typo.err <<\EOF
283 error: did you mean `--boolean` (with two dashes)?
286 test_expect_success 'detect possible typos' '
287 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
288 test_must_be_empty output &&
289 test_cmp typo.err output.err
292 cat >typo.err <<\EOF
293 error: did you mean `--ambiguous` (with two dashes)?
296 test_expect_success 'detect possible typos' '
297 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
298 test_must_be_empty output &&
299 test_cmp typo.err output.err
302 cat >expect <<\EOF
303 Callback: "four", 0
304 boolean: 5
305 integer: 4
306 magnitude: 0
307 timestamp: 0
308 string: (not set)
309 abbrev: 7
310 verbose: -1
311 quiet: 0
312 dry run: no
313 file: (not set)
316 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
317 test-tool parse-options --length=four -b -4 >output 2>output.err &&
318 test_must_be_empty output.err &&
319 test_cmp expect output
322 test_expect_success 'OPT_CALLBACK() and callback errors work' '
323 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
324 test_must_be_empty output &&
325 test_must_be_empty output.err
328 cat >expect <<\EOF
329 boolean: 1
330 integer: 23
331 magnitude: 0
332 timestamp: 0
333 string: (not set)
334 abbrev: 7
335 verbose: -1
336 quiet: 0
337 dry run: no
338 file: (not set)
341 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
342 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
343 test_must_be_empty output.err &&
344 test_cmp expect output
347 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
348 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
349 test_must_be_empty output.err &&
350 test_cmp expect output
353 test_expect_success 'OPT_BIT() works' '
354 test-tool parse-options --expect="boolean: 6" -bb --or4
357 test_expect_success 'OPT_NEGBIT() works' '
358 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
361 test_expect_success 'OPT_CMDMODE() works' '
362 test-tool parse-options --expect="integer: 1" --mode1
365 test_expect_success 'OPT_CMDMODE() detects incompatibility' '
366 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
367 test_must_be_empty output &&
368 test_i18ngrep "incompatible with --mode" output.err
371 test_expect_success 'OPT_CMDMODE() detects incompatibility with something else' '
372 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
373 test_must_be_empty output &&
374 test_i18ngrep "incompatible with something else" output.err
377 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
378 test-tool parse-options --expect="boolean: 6" + + + + + +
381 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
382 test-tool parse-options --expect="integer: 12345" -12345
385 cat >expect <<\EOF
386 boolean: 0
387 integer: 0
388 magnitude: 0
389 timestamp: 0
390 string: (not set)
391 abbrev: 7
392 verbose: -1
393 quiet: 0
394 dry run: no
395 file: (not set)
398 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
399 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
400 test-tool parse-options --no-ambig >output 2>output.err &&
401 test_must_be_empty output.err &&
402 test_cmp expect output
405 cat >>expect <<\EOF
406 list: foo
407 list: bar
408 list: baz
410 test_expect_success '--list keeps list of strings' '
411 test-tool parse-options --list foo --list=bar --list=baz >output &&
412 test_cmp expect output
415 test_expect_success '--no-list resets list' '
416 test-tool parse-options --list=other --list=irrelevant --list=options \
417 --no-list --list=foo --list=bar --list=baz >output &&
418 test_cmp expect output
421 test_expect_success 'multiple quiet levels' '
422 test-tool parse-options --expect="quiet: 3" -q -q -q
425 test_expect_success 'multiple verbose levels' '
426 test-tool parse-options --expect="verbose: 3" -v -v -v
429 test_expect_success '--no-quiet sets --quiet to 0' '
430 test-tool parse-options --expect="quiet: 0" --no-quiet
433 test_expect_success '--no-quiet resets multiple -q to 0' '
434 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
437 test_expect_success '--no-verbose sets verbose to 0' '
438 test-tool parse-options --expect="verbose: 0" --no-verbose
441 test_expect_success '--no-verbose resets multiple verbose to 0' '
442 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
445 test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
446 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
447 test-tool parse-options --ye &&
448 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
449 test-tool parse-options --ye
452 test_expect_success '--end-of-options treats remainder as args' '
453 test-tool parse-options \
454 --expect="verbose: -1" \
455 --expect="arg 00: --verbose" \
456 --end-of-options --verbose
459 test_expect_success 'KEEP_DASHDASH works' '
460 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
461 cat >expect <<-\EOF &&
462 opt: 1
463 arg 00: --
464 arg 01: --opt=2
465 arg 02: --unknown
467 test_cmp expect actual
470 test_expect_success 'KEEP_ARGV0 works' '
471 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
472 cat >expect <<-\EOF &&
473 opt: 3
474 arg 00: cmd
475 arg 01: arg0
477 test_cmp expect actual
480 test_expect_success 'STOP_AT_NON_OPTION works' '
481 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
482 cat >expect <<-\EOF &&
483 opt: 4
484 arg 00: arg0
485 arg 01: --opt=5
486 arg 02: --unknown
488 test_cmp expect actual
491 test_expect_success 'KEEP_UNKNOWN_OPT works' '
492 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
493 cat >expect <<-\EOF &&
494 opt: 6
495 arg 00: --unknown=1
496 arg 01: -u2
498 test_cmp expect actual
501 test_expect_success 'NO_INTERNAL_HELP works for -h' '
502 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
503 cat err &&
504 grep "^error: unknown switch \`h$SQ" err &&
505 grep "^usage: " err
508 for help_opt in help help-all
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 cat err &&
513 grep '^error: unknown option \`'$help_opt\' err &&
514 grep '^usage: ' err
516 done
518 test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
519 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
520 cat >expect <<-\EOF &&
521 opt: 0
522 arg 00: -h
523 arg 01: --help
524 arg 02: --help-all
526 test_cmp expect actual
529 test_expect_success 'subcommand - no subcommand shows error and usage' '
530 test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
531 grep "^error: need a subcommand" err &&
532 grep ^usage: err
535 test_expect_success 'subcommand - subcommand after -- shows error and usage' '
536 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
537 grep "^error: need a subcommand" err &&
538 grep ^usage: err
541 test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
542 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
543 grep "^error: need a subcommand" err &&
544 grep ^usage: err
547 test_expect_success 'subcommand - unknown subcommand shows error and usage' '
548 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
549 grep "^error: unknown subcommand: \`nope$SQ" err &&
550 grep ^usage: err
553 test_expect_success 'subcommand - subcommands cannot be abbreviated' '
554 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
555 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
556 grep ^usage: err
559 test_expect_success 'subcommand - no negated subcommands' '
560 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
561 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
562 grep ^usage: err
565 test_expect_success 'subcommand - simple' '
566 test-tool parse-subcommand cmd subcmd-two >actual &&
567 cat >expect <<-\EOF &&
568 opt: 0
569 fn: subcmd_two
570 arg 00: subcmd-two
572 test_cmp expect actual
575 test_expect_success 'subcommand - stop parsing at the first subcommand' '
576 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
577 cat >expect <<-\EOF &&
578 opt: 1
579 fn: subcmd_two
580 arg 00: subcmd-two
581 arg 01: subcmd-one
582 arg 02: --opt=2
584 test_cmp expect actual
587 test_expect_success 'subcommand - KEEP_ARGV0' '
588 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
589 cat >expect <<-\EOF &&
590 opt: 0
591 fn: subcmd_two
592 arg 00: cmd
593 arg 01: subcmd-two
595 test_cmp expect actual
598 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
599 test-tool parse-subcommand --subcommand-optional cmd >actual &&
600 cat >expect <<-\EOF &&
601 opt: 0
602 fn: subcmd_one
604 test_cmp expect actual
607 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
608 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
609 cat >expect <<-\EOF &&
610 opt: 0
611 fn: subcmd_two
612 arg 00: subcmd-two
613 arg 01: branch
614 arg 02: file
616 test_cmp expect actual
619 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
620 test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
621 cat >expect <<-\EOF &&
622 opt: 0
623 fn: subcmd_one
624 arg 00: branch
625 arg 01: file
627 test_cmp expect actual
630 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
631 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
632 grep "^error: unknown option" err &&
633 grep ^usage: err
636 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
637 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
638 cat >expect <<-\EOF &&
639 opt: 0
640 fn: subcmd_one
641 arg 00: --subcommand-opt
643 test_cmp expect actual
646 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
647 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
648 cat >expect <<-\EOF &&
649 opt: 0
650 fn: subcmd_one
651 arg 00: --subcommand-opt
652 arg 01: subcmd-two
654 test_cmp expect actual
657 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
658 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
659 cat >expect <<-\EOF &&
660 opt: 0
661 fn: subcmd_one
662 arg 00: --subcommand-opt
663 arg 01: branch
664 arg 02: --opt=1
666 test_cmp expect actual
669 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
670 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
671 cat >expect <<-\EOF &&
672 opt: 0
673 fn: subcmd_one
674 arg 00: cmd
675 arg 01: --subcommand-opt
676 arg 02: branch
678 test_cmp expect actual
681 test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
682 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
683 cat >expect <<-\EOF &&
684 opt: 0
685 fn: subcmd_one
686 arg 00: --
687 arg 01: --subcommand-opt
688 arg 02: file
690 test_cmp expect actual
693 test_expect_success 'subcommand - completion helper' '
694 test-tool parse-subcommand cmd --git-completion-helper >actual &&
695 echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
696 test_cmp expect actual
699 test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
700 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
701 grep ^BUG err
704 test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
705 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
706 grep ^BUG err
709 test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
710 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
711 grep ^BUG err
714 test_done