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
40 -NUM set integer to NUM
42 --ambiguous positive ambiguity
43 --no-ambiguous negative ambiguity
46 --abbrev[=<n
>] use
<n
> digits to display object names
47 -v, --verbose be verbose
50 --expect <string
> expected output
in the variable dump
53 -A, --alias-source <string
>
55 -Z, --alias-target <string
>
56 alias of
--alias-source
60 test_expect_success
'test help' '
61 test_must_fail test-tool parse-options -h >output 2>output.err &&
62 test_must_be_empty output.err &&
63 test_cmp expect output
73 test-tool parse-options
--expect="$what $expect" "$@"
76 check_unknown_i18n
() {
79 echo error
: unknown option \
`${1#--}\' >expect ;;
81 echo error: unknown switch \`${1#-}\' >expect
;;
83 cat expect.err
>>expect
&&
84 test_must_fail test-tool parse-options $
* >output
2>output.err
&&
85 test_must_be_empty output
&&
86 test_cmp expect output.err
89 test_expect_success
'OPT_BOOL() #1' 'check boolean: 1 --yes'
90 test_expect_success
'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
91 test_expect_success
'OPT_BOOL() #3' 'check boolean: 1 -D'
92 test_expect_success
'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
93 test_expect_success
'OPT_BOOL() #5' 'check boolean: 1 -B'
95 test_expect_success
'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
96 test_expect_success
'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
98 test_expect_success
'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
99 test_expect_success
'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
101 test_expect_success
'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
102 test_expect_success
'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
104 test_expect_success
'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
106 test_expect_success
'OPT_INT() negative' 'check integer: -2345 -i -2345'
108 test_expect_success
'OPT_MAGNITUDE() simple' '
109 check magnitude: 2345678 -m 2345678
112 test_expect_success
'OPT_MAGNITUDE() kilo' '
113 check magnitude: 239616 -m 234k
116 test_expect_success
'OPT_MAGNITUDE() mega' '
117 check magnitude: 104857600 -m 100m
120 test_expect_success
'OPT_MAGNITUDE() giga' '
121 check magnitude: 1073741824 -m 1g
124 test_expect_success
'OPT_MAGNITUDE() 3giga' '
125 check magnitude: 3221225472 -m 3g
141 test_expect_success
'short options' '
142 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
143 >output 2>output.err &&
144 test_cmp expect output &&
145 test_must_be_empty output.err
161 test_expect_success
'long options' '
162 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
163 --boolean --string2=321 --verbose --verbose --no-dry-run \
164 --abbrev=10 --file fi.le --obsolete \
165 >output 2>output.err &&
166 test_must_be_empty output.err &&
167 test_cmp expect output
170 test_expect_success
'missing required value' '
171 test_expect_code 129 test-tool parse-options -s &&
172 test_expect_code 129 test-tool parse-options --string &&
173 test_expect_code 129 test-tool parse-options --file
192 test_expect_success
'intermingled arguments' '
193 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
194 >output 2>output.err &&
195 test_must_be_empty output.err &&
196 test_cmp expect output
212 test_expect_success
'unambiguously abbreviated option' '
213 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
214 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
215 test_must_be_empty output.err &&
216 test_cmp expect output
219 test_expect_success
'unambiguously abbreviated option with "="' '
220 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
221 test-tool parse-options --expect="integer: 2" --int=2
224 test_expect_success
'ambiguously abbreviated option' '
225 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
226 test-tool parse-options --strin 123
229 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
230 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
231 test-tool parse-options --expect="string: 123" --st 123
234 test_expect_success
'Alias options do not contribute to abbreviation' '
235 test-tool parse-options --alias-source 123 >output &&
236 grep "^string: 123" output &&
237 test-tool parse-options --alias-target 123 >output &&
238 grep "^string: 123" output &&
239 test_must_fail test-tool parse-options --alias &&
240 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
241 test-tool parse-options --alias 123 >output &&
242 grep "^string: 123" output
246 error
: did you mean
`--boolean` (with two dashes
)?
249 test_expect_success
'detect possible typos' '
250 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
251 test_must_be_empty output &&
252 test_cmp typo.err output.err
256 error
: did you mean
`--ambiguous` (with two dashes
)?
259 test_expect_success
'detect possible typos' '
260 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
261 test_must_be_empty output &&
262 test_cmp typo.err output.err
279 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
280 test-tool parse-options --length=four -b -4 >output 2>output.err &&
281 test_must_be_empty output.err &&
282 test_cmp expect output
285 test_expect_success
'OPT_CALLBACK() and callback errors work' '
286 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
287 test_must_be_empty output &&
288 test_must_be_empty output.err
304 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
305 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
306 test_must_be_empty output.err &&
307 test_cmp expect output
310 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
311 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
312 test_must_be_empty output.err &&
313 test_cmp expect output
316 test_expect_success
'OPT_BIT() works' '
317 test-tool parse-options --expect="boolean: 6" -bb --or4
320 test_expect_success
'OPT_NEGBIT() works' '
321 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
324 test_expect_success
'OPT_CMDMODE() works' '
325 test-tool parse-options --expect="integer: 1" --mode1
328 test_expect_success
'OPT_CMDMODE() detects incompatibility' '
329 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
330 test_must_be_empty output &&
331 test_i18ngrep "incompatible with --mode" output.err
334 test_expect_success
'OPT_CMDMODE() detects incompatibility with something else' '
335 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
336 test_must_be_empty output &&
337 test_i18ngrep "incompatible with something else" output.err
340 test_expect_success
'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
341 test-tool parse-options --expect="boolean: 6" + + + + + +
344 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
345 test-tool parse-options --expect="integer: 12345" -12345
361 test_expect_success
'negation of OPT_NONEG flags is not ambiguous' '
362 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
363 test-tool parse-options --no-ambig >output 2>output.err &&
364 test_must_be_empty output.err &&
365 test_cmp expect output
373 test_expect_success
'--list keeps list of strings' '
374 test-tool parse-options --list foo --list=bar --list=baz >output &&
375 test_cmp expect output
378 test_expect_success
'--no-list resets list' '
379 test-tool parse-options --list=other --list=irrelevant --list=options \
380 --no-list --list=foo --list=bar --list=baz >output &&
381 test_cmp expect output
384 test_expect_success
'multiple quiet levels' '
385 test-tool parse-options --expect="quiet: 3" -q -q -q
388 test_expect_success
'multiple verbose levels' '
389 test-tool parse-options --expect="verbose: 3" -v -v -v
392 test_expect_success
'--no-quiet sets --quiet to 0' '
393 test-tool parse-options --expect="quiet: 0" --no-quiet
396 test_expect_success
'--no-quiet resets multiple -q to 0' '
397 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
400 test_expect_success
'--no-verbose sets verbose to 0' '
401 test-tool parse-options --expect="verbose: 0" --no-verbose
404 test_expect_success
'--no-verbose resets multiple verbose to 0' '
405 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
408 test_expect_success
'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
409 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
410 test-tool parse-options --ye &&
411 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
412 test-tool parse-options --ye
415 test_expect_success
'--end-of-options treats remainder as args' '
416 test-tool parse-options \
417 --expect="verbose: -1" \
418 --expect="arg 00: --verbose" \
419 --end-of-options --verbose