3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
11 usage: test-tool parse-options <options>
13 A helper function for the parse-options API.
16 -D, --no-doubt begins with 'no-'
17 -B, --no-fear be brave
18 -b, --boolean increment by one
19 -4, --or4 bitwise-or boolean with ...0100
20 --neg-or4 same as --no-or4
22 -i, --integer <n> get a integer
23 -j <n> get a integer, too
24 -m, --magnitude <n> get a magnitude
25 --set23 set integer to 23
26 -L, --length <str> get length of <str>
27 -F, --file <file> set file to <file>
32 --string2 <str> get another string
33 --st <st> get another string (pervert ordering)
34 -o <str> get another string
35 --list <str> add str to list
39 -NUM set integer to NUM
41 --ambiguous positive ambiguity
42 --no-ambiguous negative ambiguity
45 --abbrev[=<n>] use <n> digits to display SHA-1s
46 -v, --verbose be verbose
49 --expect <string> expected output in the variable dump
53 test_expect_success 'test help' '
54 test_must_fail test-tool parse-options -h >output 2>output.err &&
55 test_must_be_empty output.err &&
56 test_i18ncmp expect output
66 test-tool parse-options --expect="$what $expect" "$@"
69 check_unknown_i18n() {
72 echo error: unknown option \`${1#--}\' >expect ;;
74 echo error: unknown switch \`${1#-}\' >expect ;;
76 cat expect.err >>expect &&
77 test_must_fail test-tool parse-options $* >output 2>output.err &&
78 test_must_be_empty output &&
79 test_i18ncmp expect output.err
82 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
83 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
84 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
85 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
86 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
88 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
89 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
91 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
92 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
94 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
95 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
97 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
99 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
101 test_expect_success 'OPT_MAGNITUDE() simple' '
102 check magnitude: 2345678 -m 2345678
105 test_expect_success 'OPT_MAGNITUDE() kilo' '
106 check magnitude: 239616 -m 234k
109 test_expect_success 'OPT_MAGNITUDE() mega' '
110 check magnitude: 104857600 -m 100m
113 test_expect_success 'OPT_MAGNITUDE() giga' '
114 check magnitude: 1073741824 -m 1g
117 test_expect_success 'OPT_MAGNITUDE() 3giga' '
118 check magnitude: 3221225472 -m 3g
134 test_expect_success 'short options' '
135 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
136 >output 2>output.err &&
137 test_cmp expect output &&
138 test_must_be_empty output.err
154 test_expect_success 'long options' '
155 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
156 --boolean --string2=321 --verbose --verbose --no-dry-run \
157 --abbrev=10 --file fi.le --obsolete \
158 >output 2>output.err &&
159 test_must_be_empty output.err &&
160 test_cmp expect output
163 test_expect_success 'missing required value' '
164 test_expect_code 129 test-tool parse-options -s &&
165 test_expect_code 129 test-tool parse-options --string &&
166 test_expect_code 129 test-tool parse-options --file
185 test_expect_success 'intermingled arguments' '
186 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
187 >output 2>output.err &&
188 test_must_be_empty output.err &&
189 test_cmp expect output
205 test_expect_success 'unambiguously abbreviated option' '
206 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
207 test_must_be_empty output.err &&
208 test_cmp expect output
211 test_expect_success 'unambiguously abbreviated option with "="' '
212 test-tool parse-options --expect="integer: 2" --int=2
215 test_expect_success 'ambiguously abbreviated option' '
216 test_expect_code 129 test-tool parse-options --strin 123
219 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
220 test-tool parse-options --expect="string: 123" --st 123
224 error: did you mean `--boolean` (with two dashes ?)
227 test_expect_success 'detect possible typos' '
228 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
229 test_must_be_empty output &&
230 test_i18ncmp typo.err output.err
234 error: did you mean `--ambiguous` (with two dashes ?)
237 test_expect_success 'detect possible typos' '
238 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
239 test_must_be_empty output &&
240 test_i18ncmp typo.err output.err
243 test_expect_success 'keep some options as arguments' '
244 test-tool parse-options --expect="arg 00: --quux" --quux
261 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
262 test-tool parse-options --length=four -b -4 >output 2>output.err &&
263 test_must_be_empty output.err &&
264 test_cmp expect output
267 test_expect_success 'OPT_CALLBACK() and callback errors work' '
268 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
269 test_must_be_empty output &&
270 test_must_be_empty output.err
286 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
287 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
288 test_must_be_empty output.err &&
289 test_cmp expect output
292 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
293 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
294 test_must_be_empty output.err &&
295 test_cmp expect output
298 test_expect_success 'OPT_BIT() works' '
299 test-tool parse-options --expect="boolean: 6" -bb --or4
302 test_expect_success 'OPT_NEGBIT() works' '
303 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
306 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
307 test-tool parse-options --expect="boolean: 6" + + + + + +
310 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
311 test-tool parse-options --expect="integer: 12345" -12345
327 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
328 test-tool parse-options --no-ambig >output 2>output.err &&
329 test_must_be_empty output.err &&
330 test_cmp expect output
338 test_expect_success '--list keeps list of strings' '
339 test-tool parse-options --list foo --list=bar --list=baz >output &&
340 test_cmp expect output
343 test_expect_success '--no-list resets list' '
344 test-tool parse-options --list=other --list=irrelevant --list=options \
345 --no-list --list=foo --list=bar --list=baz >output &&
346 test_cmp expect output
349 test_expect_success 'multiple quiet levels' '
350 test-tool parse-options --expect="quiet: 3" -q -q -q
353 test_expect_success 'multiple verbose levels' '
354 test-tool parse-options --expect="verbose: 3" -v -v -v
357 test_expect_success '--no-quiet sets --quiet to 0' '
358 test-tool parse-options --expect="quiet: 0" --no-quiet
361 test_expect_success '--no-quiet resets multiple -q to 0' '
362 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
365 test_expect_success '--no-verbose sets verbose to 0' '
366 test-tool parse-options --expect="verbose: 0" --no-verbose
369 test_expect_success '--no-verbose resets multiple verbose to 0' '
370 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose