parseopt: add OPT_NUMBER_CALLBACK
[git/dkf.git] / t / t0040-parse-options.sh
blob8ca62ef453443e7c0f95e0e3cb3bb871b8273ed0
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
35 Standard options
36 --abbrev[=<n>] use <n> digits to display SHA-1s
37 -v, --verbose be verbose
38 -n, --dry-run dry run
39 -q, --quiet be quiet
41 EOF
43 test_expect_success 'test help' '
44 test_must_fail test-parse-options -h > output 2> output.err &&
45 test ! -s output &&
46 test_cmp expect.err output.err
49 cat > expect << EOF
50 boolean: 2
51 integer: 1729
52 timestamp: 0
53 string: 123
54 abbrev: 7
55 verbose: 2
56 quiet: no
57 dry run: yes
58 EOF
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 &&
63 test ! -s output.err
66 cat > expect << EOF
67 boolean: 2
68 integer: 1729
69 timestamp: 0
70 string: 321
71 abbrev: 10
72 verbose: 2
73 quiet: no
74 dry run: no
75 EOF
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;
87 test $? = 129 &&
88 test-parse-options --string;
89 test $? = 129
92 cat > expect << EOF
93 boolean: 1
94 integer: 13
95 timestamp: 0
96 string: 123
97 abbrev: 7
98 verbose: 0
99 quiet: no
100 dry run: no
101 arg 00: a1
102 arg 01: b1
103 arg 02: --boolean
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
113 cat > expect << EOF
114 boolean: 0
115 integer: 2
116 timestamp: 0
117 string: (not set)
118 abbrev: 7
119 verbose: 0
120 quiet: no
121 dry run: no
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;
138 test $? = 129
141 cat > expect << EOF
142 boolean: 0
143 integer: 0
144 timestamp: 0
145 string: 123
146 abbrev: 7
147 verbose: 0
148 quiet: no
149 dry run: no
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 &&
164 test ! -s output &&
165 test_cmp typo.err output.err
168 cat > expect <<EOF
169 boolean: 0
170 integer: 0
171 timestamp: 0
172 string: (not set)
173 abbrev: 7
174 verbose: 0
175 quiet: no
176 dry run: no
177 arg 00: --quux
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
186 cat > expect <<EOF
187 boolean: 0
188 integer: 0
189 timestamp: 1
190 string: default
191 abbrev: 7
192 verbose: 0
193 quiet: yes
194 dry run: no
195 arg 00: foo
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
205 cat > expect <<EOF
206 Callback: "four", 0
207 boolean: 5
208 integer: 4
209 timestamp: 0
210 string: (not set)
211 abbrev: 7
212 verbose: 0
213 quiet: no
214 dry run: no
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
223 cat > expect <<EOF
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
233 cat > expect <<EOF
234 boolean: 1
235 integer: 23
236 timestamp: 0
237 string: (not set)
238 abbrev: 7
239 verbose: 0
240 quiet: no
241 dry run: no
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
256 cat > expect <<EOF
257 boolean: 6
258 integer: 0
259 timestamp: 0
260 string: (not set)
261 abbrev: 7
262 verbose: 0
263 quiet: no
264 dry run: no
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
279 cat > expect <<EOF
280 boolean: 0
281 integer: 12345
282 timestamp: 0
283 string: (not set)
284 abbrev: 7
285 verbose: 0
286 quiet: no
287 dry run: no
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
296 test_done