Merge branch 'cc/bisect' (early part)
[git.git] / t / t0040-parse-options.sh
bloba40c1236c0862da4d77231bf9255b499ccddd99f
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>
23 String options
24 -s, --string <string>
25 get a string
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
31 Magic arguments
32 --quux means --quux
33 -NUM set integer to NUM
34 + same as -b
36 Standard options
37 --abbrev[=<n>] use <n> digits to display SHA-1s
38 -v, --verbose be verbose
39 -n, --dry-run dry run
40 -q, --quiet be quiet
42 EOF
44 test_expect_success 'test help' '
45 test_must_fail test-parse-options -h > output 2> output.err &&
46 test ! -s output &&
47 test_cmp expect.err output.err
50 cat > expect << EOF
51 boolean: 2
52 integer: 1729
53 timestamp: 0
54 string: 123
55 abbrev: 7
56 verbose: 2
57 quiet: no
58 dry run: yes
59 EOF
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 &&
64 test ! -s output.err
67 cat > expect << EOF
68 boolean: 2
69 integer: 1729
70 timestamp: 0
71 string: 321
72 abbrev: 10
73 verbose: 2
74 quiet: no
75 dry run: no
76 EOF
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;
88 test $? = 129 &&
89 test-parse-options --string;
90 test $? = 129
93 cat > expect << EOF
94 boolean: 1
95 integer: 13
96 timestamp: 0
97 string: 123
98 abbrev: 7
99 verbose: 0
100 quiet: no
101 dry run: no
102 arg 00: a1
103 arg 01: b1
104 arg 02: --boolean
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
114 cat > expect << EOF
115 boolean: 0
116 integer: 2
117 timestamp: 0
118 string: (not set)
119 abbrev: 7
120 verbose: 0
121 quiet: no
122 dry run: no
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;
139 test $? = 129
142 cat > expect << EOF
143 boolean: 0
144 integer: 0
145 timestamp: 0
146 string: 123
147 abbrev: 7
148 verbose: 0
149 quiet: no
150 dry run: no
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 &&
165 test ! -s output &&
166 test_cmp typo.err output.err
169 cat > expect <<EOF
170 boolean: 0
171 integer: 0
172 timestamp: 0
173 string: (not set)
174 abbrev: 7
175 verbose: 0
176 quiet: no
177 dry run: no
178 arg 00: --quux
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
187 cat > expect <<EOF
188 boolean: 0
189 integer: 0
190 timestamp: 1
191 string: default
192 abbrev: 7
193 verbose: 0
194 quiet: yes
195 dry run: no
196 arg 00: foo
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
206 cat > expect <<EOF
207 Callback: "four", 0
208 boolean: 5
209 integer: 4
210 timestamp: 0
211 string: (not set)
212 abbrev: 7
213 verbose: 0
214 quiet: no
215 dry run: no
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
224 cat > expect <<EOF
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
234 cat > expect <<EOF
235 boolean: 1
236 integer: 23
237 timestamp: 0
238 string: (not set)
239 abbrev: 7
240 verbose: 0
241 quiet: no
242 dry run: no
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
257 cat > expect <<EOF
258 boolean: 6
259 integer: 0
260 timestamp: 0
261 string: (not set)
262 abbrev: 7
263 verbose: 0
264 quiet: no
265 dry run: no
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
286 cat > expect <<EOF
287 boolean: 0
288 integer: 12345
289 timestamp: 0
290 string: (not set)
291 abbrev: 7
292 verbose: 0
293 quiet: no
294 dry run: no
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
303 test_done