]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0040-parse-options.sh
Merge branch 'lt/default-abbrev' into maint
[thirdparty/git.git] / t / t0040-parse-options.sh
CommitLineData
beb47437
JS
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes Schindelin
4#
5
6test_description='our own option parser'
7
8. ./test-lib.sh
9
9c7304e3 10cat > expect << EOF
beb47437
JS
11usage: test-parse-options <options>
12
13 -b, --boolean get a boolean
010a2dac 14 -4, --or4 bitwise-or boolean with ...0100
2f4b97f9 15 --neg-or4 same as --no-or4
010a2dac 16
beb47437
JS
17 -i, --integer <n> get a integer
18 -j <n> get a integer, too
010a2dac
SB
19 --set23 set integer to 23
20 -t <time> get timestamp of <time>
21 -L, --length <str> get length of <str>
41dbcd45 22 -F, --file <file> set file to <file>
beb47437 23
010a2dac 24String options
beb47437
JS
25 -s, --string <string>
26 get a string
27 --string2 <str> get another string
243e0614 28 --st <st> get another string (pervert ordering)
3a9f0f41 29 -o <str> get another string
010a2dac 30 --default-string set string to default
beb47437 31
010a2dac 32Magic arguments
580d5bff 33 --quux means --quux
e0319ff5 34 -NUM set integer to NUM
51a9949e 35 + same as -b
6bbfd1fa
AS
36 --ambiguous positive ambiguity
37 --no-ambiguous negative ambiguity
580d5bff 38
010a2dac
SB
39Standard 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
44
beb47437
JS
45EOF
46
47test_expect_success 'test help' '
010a2dac 48 test_must_fail test-parse-options -h > output 2> output.err &&
9c7304e3
GS
49 test ! -s output.err &&
50 test_cmp expect output
beb47437
JS
51'
52
9c7304e3
GS
53mv expect expect.err
54
beb47437
JS
55cat > expect << EOF
56boolean: 2
57integer: 1729
c4aca9cc 58timestamp: 0
beb47437 59string: 123
010a2dac
SB
60abbrev: 7
61verbose: 2
62quiet: no
63dry run: yes
df217ed6 64file: prefix/my.file
beb47437
JS
65EOF
66
67test_expect_success 'short options' '
df217ed6
SB
68 test-parse-options -s123 -b -i 1729 -b -vv -n -F my.file \
69 > output 2> output.err &&
3af82863 70 test_cmp expect output &&
beb47437
JS
71 test ! -s output.err
72'
010a2dac 73
beb47437
JS
74cat > expect << EOF
75boolean: 2
76integer: 1729
c4aca9cc 77timestamp: 0
beb47437 78string: 321
010a2dac
SB
79abbrev: 10
80verbose: 2
81quiet: no
82dry run: no
df217ed6 83file: prefix/fi.le
beb47437
JS
84EOF
85
86test_expect_success 'long options' '
87 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
df217ed6 88 --verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
beb47437
JS
89 > output 2> output.err &&
90 test ! -s output.err &&
3af82863 91 test_cmp expect output
beb47437
JS
92'
93
d5d745f9
OM
94test_expect_success 'missing required value' '
95 test-parse-options -s;
96 test $? = 129 &&
97 test-parse-options --string;
df217ed6
SB
98 test $? = 129 &&
99 test-parse-options --file;
d5d745f9
OM
100 test $? = 129
101'
102
beb47437
JS
103cat > expect << EOF
104boolean: 1
105integer: 13
c4aca9cc 106timestamp: 0
beb47437 107string: 123
010a2dac
SB
108abbrev: 7
109verbose: 0
110quiet: no
111dry run: no
df217ed6 112file: (not set)
beb47437
JS
113arg 00: a1
114arg 01: b1
115arg 02: --boolean
116EOF
117
118test_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 &&
3af82863 122 test_cmp expect output
beb47437
JS
123'
124
7f275b91
JS
125cat > expect << EOF
126boolean: 0
127integer: 2
c4aca9cc 128timestamp: 0
7f275b91 129string: (not set)
010a2dac
SB
130abbrev: 7
131verbose: 0
132quiet: no
133dry run: no
df217ed6 134file: (not set)
7f275b91
JS
135EOF
136
137test_expect_success 'unambiguously abbreviated option' '
138 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
139 test ! -s output.err &&
3af82863 140 test_cmp expect output
7f275b91
JS
141'
142
143test_expect_success 'unambiguously abbreviated option with "="' '
144 test-parse-options --int=2 > output 2> output.err &&
145 test ! -s output.err &&
3af82863 146 test_cmp expect output
7f275b91
JS
147'
148
41ac414e 149test_expect_success 'ambiguously abbreviated option' '
7f275b91 150 test-parse-options --strin 123;
41ac414e 151 test $? = 129
7f275b91
JS
152'
153
243e0614
JS
154cat > expect << EOF
155boolean: 0
156integer: 0
c4aca9cc 157timestamp: 0
243e0614 158string: 123
010a2dac
SB
159abbrev: 7
160verbose: 0
161quiet: no
162dry run: no
df217ed6 163file: (not set)
243e0614
JS
164EOF
165
166test_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 &&
3af82863 169 test_cmp expect output
243e0614
JS
170'
171
010a2dac 172cat > typo.err << EOF
3a9f0f41
PH
173error: did you mean \`--boolean\` (with two dashes ?)
174EOF
175
176test_expect_success 'detect possible typos' '
010a2dac 177 test_must_fail test-parse-options -boolean > output 2> output.err &&
3a9f0f41 178 test ! -s output &&
010a2dac 179 test_cmp typo.err output.err
3a9f0f41
PH
180'
181
580d5bff
PH
182cat > expect <<EOF
183boolean: 0
184integer: 0
c4aca9cc 185timestamp: 0
580d5bff 186string: (not set)
010a2dac
SB
187abbrev: 7
188verbose: 0
189quiet: no
190dry run: no
df217ed6 191file: (not set)
580d5bff
PH
192arg 00: --quux
193EOF
194
195test_expect_success 'keep some options as arguments' '
196 test-parse-options --quux > output 2> output.err &&
197 test ! -s output.err &&
3af82863 198 test_cmp expect output
580d5bff
PH
199'
200
010a2dac
SB
201cat > expect <<EOF
202boolean: 0
c4aca9cc
JH
203integer: 0
204timestamp: 1
010a2dac
SB
205string: default
206abbrev: 7
207verbose: 0
208quiet: yes
209dry run: no
df217ed6 210file: (not set)
010a2dac
SB
211arg 00: foo
212EOF
213
214test_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
219'
220
221cat > expect <<EOF
222Callback: "four", 0
223boolean: 5
224integer: 4
c4aca9cc 225timestamp: 0
010a2dac
SB
226string: (not set)
227abbrev: 7
228verbose: 0
229quiet: no
230dry run: no
df217ed6 231file: (not set)
010a2dac
SB
232EOF
233
234test_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
238'
239
240cat > expect <<EOF
241Callback: "not set", 1
242EOF
243
244test_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
248'
249
250cat > expect <<EOF
251boolean: 1
252integer: 23
c4aca9cc 253timestamp: 0
010a2dac
SB
254string: (not set)
255abbrev: 7
256verbose: 0
257quiet: no
258dry run: no
df217ed6 259file: (not set)
010a2dac
SB
260EOF
261
262test_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
266'
267
2f4b97f9
RS
268test_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
272'
273
274cat > expect <<EOF
275boolean: 6
276integer: 0
277timestamp: 0
278string: (not set)
279abbrev: 7
280verbose: 0
281quiet: no
282dry run: no
df217ed6 283file: (not set)
2f4b97f9
RS
284EOF
285
286test_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
290'
291
292test_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
296'
010a2dac 297
51a9949e
RS
298test_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
302'
303
e0319ff5
RS
304cat > expect <<EOF
305boolean: 0
306integer: 12345
307timestamp: 0
308string: (not set)
309abbrev: 7
310verbose: 0
311quiet: no
312dry run: no
df217ed6 313file: (not set)
e0319ff5
RS
314EOF
315
316test_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
320'
321
6bbfd1fa
AS
322cat >expect <<EOF
323boolean: 0
324integer: 0
325timestamp: 0
326string: (not set)
327abbrev: 7
328verbose: 0
329quiet: no
330dry run: no
331file: (not set)
332EOF
333
334test_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
338'
339
beb47437 340test_done