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 --default-string set string to default
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 ! -s output.err &&
54 test_cmp expect output
59 cat >expect.template
<<EOF
76 sed "s/^$what .*/$what $expect/" <expect.template
>expect
&&
77 test-parse-options $
* >output
2>output.err
&&
78 test ! -s output.err
&&
79 test_cmp expect output
85 echo error
: unknown option \
`${1#--}\' >expect ;;
87 echo error: unknown switch \`${1#-}\' >expect
;;
89 cat expect.err
>>expect
&&
90 test_must_fail test-parse-options $
* >output
2>output.err
&&
92 test_cmp expect output.err
95 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
96 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
97 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
98 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
99 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
101 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
102 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
104 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
105 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
107 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown --fear'
108 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown --no-no-fear'
110 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
124 test_expect_success
'short options' '
125 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
126 > output 2> output.err &&
127 test_cmp expect output &&
143 test_expect_success
'long options' '
144 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
145 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
146 --obsolete > output 2> output.err &&
147 test ! -s output.err &&
148 test_cmp expect output
151 test_expect_success
'missing required value' '
152 test-parse-options -s;
154 test-parse-options --string;
156 test-parse-options --file;
175 test_expect_success
'intermingled arguments' '
176 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
177 > output 2> output.err &&
178 test ! -s output.err &&
179 test_cmp expect output
194 test_expect_success
'unambiguously abbreviated option' '
195 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
196 test ! -s output.err &&
197 test_cmp expect output
200 test_expect_success
'unambiguously abbreviated option with "="' '
201 test-parse-options --int=2 > output 2> output.err &&
202 test ! -s output.err &&
203 test_cmp expect output
206 test_expect_success
'ambiguously abbreviated option' '
207 test-parse-options --strin 123;
223 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
224 test-parse-options --st 123 > output 2> output.err &&
225 test ! -s output.err &&
226 test_cmp expect output
229 cat > typo.err
<< EOF
230 error: did you mean \`--boolean\` (with two dashes ?)
233 test_expect_success
'detect possible typos' '
234 test_must_fail test-parse-options -boolean > output 2> output.err &&
236 test_cmp typo.err output.err
239 cat > typo.err
<< EOF
240 error: did you mean \`--ambiguous\` (with two dashes ?)
243 test_expect_success
'detect possible typos' '
244 test_must_fail test-parse-options -ambiguous > output 2> output.err &&
246 test_cmp typo.err output.err
262 test_expect_success
'keep some options as arguments' '
263 test-parse-options --quux > output 2> output.err &&
264 test ! -s output.err &&
265 test_cmp expect output
281 test_expect_success
'OPT_DATE() and OPT_SET_PTR() work' '
282 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
283 foo -q > output 2> output.err &&
284 test ! -s output.err &&
285 test_cmp expect output
301 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
302 test-parse-options --length=four -b -4 > output 2> output.err &&
303 test ! -s output.err &&
304 test_cmp expect output
308 Callback: "not set", 1
311 test_expect_success
'OPT_CALLBACK() and callback errors work' '
312 test_must_fail test-parse-options --no-length > output 2> output.err &&
313 test_cmp expect output &&
314 test_cmp expect.err output.err
329 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
330 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
331 test ! -s output.err &&
332 test_cmp expect output
335 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
336 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
337 test ! -s output.err &&
338 test_cmp expect output
353 test_expect_success
'OPT_BIT() works' '
354 test-parse-options -bb --or4 > output 2> output.err &&
355 test ! -s output.err &&
356 test_cmp expect output
359 test_expect_success
'OPT_NEGBIT() works' '
360 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
361 test ! -s output.err &&
362 test_cmp expect output
365 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
366 test-parse-options + + + + + + > output 2> output.err &&
367 test ! -s output.err &&
368 test_cmp expect output
383 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
384 test-parse-options -12345 > output 2> output.err &&
385 test ! -s output.err &&
386 test_cmp expect output
401 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
402 test-parse-options --no-ambig >output 2>output.err &&
403 test ! -s output.err &&
404 test_cmp expect output
412 test_expect_success
'--list keeps list of strings' '
413 test-parse-options --list foo --list=bar --list=baz >output &&
414 test_cmp expect output
417 test_expect_success
'--no-list resets list' '
418 test-parse-options --list=other --list=irrelevant --list=options \
419 --no-list --list=foo --list=bar --list=baz >output &&
420 test_cmp expect output