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
36 --abbrev[=<n>] use <n> digits to display SHA-1s
37 -v, --verbose be verbose
43 test_expect_success
'test help' '
44 test_must_fail test-parse-options -h > output 2> output.err &&
46 test_cmp expect.err output.err
60 test_expect_success
'short options' '
61 test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
62 test_cmp expect output &&
77 test_expect_success
'long options' '
78 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
79 --verbose --verbose --no-dry-run --abbrev=10 \
80 > output 2> output.err &&
81 test ! -s output.err &&
82 test_cmp expect output
85 test_expect_success
'missing required value' '
86 test-parse-options -s;
88 test-parse-options --string;
106 test_expect_success
'intermingled arguments' '
107 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
108 > output 2> output.err &&
109 test ! -s output.err &&
110 test_cmp expect output
124 test_expect_success
'unambiguously abbreviated option' '
125 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
126 test ! -s output.err &&
127 test_cmp expect output
130 test_expect_success
'unambiguously abbreviated option with "="' '
131 test-parse-options --int=2 > output 2> output.err &&
132 test ! -s output.err &&
133 test_cmp expect output
136 test_expect_success
'ambiguously abbreviated option' '
137 test-parse-options --strin 123;
152 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
153 test-parse-options --st 123 > output 2> output.err &&
154 test ! -s output.err &&
155 test_cmp expect output
158 cat > typo.err
<< EOF
159 error: did you mean \`--boolean\` (with two dashes ?)
162 test_expect_success
'detect possible typos' '
163 test_must_fail test-parse-options -boolean > output 2> output.err &&
165 test_cmp typo.err output.err
180 test_expect_success
'keep some options as arguments' '
181 test-parse-options --quux > output 2> output.err &&
182 test ! -s output.err &&
183 test_cmp expect output
198 test_expect_success
'OPT_DATE() and OPT_SET_PTR() work' '
199 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
200 foo -q > output 2> output.err &&
201 test ! -s output.err &&
202 test_cmp expect output
217 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
218 test-parse-options --length=four -b -4 > output 2> output.err &&
219 test ! -s output.err &&
220 test_cmp expect output
224 Callback: "not set", 1
227 test_expect_success
'OPT_CALLBACK() and callback errors work' '
228 test_must_fail test-parse-options --no-length > output 2> output.err &&
229 test_cmp expect output &&
230 test_cmp expect.err output.err
244 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
245 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
246 test ! -s output.err &&
247 test_cmp expect output
250 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
251 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
252 test ! -s output.err &&
253 test_cmp expect output
267 test_expect_success
'OPT_BIT() works' '
268 test-parse-options -bb --or4 > output 2> output.err &&
269 test ! -s output.err &&
270 test_cmp expect output
273 test_expect_success
'OPT_NEGBIT() works' '
274 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
275 test ! -s output.err &&
276 test_cmp expect output
290 test_expect_success
'OPT_NUMBER_CALLBACK() works' '
291 test-parse-options -12345 > output 2> output.err &&
292 test ! -s output.err &&
293 test_cmp expect output