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 --set23 set integer to 23
23 -t <time> get timestamp of <time>
24 -L, --length <str> get length of <str>
25 -F, --file <file> set file to <file>
30 --string2 <str> get another string
31 --st <st> get another string (pervert ordering)
32 -o <str> get another string
33 --list <str> add str to list
37 -NUM set integer to NUM
39 --ambiguous positive ambiguity
40 --no-ambiguous negative ambiguity
43 --abbrev[=<n>] use <n> digits to display SHA-1s
44 -v, --verbose be verbose
50 test_expect_success
'test help' '
51 test_must_fail test-parse-options -h > output 2> output.err &&
52 test_must_be_empty output.err &&
53 test_i18ncmp expect output
58 cat >expect.template
<<EOF
75 sed "s/^$what .*/$what $expect/" <expect.template
>expect
&&
76 test-parse-options $
* >output
2>output.err
&&
77 test_must_be_empty output.err
&&
78 test_cmp expect output
86 sed "s/^$what .*/$what $expect/" <expect.template
>expect
&&
87 test-parse-options $
* >output
2>output.err
&&
88 test_must_be_empty output.err
&&
89 test_i18ncmp expect output
95 echo error
: unknown option \
`${1#--}\' >expect ;;
97 echo error: unknown switch \`${1#-}\' >expect
;;
99 cat expect.err
>>expect
&&
100 test_must_fail test-parse-options $
* >output
2>output.err
&&
101 test_must_be_empty output
&&
102 test_cmp expect output.err
105 check_unknown_i18n
() {
108 echo error
: unknown option \
`${1#--}\' >expect ;;
110 echo error: unknown switch \`${1#-}\' >expect
;;
112 cat expect.err
>>expect
&&
113 test_must_fail test-parse-options $
* >output
2>output.err
&&
114 test_must_be_empty output
&&
115 test_i18ncmp expect output.err
118 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
119 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
120 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
121 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
122 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
124 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
125 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
127 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
128 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
130 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
131 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
133 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
147 test_expect_success
'short options' '
148 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
149 > output 2> output.err &&
150 test_cmp expect output &&
151 test_must_be_empty output.err
166 test_expect_success
'long options' '
167 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
168 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
169 --obsolete > output 2> output.err &&
170 test_must_be_empty output.err &&
171 test_cmp expect output
174 test_expect_success
'missing required value' '
175 test_expect_code 129 test-parse-options -s &&
176 test_expect_code 129 test-parse-options --string &&
177 test_expect_code 129 test-parse-options --file
195 test_expect_success
'intermingled arguments' '
196 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
197 > output 2> output.err &&
198 test_must_be_empty output.err &&
199 test_cmp expect output
214 test_expect_success
'unambiguously abbreviated option' '
215 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
216 test_must_be_empty output.err &&
217 test_cmp expect output
220 test_expect_success
'unambiguously abbreviated option with "="' '
221 test-parse-options --int=2 > output 2> output.err &&
222 test_must_be_empty output.err &&
223 test_cmp expect output
226 test_expect_success
'ambiguously abbreviated option' '
227 test_expect_code 129 test-parse-options --strin 123
242 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
243 test-parse-options --st 123 > output 2> output.err &&
244 test_must_be_empty output.err &&
245 test_cmp expect output
248 cat > typo.err
<< EOF
249 error: did you mean \`--boolean\` (with two dashes ?)
252 test_expect_success
'detect possible typos' '
253 test_must_fail test-parse-options -boolean > output 2> output.err &&
254 test_must_be_empty output &&
255 test_cmp typo.err output.err
258 cat > typo.err
<< EOF
259 error: did you mean \`--ambiguous\` (with two dashes ?)
262 test_expect_success
'detect possible typos' '
263 test_must_fail test-parse-options -ambiguous > output 2> output.err &&
264 test_must_be_empty output &&
265 test_cmp typo.err output.err
281 test_expect_success
'keep some options as arguments' '
282 test-parse-options --quux > output 2> output.err &&
283 test_must_be_empty output.err &&
284 test_cmp expect output
300 test_expect_success
'OPT_DATE() works' '
301 test-parse-options -t "1970-01-01 00:00:01 +0000" \
302 foo -q > output 2> output.err &&
303 test_must_be_empty output.err &&
304 test_cmp expect output
320 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
321 test-parse-options --length=four -b -4 > output 2> output.err &&
322 test_must_be_empty output.err &&
323 test_cmp expect output
327 Callback: "not set", 1
330 test_expect_success
'OPT_CALLBACK() and callback errors work' '
331 test_must_fail test-parse-options --no-length > output 2> output.err &&
332 test_i18ncmp expect output &&
333 test_i18ncmp expect.err output.err
348 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
349 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
350 test_must_be_empty output.err &&
351 test_cmp expect output
354 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
355 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
356 test_must_be_empty output.err &&
357 test_cmp expect output
372 test_expect_success
'OPT_BIT() works' '
373 test-parse-options -bb --or4 > output 2> output.err &&
374 test_must_be_empty output.err &&
375 test_cmp expect output
378 test_expect_success
'OPT_NEGBIT() works' '
379 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
380 test_must_be_empty output.err &&
381 test_cmp expect output
384 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
385 test-parse-options + + + + + + > output 2> output.err &&
386 test_must_be_empty output.err &&
387 test_cmp expect output
402 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
403 test-parse-options -12345 > output 2> output.err &&
404 test_must_be_empty output.err &&
405 test_cmp expect output
420 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
421 test-parse-options --no-ambig >output 2>output.err &&
422 test_must_be_empty output.err &&
423 test_cmp expect output
431 test_expect_success
'--list keeps list of strings' '
432 test-parse-options --list foo --list=bar --list=baz >output &&
433 test_cmp expect output
436 test_expect_success
'--no-list resets list' '
437 test-parse-options --list=other --list=irrelevant --list=options \
438 --no-list --list=foo --list=bar --list=baz >output &&
439 test_cmp expect output