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 --mode1 set integer to
1 (cmdmode option
)
27 --mode2 set integer to
2 (cmdmode option
)
28 -L, --length <str
> get length of
<str
>
29 -F, --file <file> set file to
<file>
34 --string2 <str
> get another string
35 --st <st
> get another string
(pervert ordering
)
36 -o <str
> get another string
37 --list <str
> add str to list
41 -NUM set integer to NUM
43 --ambiguous positive ambiguity
44 --no-ambiguous negative ambiguity
47 --abbrev[=<n
>] use
<n
> digits to display object names
48 -v, --verbose be verbose
51 --expect <string
> expected output
in the variable dump
54 -A, --alias-source <string
>
56 -Z, --alias-target <string
>
57 alias of
--alias-source
61 test_expect_success
'test help' '
62 test_must_fail test-tool parse-options -h >output 2>output.err &&
63 test_must_be_empty output.err &&
64 test_i18ncmp expect output
74 test-tool parse-options
--expect="$what $expect" "$@"
77 check_unknown_i18n
() {
80 echo error
: unknown option \
`${1#--}\' >expect ;;
82 echo error: unknown switch \`${1#-}\' >expect
;;
84 cat expect.err
>>expect
&&
85 test_must_fail test-tool parse-options $
* >output
2>output.err
&&
86 test_must_be_empty output
&&
87 test_i18ncmp expect output.err
90 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
91 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
92 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
93 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
94 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
96 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
97 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
99 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
100 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
102 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
103 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
105 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
107 test_expect_success
'OPT_INT() negative' 'check integer: -2345 -i -2345'
109 test_expect_success
'OPT_MAGNITUDE() simple' '
110 check magnitude: 2345678 -m 2345678
113 test_expect_success
'OPT_MAGNITUDE() kilo' '
114 check magnitude: 239616 -m 234k
117 test_expect_success
'OPT_MAGNITUDE() mega' '
118 check magnitude: 104857600 -m 100m
121 test_expect_success
'OPT_MAGNITUDE() giga' '
122 check magnitude: 1073741824 -m 1g
125 test_expect_success
'OPT_MAGNITUDE() 3giga' '
126 check magnitude: 3221225472 -m 3g
142 test_expect_success
'short options' '
143 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
144 >output 2>output.err &&
145 test_cmp expect output &&
146 test_must_be_empty output.err
162 test_expect_success
'long options' '
163 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
164 --boolean --string2=321 --verbose --verbose --no-dry-run \
165 --abbrev=10 --file fi.le --obsolete \
166 >output 2>output.err &&
167 test_must_be_empty output.err &&
168 test_cmp expect output
171 test_expect_success
'missing required value' '
172 test_expect_code 129 test-tool parse-options -s &&
173 test_expect_code 129 test-tool parse-options --string &&
174 test_expect_code 129 test-tool parse-options --file
193 test_expect_success
'intermingled arguments' '
194 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
195 >output 2>output.err &&
196 test_must_be_empty output.err &&
197 test_cmp expect output
213 test_expect_success
'unambiguously abbreviated option' '
214 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
215 test-tool 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 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
222 test-tool parse-options --expect="integer: 2" --int=2
225 test_expect_success
'ambiguously abbreviated option' '
226 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
227 test-tool parse-options --strin 123
230 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
231 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
232 test-tool parse-options --expect="string: 123" --st 123
235 test_expect_success
'Alias options do not contribute to abbreviation' '
236 test-tool parse-options --alias-source 123 >output &&
237 grep "^string: 123" output &&
238 test-tool parse-options --alias-target 123 >output &&
239 grep "^string: 123" output &&
240 test_must_fail test-tool parse-options --alias &&
241 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
242 test-tool parse-options --alias 123 >output &&
243 grep "^string: 123" output
247 error
: did you mean
`--boolean` (with two dashes
)?
250 test_expect_success
'detect possible typos' '
251 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
252 test_must_be_empty output &&
253 test_i18ncmp typo.err output.err
257 error
: did you mean
`--ambiguous` (with two dashes
)?
260 test_expect_success
'detect possible typos' '
261 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
262 test_must_be_empty output &&
263 test_i18ncmp typo.err output.err
266 test_expect_success
'keep some options as arguments' '
267 test-tool parse-options --expect="arg 00: --quux" --quux
284 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
285 test-tool parse-options --length=four -b -4 >output 2>output.err &&
286 test_must_be_empty output.err &&
287 test_cmp expect output
290 test_expect_success
'OPT_CALLBACK() and callback errors work' '
291 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
292 test_must_be_empty output &&
293 test_must_be_empty output.err
309 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
310 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
311 test_must_be_empty output.err &&
312 test_cmp expect output
315 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
316 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
317 test_must_be_empty output.err &&
318 test_cmp expect output
321 test_expect_success
'OPT_BIT() works' '
322 test-tool parse-options --expect="boolean: 6" -bb --or4
325 test_expect_success
'OPT_NEGBIT() works' '
326 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
329 test_expect_success
'OPT_CMDMODE() works' '
330 test-tool parse-options --expect="integer: 1" --mode1
333 test_expect_success
'OPT_CMDMODE() detects incompatibility' '
334 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
335 test_must_be_empty output &&
336 test_i18ngrep "incompatible with --mode" output.err
339 test_expect_success
'OPT_CMDMODE() detects incompatibility with something else' '
340 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
341 test_must_be_empty output &&
342 test_i18ngrep "incompatible with something else" output.err
345 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
346 test-tool parse-options --expect="boolean: 6" + + + + + +
349 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
350 test-tool parse-options --expect="integer: 12345" -12345
366 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
367 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
368 test-tool parse-options --no-ambig >output 2>output.err &&
369 test_must_be_empty output.err &&
370 test_cmp expect output
378 test_expect_success
'--list keeps list of strings' '
379 test-tool parse-options --list foo --list=bar --list=baz >output &&
380 test_cmp expect output
383 test_expect_success
'--no-list resets list' '
384 test-tool parse-options --list=other --list=irrelevant --list=options \
385 --no-list --list=foo --list=bar --list=baz >output &&
386 test_cmp expect output
389 test_expect_success
'multiple quiet levels' '
390 test-tool parse-options --expect="quiet: 3" -q -q -q
393 test_expect_success
'multiple verbose levels' '
394 test-tool parse-options --expect="verbose: 3" -v -v -v
397 test_expect_success
'--no-quiet sets --quiet to 0' '
398 test-tool parse-options --expect="quiet: 0" --no-quiet
401 test_expect_success
'--no-quiet resets multiple -q to 0' '
402 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
405 test_expect_success
'--no-verbose sets verbose to 0' '
406 test-tool parse-options --expect="verbose: 0" --no-verbose
409 test_expect_success
'--no-verbose resets multiple verbose to 0' '
410 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
413 test_expect_success
'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
414 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
415 test-tool parse-options --ye &&
416 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
417 test-tool parse-options --ye
420 test_expect_success
'--end-of-options treats remainder as args' '
421 test-tool parse-options \
422 --expect="verbose: -1" \
423 --expect="arg 00: --verbose" \
424 --end-of-options --verbose