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
51 test_expect_success
'test help' '
52 test_must_fail test-parse-options -h >output 2>output.err &&
53 test_must_be_empty output.err &&
54 test_i18ncmp expect output
59 cat >expect.template
<<\EOF
77 sed "s/^$what .*/$what $expect/" <expect.template
>expect
&&
78 test-parse-options $
* >output
2>output.err
&&
79 test_must_be_empty output.err
&&
80 test_cmp expect output
88 sed "s/^$what .*/$what $expect/" <expect.template
>expect
&&
89 test-parse-options $
* >output
2>output.err
&&
90 test_must_be_empty output.err
&&
91 test_i18ncmp expect output
97 echo error
: unknown option \
`${1#--}\' >expect ;;
99 echo error: unknown switch \`${1#-}\' >expect
;;
101 cat expect.err
>>expect
&&
102 test_must_fail test-parse-options $
* >output
2>output.err
&&
103 test_must_be_empty output
&&
104 test_cmp expect output.err
107 check_unknown_i18n
() {
110 echo error
: unknown option \
`${1#--}\' >expect ;;
112 echo error: unknown switch \`${1#-}\' >expect
;;
114 cat expect.err
>>expect
&&
115 test_must_fail test-parse-options $
* >output
2>output.err
&&
116 test_must_be_empty output
&&
117 test_i18ncmp expect output.err
120 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
121 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
122 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
123 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
124 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
126 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
127 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
129 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
130 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
132 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
133 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
135 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
137 test_expect_success
'OPT_INT() negative' 'check integer: -2345 -i -2345'
139 test_expect_success
'OPT_MAGNITUDE() simple' '
140 check magnitude: 2345678 -m 2345678
143 test_expect_success
'OPT_MAGNITUDE() kilo' '
144 check magnitude: 239616 -m 234k
147 test_expect_success
'OPT_MAGNITUDE() mega' '
148 check magnitude: 104857600 -m 100m
151 test_expect_success
'OPT_MAGNITUDE() giga' '
152 check magnitude: 1073741824 -m 1g
155 test_expect_success
'OPT_MAGNITUDE() 3giga' '
156 check magnitude: 3221225472 -m 3g
172 test_expect_success
'short options' '
173 test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
174 >output 2>output.err &&
175 test_cmp expect output &&
176 test_must_be_empty output.err
192 test_expect_success
'long options' '
193 test-parse-options --boolean --integer 1729 --magnitude 16k \
194 --boolean --string2=321 --verbose --verbose --no-dry-run \
195 --abbrev=10 --file fi.le --obsolete \
196 >output 2>output.err &&
197 test_must_be_empty output.err &&
198 test_cmp expect output
201 test_expect_success
'missing required value' '
202 test_expect_code 129 test-parse-options -s &&
203 test_expect_code 129 test-parse-options --string &&
204 test_expect_code 129 test-parse-options --file
223 test_expect_success
'intermingled arguments' '
224 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
225 >output 2>output.err &&
226 test_must_be_empty output.err &&
227 test_cmp expect output
243 test_expect_success
'unambiguously abbreviated option' '
244 test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
245 test_must_be_empty output.err &&
246 test_cmp expect output
249 test_expect_success
'unambiguously abbreviated option with "="' '
250 test-parse-options --int=2 >output 2>output.err &&
251 test_must_be_empty output.err &&
252 test_cmp expect output
255 test_expect_success
'ambiguously abbreviated option' '
256 test_expect_code 129 test-parse-options --strin 123
272 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
273 test-parse-options --st 123 >output 2>output.err &&
274 test_must_be_empty output.err &&
275 test_cmp expect output
279 error
: did you mean
`--boolean` (with two dashes ?
)
282 test_expect_success
'detect possible typos' '
283 test_must_fail test-parse-options -boolean >output 2>output.err &&
284 test_must_be_empty output &&
285 test_cmp typo.err output.err
289 error
: did you mean
`--ambiguous` (with two dashes ?
)
292 test_expect_success
'detect possible typos' '
293 test_must_fail test-parse-options -ambiguous >output 2>output.err &&
294 test_must_be_empty output &&
295 test_cmp typo.err output.err
312 test_expect_success
'keep some options as arguments' '
313 test-parse-options --quux >output 2>output.err &&
314 test_must_be_empty output.err &&
315 test_cmp expect output
332 test_expect_success
'OPT_DATE() works' '
333 test-parse-options -t "1970-01-01 00:00:01 +0000" \
334 foo -q >output 2>output.err &&
335 test_must_be_empty output.err &&
336 test_cmp expect output
353 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
354 test-parse-options --length=four -b -4 >output 2>output.err &&
355 test_must_be_empty output.err &&
356 test_cmp expect output
361 test_expect_success
'OPT_CALLBACK() and callback errors work' '
362 test_must_fail test-parse-options --no-length >output 2>output.err &&
363 test_i18ncmp expect output &&
364 test_i18ncmp expect.err output.err
380 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
381 test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
382 test_must_be_empty output.err &&
383 test_cmp expect output
386 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
387 test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
388 test_must_be_empty output.err &&
389 test_cmp expect output
405 test_expect_success
'OPT_BIT() works' '
406 test-parse-options -bb --or4 >output 2>output.err &&
407 test_must_be_empty output.err &&
408 test_cmp expect output
411 test_expect_success
'OPT_NEGBIT() works' '
412 test-parse-options -bb --no-neg-or4 >output 2>output.err &&
413 test_must_be_empty output.err &&
414 test_cmp expect output
417 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
418 test-parse-options + + + + + + >output 2>output.err &&
419 test_must_be_empty output.err &&
420 test_cmp expect output
436 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
437 test-parse-options -12345 >output 2>output.err &&
438 test_must_be_empty output.err &&
439 test_cmp expect output
455 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
456 test-parse-options --no-ambig >output 2>output.err &&
457 test_must_be_empty output.err &&
458 test_cmp expect output
466 test_expect_success
'--list keeps list of strings' '
467 test-parse-options --list foo --list=bar --list=baz >output &&
468 test_cmp expect output
471 test_expect_success
'--no-list resets list' '
472 test-parse-options --list=other --list=irrelevant --list=options \
473 --no-list --list=foo --list=bar --list=baz >output &&
474 test_cmp expect output
490 test_expect_success
'multiple quiet levels' '
491 test-parse-options -q -q -q >output 2>output.err &&
492 test_must_be_empty output.err &&
493 test_cmp expect output
509 test_expect_success
'multiple verbose levels' '
510 test-parse-options -v -v -v >output 2>output.err &&
511 test_must_be_empty output.err &&
512 test_cmp expect output
528 test_expect_success
'--no-quiet sets --quiet to 0' '
529 test-parse-options --no-quiet >output 2>output.err &&
530 test_must_be_empty output.err &&
531 test_cmp expect output
547 test_expect_success
'--no-quiet resets multiple -q to 0' '
548 test-parse-options -q -q -q --no-quiet >output 2>output.err &&
549 test_must_be_empty output.err &&
550 test_cmp expect output
566 test_expect_success
'--no-verbose sets verbose to 0' '
567 test-parse-options --no-verbose >output 2>output.err &&
568 test_must_be_empty output.err &&
569 test_cmp expect output
585 test_expect_success
'--no-verbose resets multiple verbose to 0' '
586 test-parse-options -v -v -v --no-verbose >output 2>output.err &&
587 test_must_be_empty output.err &&
588 test_cmp expect output