dir: convert to strbuf
[alt-git.git] / t / t0040-parse-options.sh
blobae266147b6db9e06abeb4dc30e4271b6c611deac
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-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>
24 String options
25 -s, --string <string>
26 get a string
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
32 Magic arguments
33 --quux means --quux
34 -NUM set integer to NUM
35 + same as -b
36 --ambiguous positive ambiguity
37 --no-ambiguous negative ambiguity
39 Standard options
40 --abbrev[=<n>] use <n> digits to display SHA-1s
41 -v, --verbose be verbose
42 -n, --dry-run dry run
43 -q, --quiet be quiet
45 EOF
47 test_expect_success 'test help' '
48 test_must_fail test-parse-options -h > output 2> output.err &&
49 test ! -s output.err &&
50 test_cmp expect output
53 mv expect expect.err
55 cat > expect << EOF
56 boolean: 2
57 integer: 1729
58 timestamp: 0
59 string: 123
60 abbrev: 7
61 verbose: 2
62 quiet: no
63 dry run: yes
64 file: prefix/my.file
65 EOF
67 test_expect_success 'short options' '
68 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
69 > output 2> output.err &&
70 test_cmp expect output &&
71 test ! -s output.err
74 cat > expect << EOF
75 boolean: 2
76 integer: 1729
77 timestamp: 0
78 string: 321
79 abbrev: 10
80 verbose: 2
81 quiet: no
82 dry run: no
83 file: prefix/fi.le
84 EOF
86 test_expect_success 'long options' '
87 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
88 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
89 > output 2> output.err &&
90 test ! -s output.err &&
91 test_cmp expect output
94 test_expect_success 'missing required value' '
95 test-parse-options -s;
96 test $? = 129 &&
97 test-parse-options --string;
98 test $? = 129 &&
99 test-parse-options --file;
100 test $? = 129
103 cat > expect << EOF
104 boolean: 1
105 integer: 13
106 timestamp: 0
107 string: 123
108 abbrev: 7
109 verbose: 0
110 quiet: no
111 dry run: no
112 file: (not set)
113 arg 00: a1
114 arg 01: b1
115 arg 02: --boolean
118 test_expect_success 'intermingled arguments' '
119 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
120 > output 2> output.err &&
121 test ! -s output.err &&
122 test_cmp expect output
125 cat > expect << EOF
126 boolean: 0
127 integer: 2
128 timestamp: 0
129 string: (not set)
130 abbrev: 7
131 verbose: 0
132 quiet: no
133 dry run: no
134 file: (not set)
137 test_expect_success 'unambiguously abbreviated option' '
138 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
139 test ! -s output.err &&
140 test_cmp expect output
143 test_expect_success 'unambiguously abbreviated option with "="' '
144 test-parse-options --int=2 > output 2> output.err &&
145 test ! -s output.err &&
146 test_cmp expect output
149 test_expect_success 'ambiguously abbreviated option' '
150 test-parse-options --strin 123;
151 test $? = 129
154 cat > expect << EOF
155 boolean: 0
156 integer: 0
157 timestamp: 0
158 string: 123
159 abbrev: 7
160 verbose: 0
161 quiet: no
162 dry run: no
163 file: (not set)
166 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
167 test-parse-options --st 123 > output 2> output.err &&
168 test ! -s output.err &&
169 test_cmp expect output
172 cat > typo.err << EOF
173 error: did you mean \`--boolean\` (with two dashes ?)
176 test_expect_success 'detect possible typos' '
177 test_must_fail test-parse-options -boolean > output 2> output.err &&
178 test ! -s output &&
179 test_cmp typo.err output.err
182 cat > expect <<EOF
183 boolean: 0
184 integer: 0
185 timestamp: 0
186 string: (not set)
187 abbrev: 7
188 verbose: 0
189 quiet: no
190 dry run: no
191 file: (not set)
192 arg 00: --quux
195 test_expect_success 'keep some options as arguments' '
196 test-parse-options --quux > output 2> output.err &&
197 test ! -s output.err &&
198 test_cmp expect output
201 cat > expect <<EOF
202 boolean: 0
203 integer: 0
204 timestamp: 1
205 string: default
206 abbrev: 7
207 verbose: 0
208 quiet: yes
209 dry run: no
210 file: (not set)
211 arg 00: foo
214 test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
215 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
216 foo -q > output 2> output.err &&
217 test ! -s output.err &&
218 test_cmp expect output
221 cat > expect <<EOF
222 Callback: "four", 0
223 boolean: 5
224 integer: 4
225 timestamp: 0
226 string: (not set)
227 abbrev: 7
228 verbose: 0
229 quiet: no
230 dry run: no
231 file: (not set)
234 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
235 test-parse-options --length=four -b -4 > output 2> output.err &&
236 test ! -s output.err &&
237 test_cmp expect output
240 cat > expect <<EOF
241 Callback: "not set", 1
244 test_expect_success 'OPT_CALLBACK() and callback errors work' '
245 test_must_fail test-parse-options --no-length > output 2> output.err &&
246 test_cmp expect output &&
247 test_cmp expect.err output.err
250 cat > expect <<EOF
251 boolean: 1
252 integer: 23
253 timestamp: 0
254 string: (not set)
255 abbrev: 7
256 verbose: 0
257 quiet: no
258 dry run: no
259 file: (not set)
262 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
263 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
264 test ! -s output.err &&
265 test_cmp expect output
268 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
269 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
270 test ! -s output.err &&
271 test_cmp expect output
274 cat > expect <<EOF
275 boolean: 6
276 integer: 0
277 timestamp: 0
278 string: (not set)
279 abbrev: 7
280 verbose: 0
281 quiet: no
282 dry run: no
283 file: (not set)
286 test_expect_success 'OPT_BIT() works' '
287 test-parse-options -bb --or4 > output 2> output.err &&
288 test ! -s output.err &&
289 test_cmp expect output
292 test_expect_success 'OPT_NEGBIT() works' '
293 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
294 test ! -s output.err &&
295 test_cmp expect output
298 test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
299 test-parse-options + + + + + + > output 2> output.err &&
300 test ! -s output.err &&
301 test_cmp expect output
304 cat > expect <<EOF
305 boolean: 0
306 integer: 12345
307 timestamp: 0
308 string: (not set)
309 abbrev: 7
310 verbose: 0
311 quiet: no
312 dry run: no
313 file: (not set)
316 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
317 test-parse-options -12345 > output 2> output.err &&
318 test ! -s output.err &&
319 test_cmp expect output
322 cat >expect <<EOF
323 boolean: 0
324 integer: 0
325 timestamp: 0
326 string: (not set)
327 abbrev: 7
328 verbose: 0
329 quiet: no
330 dry run: no
331 file: (not set)
334 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
335 test-parse-options --no-ambig >output 2>output.err &&
336 test ! -s output.err &&
337 test_cmp expect output
340 test_done