builtin-commit: add --date option
[git/vmiklos.git] / t / t0040-parse-options.sh
blob3d450ed379fcd1bf480fc75feb0e6ddb8f05e5be
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 . ./test-lib.sh
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>
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 &&
50 test_cmp expect.err output.err
53 cat > expect << EOF
54 boolean: 2
55 integer: 1729
56 timestamp: 0
57 string: 123
58 abbrev: 7
59 verbose: 2
60 quiet: no
61 dry run: yes
62 file: prefix/my.file
63 EOF
65 test_expect_success 'short options' '
66 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
67 > output 2> output.err &&
68 test_cmp expect output &&
69 test ! -s output.err
72 cat > expect << EOF
73 boolean: 2
74 integer: 1729
75 timestamp: 0
76 string: 321
77 abbrev: 10
78 verbose: 2
79 quiet: no
80 dry run: no
81 file: prefix/fi.le
82 EOF
84 test_expect_success 'long options' '
85 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
86 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
87 > output 2> output.err &&
88 test ! -s output.err &&
89 test_cmp expect output
92 test_expect_success 'missing required value' '
93 test-parse-options -s;
94 test $? = 129 &&
95 test-parse-options --string;
96 test $? = 129 &&
97 test-parse-options --file;
98 test $? = 129
101 cat > expect << EOF
102 boolean: 1
103 integer: 13
104 timestamp: 0
105 string: 123
106 abbrev: 7
107 verbose: 0
108 quiet: no
109 dry run: no
110 file: (not set)
111 arg 00: a1
112 arg 01: b1
113 arg 02: --boolean
116 test_expect_success 'intermingled arguments' '
117 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
118 > output 2> output.err &&
119 test ! -s output.err &&
120 test_cmp expect output
123 cat > expect << EOF
124 boolean: 0
125 integer: 2
126 timestamp: 0
127 string: (not set)
128 abbrev: 7
129 verbose: 0
130 quiet: no
131 dry run: no
132 file: (not set)
135 test_expect_success 'unambiguously abbreviated option' '
136 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
137 test ! -s output.err &&
138 test_cmp expect output
141 test_expect_success 'unambiguously abbreviated option with "="' '
142 test-parse-options --int=2 > output 2> output.err &&
143 test ! -s output.err &&
144 test_cmp expect output
147 test_expect_success 'ambiguously abbreviated option' '
148 test-parse-options --strin 123;
149 test $? = 129
152 cat > expect << EOF
153 boolean: 0
154 integer: 0
155 timestamp: 0
156 string: 123
157 abbrev: 7
158 verbose: 0
159 quiet: no
160 dry run: no
161 file: (not set)
164 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
165 test-parse-options --st 123 > output 2> output.err &&
166 test ! -s output.err &&
167 test_cmp expect output
170 cat > typo.err << EOF
171 error: did you mean \`--boolean\` (with two dashes ?)
174 test_expect_success 'detect possible typos' '
175 test_must_fail test-parse-options -boolean > output 2> output.err &&
176 test ! -s output &&
177 test_cmp typo.err output.err
180 cat > expect <<EOF
181 boolean: 0
182 integer: 0
183 timestamp: 0
184 string: (not set)
185 abbrev: 7
186 verbose: 0
187 quiet: no
188 dry run: no
189 file: (not set)
190 arg 00: --quux
193 test_expect_success 'keep some options as arguments' '
194 test-parse-options --quux > output 2> output.err &&
195 test ! -s output.err &&
196 test_cmp expect output
199 cat > expect <<EOF
200 boolean: 0
201 integer: 0
202 timestamp: 1
203 string: default
204 abbrev: 7
205 verbose: 0
206 quiet: yes
207 dry run: no
208 file: (not set)
209 arg 00: foo
212 test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
213 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
214 foo -q > output 2> output.err &&
215 test ! -s output.err &&
216 test_cmp expect output
219 cat > expect <<EOF
220 Callback: "four", 0
221 boolean: 5
222 integer: 4
223 timestamp: 0
224 string: (not set)
225 abbrev: 7
226 verbose: 0
227 quiet: no
228 dry run: no
229 file: (not set)
232 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
233 test-parse-options --length=four -b -4 > output 2> output.err &&
234 test ! -s output.err &&
235 test_cmp expect output
238 cat > expect <<EOF
239 Callback: "not set", 1
242 test_expect_success 'OPT_CALLBACK() and callback errors work' '
243 test_must_fail test-parse-options --no-length > output 2> output.err &&
244 test_cmp expect output &&
245 test_cmp expect.err output.err
248 cat > expect <<EOF
249 boolean: 1
250 integer: 23
251 timestamp: 0
252 string: (not set)
253 abbrev: 7
254 verbose: 0
255 quiet: no
256 dry run: no
257 file: (not set)
260 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
261 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
262 test ! -s output.err &&
263 test_cmp expect output
266 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
267 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
268 test ! -s output.err &&
269 test_cmp expect output
272 cat > expect <<EOF
273 boolean: 6
274 integer: 0
275 timestamp: 0
276 string: (not set)
277 abbrev: 7
278 verbose: 0
279 quiet: no
280 dry run: no
281 file: (not set)
284 test_expect_success 'OPT_BIT() works' '
285 test-parse-options -bb --or4 > output 2> output.err &&
286 test ! -s output.err &&
287 test_cmp expect output
290 test_expect_success 'OPT_NEGBIT() works' '
291 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
292 test ! -s output.err &&
293 test_cmp expect output
296 test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
297 test-parse-options + + + + + + > output 2> output.err &&
298 test ! -s output.err &&
299 test_cmp expect output
302 cat > expect <<EOF
303 boolean: 0
304 integer: 12345
305 timestamp: 0
306 string: (not set)
307 abbrev: 7
308 verbose: 0
309 quiet: no
310 dry run: no
311 file: (not set)
314 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
315 test-parse-options -12345 > output 2> output.err &&
316 test ! -s output.err &&
317 test_cmp expect output
320 cat >expect <<EOF
321 boolean: 0
322 integer: 0
323 timestamp: 0
324 string: (not set)
325 abbrev: 7
326 verbose: 0
327 quiet: no
328 dry run: no
329 file: (not set)
332 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
333 test-parse-options --no-ambig >output 2>output.err &&
334 test ! -s output.err &&
335 test_cmp expect output
338 test_done