Seventh batch for 2.20
[git/debian.git] / t / t0040-parse-options.sh
blob17d0c18feb84e2c47167b9b54f71f192d78b0e9b
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 -t <time> get timestamp of <time>
27 -L, --length <str> get length of <str>
28 -F, --file <file> set file to <file>
30 String options
31 -s, --string <string>
32 get a string
33 --string2 <str> get another string
34 --st <st> get another string (pervert ordering)
35 -o <str> get another string
36 --list <str> add str to list
38 Magic arguments
39 --quux means --quux
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 SHA-1s
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 EOF
54 test_expect_success 'test help' '
55 test_must_fail test-tool parse-options -h >output 2>output.err &&
56 test_must_be_empty output.err &&
57 test_i18ncmp expect output
60 mv expect expect.err
62 check () {
63 what="$1" &&
64 shift &&
65 expect="$1" &&
66 shift &&
67 test-tool parse-options --expect="$what $expect" "$@"
70 check_unknown_i18n() {
71 case "$1" in
72 --*)
73 echo error: unknown option \`${1#--}\' >expect ;;
74 -*)
75 echo error: unknown switch \`${1#-}\' >expect ;;
76 esac &&
77 cat expect.err >>expect &&
78 test_must_fail test-tool parse-options $* >output 2>output.err &&
79 test_must_be_empty output &&
80 test_i18ncmp expect output.err
83 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
84 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
85 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
86 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
87 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
89 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
90 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
92 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
93 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
95 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
96 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
98 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
100 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
102 test_expect_success 'OPT_MAGNITUDE() simple' '
103 check magnitude: 2345678 -m 2345678
106 test_expect_success 'OPT_MAGNITUDE() kilo' '
107 check magnitude: 239616 -m 234k
110 test_expect_success 'OPT_MAGNITUDE() mega' '
111 check magnitude: 104857600 -m 100m
114 test_expect_success 'OPT_MAGNITUDE() giga' '
115 check magnitude: 1073741824 -m 1g
118 test_expect_success 'OPT_MAGNITUDE() 3giga' '
119 check magnitude: 3221225472 -m 3g
122 cat >expect <<\EOF
123 boolean: 2
124 integer: 1729
125 magnitude: 16384
126 timestamp: 0
127 string: 123
128 abbrev: 7
129 verbose: 2
130 quiet: 0
131 dry run: yes
132 file: prefix/my.file
135 test_expect_success 'short options' '
136 test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
137 >output 2>output.err &&
138 test_cmp expect output &&
139 test_must_be_empty output.err
142 cat >expect <<\EOF
143 boolean: 2
144 integer: 1729
145 magnitude: 16384
146 timestamp: 0
147 string: 321
148 abbrev: 10
149 verbose: 2
150 quiet: 0
151 dry run: no
152 file: prefix/fi.le
155 test_expect_success 'long options' '
156 test-tool parse-options --boolean --integer 1729 --magnitude 16k \
157 --boolean --string2=321 --verbose --verbose --no-dry-run \
158 --abbrev=10 --file fi.le --obsolete \
159 >output 2>output.err &&
160 test_must_be_empty output.err &&
161 test_cmp expect output
164 test_expect_success 'missing required value' '
165 test_expect_code 129 test-tool parse-options -s &&
166 test_expect_code 129 test-tool parse-options --string &&
167 test_expect_code 129 test-tool parse-options --file
170 cat >expect <<\EOF
171 boolean: 1
172 integer: 13
173 magnitude: 0
174 timestamp: 0
175 string: 123
176 abbrev: 7
177 verbose: -1
178 quiet: 0
179 dry run: no
180 file: (not set)
181 arg 00: a1
182 arg 01: b1
183 arg 02: --boolean
186 test_expect_success 'intermingled arguments' '
187 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
188 >output 2>output.err &&
189 test_must_be_empty output.err &&
190 test_cmp expect output
193 cat >expect <<\EOF
194 boolean: 0
195 integer: 2
196 magnitude: 0
197 timestamp: 0
198 string: (not set)
199 abbrev: 7
200 verbose: -1
201 quiet: 0
202 dry run: no
203 file: (not set)
206 test_expect_success 'unambiguously abbreviated option' '
207 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
208 test_must_be_empty output.err &&
209 test_cmp expect output
212 test_expect_success 'unambiguously abbreviated option with "="' '
213 test-tool parse-options --expect="integer: 2" --int=2
216 test_expect_success 'ambiguously abbreviated option' '
217 test_expect_code 129 test-tool parse-options --strin 123
220 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
221 test-tool parse-options --expect="string: 123" --st 123
224 cat >typo.err <<\EOF
225 error: did you mean `--boolean` (with two dashes ?)
228 test_expect_success 'detect possible typos' '
229 test_must_fail test-tool parse-options -boolean >output 2>output.err &&
230 test_must_be_empty output &&
231 test_cmp typo.err output.err
234 cat >typo.err <<\EOF
235 error: did you mean `--ambiguous` (with two dashes ?)
238 test_expect_success 'detect possible typos' '
239 test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
240 test_must_be_empty output &&
241 test_cmp typo.err output.err
244 test_expect_success 'keep some options as arguments' '
245 test-tool parse-options --expect="arg 00: --quux" --quux
248 cat >expect <<\EOF
249 boolean: 0
250 integer: 0
251 magnitude: 0
252 timestamp: 1
253 string: (not set)
254 abbrev: 7
255 verbose: -1
256 quiet: 1
257 dry run: no
258 file: (not set)
259 arg 00: foo
262 test_expect_success 'OPT_DATE() works' '
263 test-tool parse-options -t "1970-01-01 00:00:01 +0000" \
264 foo -q >output 2>output.err &&
265 test_must_be_empty output.err &&
266 test_cmp expect output
269 cat >expect <<\EOF
270 Callback: "four", 0
271 boolean: 5
272 integer: 4
273 magnitude: 0
274 timestamp: 0
275 string: (not set)
276 abbrev: 7
277 verbose: -1
278 quiet: 0
279 dry run: no
280 file: (not set)
283 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
284 test-tool parse-options --length=four -b -4 >output 2>output.err &&
285 test_must_be_empty output.err &&
286 test_cmp expect output
289 test_expect_success 'OPT_CALLBACK() and callback errors work' '
290 test_must_fail test-tool parse-options --no-length >output 2>output.err &&
291 test_must_be_empty output &&
292 test_must_be_empty output.err
295 cat >expect <<\EOF
296 boolean: 1
297 integer: 23
298 magnitude: 0
299 timestamp: 0
300 string: (not set)
301 abbrev: 7
302 verbose: -1
303 quiet: 0
304 dry run: no
305 file: (not set)
308 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
309 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
310 test_must_be_empty output.err &&
311 test_cmp expect output
314 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
315 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
316 test_must_be_empty output.err &&
317 test_cmp expect output
320 test_expect_success 'OPT_BIT() works' '
321 test-tool parse-options --expect="boolean: 6" -bb --or4
324 test_expect_success 'OPT_NEGBIT() works' '
325 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
328 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
329 test-tool parse-options --expect="boolean: 6" + + + + + +
332 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
333 test-tool parse-options --expect="integer: 12345" -12345
336 cat >expect <<\EOF
337 boolean: 0
338 integer: 0
339 magnitude: 0
340 timestamp: 0
341 string: (not set)
342 abbrev: 7
343 verbose: -1
344 quiet: 0
345 dry run: no
346 file: (not set)
349 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
350 test-tool parse-options --no-ambig >output 2>output.err &&
351 test_must_be_empty output.err &&
352 test_cmp expect output
355 cat >>expect <<\EOF
356 list: foo
357 list: bar
358 list: baz
360 test_expect_success '--list keeps list of strings' '
361 test-tool parse-options --list foo --list=bar --list=baz >output &&
362 test_cmp expect output
365 test_expect_success '--no-list resets list' '
366 test-tool parse-options --list=other --list=irrelevant --list=options \
367 --no-list --list=foo --list=bar --list=baz >output &&
368 test_cmp expect output
371 test_expect_success 'multiple quiet levels' '
372 test-tool parse-options --expect="quiet: 3" -q -q -q
375 test_expect_success 'multiple verbose levels' '
376 test-tool parse-options --expect="verbose: 3" -v -v -v
379 test_expect_success '--no-quiet sets --quiet to 0' '
380 test-tool parse-options --expect="quiet: 0" --no-quiet
383 test_expect_success '--no-quiet resets multiple -q to 0' '
384 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
387 test_expect_success '--no-verbose sets verbose to 0' '
388 test-tool parse-options --expect="verbose: 0" --no-verbose
391 test_expect_success '--no-verbose resets multiple verbose to 0' '
392 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
395 test_done