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