3 # Copyright (c) 2007 Johannes Schindelin
6 test_description
='our own option parser'
10 cat > expect.err
<< EOF
11 usage: test-parse-options <options>
13 -b, --boolean get a boolean
14 -4, --or4 bitwise-or boolean with ...0100
15 --neg-or4 same as --no-or4
17 -i, --integer <n> get a integer
18 -j <n> get a integer, too
19 --set23 set integer to 23
20 -t <time> get timestamp of <time>
21 -L, --length <str> get length of <str>
22 -F, --file <FILE> set file to <FILE>
27 --string2 <str> get another string
28 --st <st> get another string (pervert ordering)
29 -o <str> get another string
30 --default-string set string to default
34 -NUM set integer to NUM
38 --abbrev[=<n>] use <n> digits to display SHA-1s
39 -v, --verbose be verbose
45 test_expect_success
'test help' '
46 test_must_fail test-parse-options -h > output 2> output.err &&
48 test_cmp expect.err output.err
63 test_expect_success
'short options' '
64 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
65 > output 2> output.err &&
66 test_cmp expect output &&
82 test_expect_success
'long options' '
83 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
84 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
85 > output 2> output.err &&
86 test ! -s output.err &&
87 test_cmp expect output
90 test_expect_success
'missing required value' '
91 test-parse-options -s;
93 test-parse-options --string;
95 test-parse-options --file;
114 test_expect_success
'intermingled arguments' '
115 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
116 > output 2> output.err &&
117 test ! -s output.err &&
118 test_cmp expect output
133 test_expect_success
'unambiguously abbreviated option' '
134 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
135 test ! -s output.err &&
136 test_cmp expect output
139 test_expect_success
'unambiguously abbreviated option with "="' '
140 test-parse-options --int=2 > output 2> output.err &&
141 test ! -s output.err &&
142 test_cmp expect output
145 test_expect_success
'ambiguously abbreviated option' '
146 test-parse-options --strin 123;
162 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
163 test-parse-options --st 123 > output 2> output.err &&
164 test ! -s output.err &&
165 test_cmp expect output
168 cat > typo.err
<< EOF
169 error: did you mean \`--boolean\` (with two dashes ?)
172 test_expect_success
'detect possible typos' '
173 test_must_fail test-parse-options -boolean > output 2> output.err &&
175 test_cmp typo.err output.err
191 test_expect_success
'keep some options as arguments' '
192 test-parse-options --quux > output 2> output.err &&
193 test ! -s output.err &&
194 test_cmp expect output
210 test_expect_success
'OPT_DATE() and OPT_SET_PTR() work' '
211 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
212 foo -q > output 2> output.err &&
213 test ! -s output.err &&
214 test_cmp expect output
230 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
231 test-parse-options --length=four -b -4 > output 2> output.err &&
232 test ! -s output.err &&
233 test_cmp expect output
237 Callback: "not set", 1
240 test_expect_success
'OPT_CALLBACK() and callback errors work' '
241 test_must_fail test-parse-options --no-length > output 2> output.err &&
242 test_cmp expect output &&
243 test_cmp expect.err output.err
258 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
259 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
260 test ! -s output.err &&
261 test_cmp expect output
264 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
265 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
266 test ! -s output.err &&
267 test_cmp expect output
282 test_expect_success
'OPT_BIT() works' '
283 test-parse-options -bb --or4 > output 2> output.err &&
284 test ! -s output.err &&
285 test_cmp expect output
288 test_expect_success
'OPT_NEGBIT() works' '
289 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
290 test ! -s output.err &&
291 test_cmp expect output
294 test_expect_success
'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
295 test-parse-options + + + + + + > output 2> output.err &&
296 test ! -s output.err &&
297 test_cmp expect output
312 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
313 test-parse-options -12345 > output 2> output.err &&
314 test ! -s output.err &&
315 test_cmp expect output