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