3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
11 usage: test-parse-options <options>
14 -D, --no-doubt begins with 'no-'
15 -B, --no-fear be brave
16 -b, --boolean increment by one
17 -4, --or4 bitwise-or boolean with ...0100
18 --neg-or4 same as --no-or4
20 -i, --integer <n> get a integer
21 -j <n> get a integer, too
22 -m, --magnitude <n> get a magnitude
23 --set23 set integer to 23
24 -t <time> get timestamp of <time>
25 -L, --length <str> get length of <str>
26 -F, --file <file> set file to <file>
31 --string2 <str> get another string
32 --st <st> get another string (pervert ordering)
33 -o <str> get another string
34 --list <str> add str to list
38 -NUM set integer to NUM
40 --ambiguous positive ambiguity
41 --no-ambiguous negative ambiguity
44 --abbrev[=<n>] use <n> digits to display SHA-1s
45 -v, --verbose be verbose
48 --expect <string> expected output in the variable dump
52 test_expect_success 'test help' '
53 test_must_fail test-parse-options -h >output 2>output.err &&
54 test_must_be_empty output.err &&
55 test_i18ncmp expect output
60 cat >expect.template <<\EOF
78 sed "s/^$what .*/$what $expect/" <expect.template >expect &&
79 test-parse-options $* >output 2>output.err &&
80 test_must_be_empty output.err &&
81 test_cmp expect output
84 check_unknown_i18n() {
87 echo error: unknown option \`${1#--}\' >expect ;;
89 echo error: unknown switch \`${1#-}\' >expect ;;
91 cat expect.err >>expect &&
92 test_must_fail test-parse-options $* >output 2>output.err &&
93 test_must_be_empty output &&
94 test_i18ncmp expect output.err
97 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
98 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
99 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
100 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
101 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
103 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
104 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
106 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
107 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
109 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
110 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
112 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
114 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
116 test_expect_success 'OPT_MAGNITUDE() simple' '
117 check magnitude: 2345678 -m 2345678
120 test_expect_success 'OPT_MAGNITUDE() kilo' '
121 check magnitude: 239616 -m 234k
124 test_expect_success 'OPT_MAGNITUDE() mega' '
125 check magnitude: 104857600 -m 100m
128 test_expect_success 'OPT_MAGNITUDE() giga' '
129 check magnitude: 1073741824 -m 1g
132 test_expect_success 'OPT_MAGNITUDE() 3giga' '
133 check magnitude: 3221225472 -m 3g
149 test_expect_success 'short options' '
150 test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
151 >output 2>output.err &&
152 test_cmp expect output &&
153 test_must_be_empty output.err
169 test_expect_success 'long options' '
170 test-parse-options --boolean --integer 1729 --magnitude 16k \
171 --boolean --string2=321 --verbose --verbose --no-dry-run \
172 --abbrev=10 --file fi.le --obsolete \
173 >output 2>output.err &&
174 test_must_be_empty output.err &&
175 test_cmp expect output
178 test_expect_success 'missing required value' '
179 test_expect_code 129 test-parse-options -s &&
180 test_expect_code 129 test-parse-options --string &&
181 test_expect_code 129 test-parse-options --file
200 test_expect_success 'intermingled arguments' '
201 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
202 >output 2>output.err &&
203 test_must_be_empty output.err &&
204 test_cmp expect output
220 test_expect_success 'unambiguously abbreviated option' '
221 test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
222 test_must_be_empty output.err &&
223 test_cmp expect output
226 test_expect_success 'unambiguously abbreviated option with "="' '
227 test-parse-options --int=2 >output 2>output.err &&
228 test_must_be_empty output.err &&
229 test_cmp expect output
232 test_expect_success 'ambiguously abbreviated option' '
233 test_expect_code 129 test-parse-options --strin 123
249 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
250 test-parse-options --st 123 >output 2>output.err &&
251 test_must_be_empty output.err &&
252 test_cmp expect output
256 error: did you mean `--boolean` (with two dashes ?)
259 test_expect_success 'detect possible typos' '
260 test_must_fail test-parse-options -boolean >output 2>output.err &&
261 test_must_be_empty output &&
262 test_cmp typo.err output.err
266 error: did you mean `--ambiguous` (with two dashes ?)
269 test_expect_success 'detect possible typos' '
270 test_must_fail test-parse-options -ambiguous >output 2>output.err &&
271 test_must_be_empty output &&
272 test_cmp typo.err output.err
289 test_expect_success 'keep some options as arguments' '
290 test-parse-options --quux >output 2>output.err &&
291 test_must_be_empty output.err &&
292 test_cmp expect output
309 test_expect_success 'OPT_DATE() works' '
310 test-parse-options -t "1970-01-01 00:00:01 +0000" \
311 foo -q >output 2>output.err &&
312 test_must_be_empty output.err &&
313 test_cmp expect output
330 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
331 test-parse-options --length=four -b -4 >output 2>output.err &&
332 test_must_be_empty output.err &&
333 test_cmp expect output
338 test_expect_success 'OPT_CALLBACK() and callback errors work' '
339 test_must_fail test-parse-options --no-length >output 2>output.err &&
340 test_i18ncmp expect output &&
341 test_i18ncmp expect.err output.err
357 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
358 test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
359 test_must_be_empty output.err &&
360 test_cmp expect output
363 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
364 test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
365 test_must_be_empty output.err &&
366 test_cmp expect output
382 test_expect_success 'OPT_BIT() works' '
383 test-parse-options -bb --or4 >output 2>output.err &&
384 test_must_be_empty output.err &&
385 test_cmp expect output
388 test_expect_success 'OPT_NEGBIT() works' '
389 test-parse-options -bb --no-neg-or4 >output 2>output.err &&
390 test_must_be_empty output.err &&
391 test_cmp expect output
394 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
395 test-parse-options + + + + + + >output 2>output.err &&
396 test_must_be_empty output.err &&
397 test_cmp expect output
413 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
414 test-parse-options -12345 >output 2>output.err &&
415 test_must_be_empty output.err &&
416 test_cmp expect output
432 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
433 test-parse-options --no-ambig >output 2>output.err &&
434 test_must_be_empty output.err &&
435 test_cmp expect output
443 test_expect_success '--list keeps list of strings' '
444 test-parse-options --list foo --list=bar --list=baz >output &&
445 test_cmp expect output
448 test_expect_success '--no-list resets list' '
449 test-parse-options --list=other --list=irrelevant --list=options \
450 --no-list --list=foo --list=bar --list=baz >output &&
451 test_cmp expect output
467 test_expect_success 'multiple quiet levels' '
468 test-parse-options -q -q -q >output 2>output.err &&
469 test_must_be_empty output.err &&
470 test_cmp expect output
486 test_expect_success 'multiple verbose levels' '
487 test-parse-options -v -v -v >output 2>output.err &&
488 test_must_be_empty output.err &&
489 test_cmp expect output
505 test_expect_success '--no-quiet sets --quiet to 0' '
506 test-parse-options --no-quiet >output 2>output.err &&
507 test_must_be_empty output.err &&
508 test_cmp expect output
524 test_expect_success '--no-quiet resets multiple -q to 0' '
525 test-parse-options -q -q -q --no-quiet >output 2>output.err &&
526 test_must_be_empty output.err &&
527 test_cmp expect output
543 test_expect_success '--no-verbose sets verbose to 0' '
544 test-parse-options --no-verbose >output 2>output.err &&
545 test_must_be_empty output.err &&
546 test_cmp expect output
562 test_expect_success '--no-verbose resets multiple verbose to 0' '
563 test-parse-options -v -v -v --no-verbose >output 2>output.err &&
564 test_must_be_empty output.err &&
565 test_cmp expect output