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>
26 --string2 <str> get another string
27 --st <st> get another string (pervert ordering)
28 -o <str> get another string
29 --default-string set string to default
33 -NUM set integer to NUM
37 --abbrev[=<n>] use <n> digits to display SHA-1s
38 -v, --verbose be verbose
44 test_expect_success
'test help' '
45 test_must_fail test-parse-options -h > output 2> output.err &&
47 test_cmp expect.err output.err
61 test_expect_success
'short options' '
62 test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
63 test_cmp expect output &&
78 test_expect_success
'long options' '
79 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
80 --verbose --verbose --no-dry-run --abbrev=10 \
81 > output 2> output.err &&
82 test ! -s output.err &&
83 test_cmp expect output
86 test_expect_success
'missing required value' '
87 test-parse-options -s;
89 test-parse-options --string;
107 test_expect_success
'intermingled arguments' '
108 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
109 > output 2> output.err &&
110 test ! -s output.err &&
111 test_cmp expect output
125 test_expect_success
'unambiguously abbreviated option' '
126 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
127 test ! -s output.err &&
128 test_cmp expect output
131 test_expect_success
'unambiguously abbreviated option with "="' '
132 test-parse-options --int=2 > output 2> output.err &&
133 test ! -s output.err &&
134 test_cmp expect output
137 test_expect_success
'ambiguously abbreviated option' '
138 test-parse-options --strin 123;
153 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
154 test-parse-options --st 123 > output 2> output.err &&
155 test ! -s output.err &&
156 test_cmp expect output
159 cat > typo.err
<< EOF
160 error: did you mean \`--boolean\` (with two dashes ?)
163 test_expect_success
'detect possible typos' '
164 test_must_fail test-parse-options -boolean > output 2> output.err &&
166 test_cmp typo.err output.err
181 test_expect_success
'keep some options as arguments' '
182 test-parse-options --quux > output 2> output.err &&
183 test ! -s output.err &&
184 test_cmp expect output
199 test_expect_success
'OPT_DATE() and OPT_SET_PTR() work' '
200 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
201 foo -q > output 2> output.err &&
202 test ! -s output.err &&
203 test_cmp expect output
218 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
219 test-parse-options --length=four -b -4 > output 2> output.err &&
220 test ! -s output.err &&
221 test_cmp expect output
225 Callback: "not set", 1
228 test_expect_success
'OPT_CALLBACK() and callback errors work' '
229 test_must_fail test-parse-options --no-length > output 2> output.err &&
230 test_cmp expect output &&
231 test_cmp expect.err output.err
245 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
246 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
247 test ! -s output.err &&
248 test_cmp expect output
251 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
252 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
253 test ! -s output.err &&
254 test_cmp expect output
268 test_expect_success
'OPT_BIT() works' '
269 test-parse-options -bb --or4 > output 2> output.err &&
270 test ! -s output.err &&
271 test_cmp expect output
274 test_expect_success
'OPT_NEGBIT() works' '
275 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
276 test ! -s output.err &&
277 test_cmp expect output
280 test_expect_success
'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
281 test-parse-options + + + + + + > output 2> output.err &&
282 test ! -s output.err &&
283 test_cmp expect output
297 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
298 test-parse-options -12345 > output 2> output.err &&
299 test ! -s output.err &&
300 test_cmp expect output