reset: make sparse-aware (except --mixed)
[git.git] / t / t0040-parse-options.sh
blobda310ed29b15b320916a0eea8181e3b4c5211f64
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 . ./test-lib.sh
10 cat >expect <<\EOF
11 usage: test-tool parse-options <options>
13 A helper function for the parse-options API.
15 --yes get a boolean
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>
31 String options
32 -s, --string <string>
33 get a string
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
39 Magic arguments
40 -NUM set integer to NUM
41 + same as -b
42 --ambiguous positive ambiguity
43 --no-ambiguous negative ambiguity
45 Standard options
46 --abbrev[=<n>] use <n> digits to display object names
47 -v, --verbose be verbose
48 -n, --dry-run dry run
49 -q, --quiet be quiet
50 --expect <string> expected output in the variable dump
52 Alias
53 -A, --alias-source <string>
54 get a string
55 -Z, --alias-target <string>
56 alias of --alias-source
58 EOF
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
66 mv expect expect.err
68 check () {
69 what="$1" &&
70 shift &&
71 expect="$1" &&
72 shift &&
73 test-tool parse-options --expect="$what $expect" "$@"
76 check_unknown_i18n() {
77 case "$1" in
78 --*)
79 echo error: unknown option \`${1#--}\' >expect ;;
80 -*)
81 echo error: unknown switch \`${1#-}\' >expect ;;
82 esac &&
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
128 cat >expect <<\EOF
129 boolean: 2
130 integer: 1729
131 magnitude: 16384
132 timestamp: 0
133 string: 123
134 abbrev: 7
135 verbose: 2
136 quiet: 0
137 dry run: yes
138 file: prefix/my.file
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
148 cat >expect <<\EOF
149 boolean: 2
150 integer: 1729
151 magnitude: 16384
152 timestamp: 0
153 string: 321
154 abbrev: 10
155 verbose: 2
156 quiet: 0
157 dry run: no
158 file: prefix/fi.le
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
176 cat >expect <<\EOF
177 boolean: 1
178 integer: 13
179 magnitude: 0
180 timestamp: 0
181 string: 123
182 abbrev: 7
183 verbose: -1
184 quiet: 0
185 dry run: no
186 file: (not set)
187 arg 00: a1
188 arg 01: b1
189 arg 02: --boolean
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
199 cat >expect <<\EOF
200 boolean: 0
201 integer: 2
202 magnitude: 0
203 timestamp: 0
204 string: (not set)
205 abbrev: 7
206 verbose: -1
207 quiet: 0
208 dry run: no
209 file: (not set)
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
245 cat >typo.err <<\EOF
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
255 cat >typo.err <<\EOF
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
265 cat >expect <<\EOF
266 Callback: "four", 0
267 boolean: 5
268 integer: 4
269 magnitude: 0
270 timestamp: 0
271 string: (not set)
272 abbrev: 7
273 verbose: -1
274 quiet: 0
275 dry run: no
276 file: (not set)
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
291 cat >expect <<\EOF
292 boolean: 1
293 integer: 23
294 magnitude: 0
295 timestamp: 0
296 string: (not set)
297 abbrev: 7
298 verbose: -1
299 quiet: 0
300 dry run: no
301 file: (not set)
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
348 cat >expect <<\EOF
349 boolean: 0
350 integer: 0
351 magnitude: 0
352 timestamp: 0
353 string: (not set)
354 abbrev: 7
355 verbose: -1
356 quiet: 0
357 dry run: no
358 file: (not set)
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
368 cat >>expect <<\EOF
369 list: foo
370 list: bar
371 list: baz
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
422 test_done