parseopt: add OPT_NEGBIT
[git/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
10cat > expect.err << EOF
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>
beb47437 22
010a2dac 23String options
beb47437
JS
24 -s, --string <string>
25 get a string
26 --string2 <str> get another string
243e0614 27 --st <st> get another string (pervert ordering)
3a9f0f41 28 -o <str> get another string
010a2dac 29 --default-string set string to default
beb47437 30
010a2dac 31Magic arguments
580d5bff
PH
32 --quux means --quux
33
010a2dac
SB
34Standard options
35 --abbrev[=<n>] use <n> digits to display SHA-1s
36 -v, --verbose be verbose
37 -n, --dry-run dry run
38 -q, --quiet be quiet
39
beb47437
JS
40EOF
41
42test_expect_success 'test help' '
010a2dac 43 test_must_fail test-parse-options -h > output 2> output.err &&
beb47437 44 test ! -s output &&
3af82863 45 test_cmp expect.err output.err
beb47437
JS
46'
47
48cat > expect << EOF
49boolean: 2
50integer: 1729
c4aca9cc 51timestamp: 0
beb47437 52string: 123
010a2dac
SB
53abbrev: 7
54verbose: 2
55quiet: no
56dry run: yes
beb47437
JS
57EOF
58
59test_expect_success 'short options' '
010a2dac 60 test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
3af82863 61 test_cmp expect output &&
beb47437
JS
62 test ! -s output.err
63'
010a2dac 64
beb47437
JS
65cat > expect << EOF
66boolean: 2
67integer: 1729
c4aca9cc 68timestamp: 0
beb47437 69string: 321
010a2dac
SB
70abbrev: 10
71verbose: 2
72quiet: no
73dry run: no
beb47437
JS
74EOF
75
76test_expect_success 'long options' '
77 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
010a2dac 78 --verbose --verbose --no-dry-run --abbrev=10 \
beb47437
JS
79 > output 2> output.err &&
80 test ! -s output.err &&
3af82863 81 test_cmp expect output
beb47437
JS
82'
83
d5d745f9
OM
84test_expect_success 'missing required value' '
85 test-parse-options -s;
86 test $? = 129 &&
87 test-parse-options --string;
88 test $? = 129
89'
90
beb47437
JS
91cat > expect << EOF
92boolean: 1
93integer: 13
c4aca9cc 94timestamp: 0
beb47437 95string: 123
010a2dac
SB
96abbrev: 7
97verbose: 0
98quiet: no
99dry run: no
beb47437
JS
100arg 00: a1
101arg 01: b1
102arg 02: --boolean
103EOF
104
105test_expect_success 'intermingled arguments' '
106 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
107 > output 2> output.err &&
108 test ! -s output.err &&
3af82863 109 test_cmp expect output
beb47437
JS
110'
111
7f275b91
JS
112cat > expect << EOF
113boolean: 0
114integer: 2
c4aca9cc 115timestamp: 0
7f275b91 116string: (not set)
010a2dac
SB
117abbrev: 7
118verbose: 0
119quiet: no
120dry run: no
7f275b91
JS
121EOF
122
123test_expect_success 'unambiguously abbreviated option' '
124 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
125 test ! -s output.err &&
3af82863 126 test_cmp expect output
7f275b91
JS
127'
128
129test_expect_success 'unambiguously abbreviated option with "="' '
130 test-parse-options --int=2 > output 2> output.err &&
131 test ! -s output.err &&
3af82863 132 test_cmp expect output
7f275b91
JS
133'
134
41ac414e 135test_expect_success 'ambiguously abbreviated option' '
7f275b91 136 test-parse-options --strin 123;
41ac414e 137 test $? = 129
7f275b91
JS
138'
139
243e0614
JS
140cat > expect << EOF
141boolean: 0
142integer: 0
c4aca9cc 143timestamp: 0
243e0614 144string: 123
010a2dac
SB
145abbrev: 7
146verbose: 0
147quiet: no
148dry run: no
243e0614
JS
149EOF
150
151test_expect_success 'non ambiguous option (after two options it abbreviates)' '
152 test-parse-options --st 123 > output 2> output.err &&
153 test ! -s output.err &&
3af82863 154 test_cmp expect output
243e0614
JS
155'
156
010a2dac 157cat > typo.err << EOF
3a9f0f41
PH
158error: did you mean \`--boolean\` (with two dashes ?)
159EOF
160
161test_expect_success 'detect possible typos' '
010a2dac 162 test_must_fail test-parse-options -boolean > output 2> output.err &&
3a9f0f41 163 test ! -s output &&
010a2dac 164 test_cmp typo.err output.err
3a9f0f41
PH
165'
166
580d5bff
PH
167cat > expect <<EOF
168boolean: 0
169integer: 0
c4aca9cc 170timestamp: 0
580d5bff 171string: (not set)
010a2dac
SB
172abbrev: 7
173verbose: 0
174quiet: no
175dry run: no
580d5bff
PH
176arg 00: --quux
177EOF
178
179test_expect_success 'keep some options as arguments' '
180 test-parse-options --quux > output 2> output.err &&
181 test ! -s output.err &&
3af82863 182 test_cmp expect output
580d5bff
PH
183'
184
010a2dac
SB
185cat > expect <<EOF
186boolean: 0
c4aca9cc
JH
187integer: 0
188timestamp: 1
010a2dac
SB
189string: default
190abbrev: 7
191verbose: 0
192quiet: yes
193dry run: no
194arg 00: foo
195EOF
196
197test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
198 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
199 foo -q > output 2> output.err &&
200 test ! -s output.err &&
201 test_cmp expect output
202'
203
204cat > expect <<EOF
205Callback: "four", 0
206boolean: 5
207integer: 4
c4aca9cc 208timestamp: 0
010a2dac
SB
209string: (not set)
210abbrev: 7
211verbose: 0
212quiet: no
213dry run: no
214EOF
215
216test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
217 test-parse-options --length=four -b -4 > output 2> output.err &&
218 test ! -s output.err &&
219 test_cmp expect output
220'
221
222cat > expect <<EOF
223Callback: "not set", 1
224EOF
225
226test_expect_success 'OPT_CALLBACK() and callback errors work' '
227 test_must_fail test-parse-options --no-length > output 2> output.err &&
228 test_cmp expect output &&
229 test_cmp expect.err output.err
230'
231
232cat > expect <<EOF
233boolean: 1
234integer: 23
c4aca9cc 235timestamp: 0
010a2dac
SB
236string: (not set)
237abbrev: 7
238verbose: 0
239quiet: no
240dry run: no
241EOF
242
243test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
244 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
245 test ! -s output.err &&
246 test_cmp expect output
247'
248
2f4b97f9
RS
249test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
250 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
251 test ! -s output.err &&
252 test_cmp expect output
253'
254
255cat > expect <<EOF
256boolean: 6
257integer: 0
258timestamp: 0
259string: (not set)
260abbrev: 7
261verbose: 0
262quiet: no
263dry run: no
264EOF
265
266test_expect_success 'OPT_BIT() works' '
267 test-parse-options -bb --or4 > output 2> output.err &&
268 test ! -s output.err &&
269 test_cmp expect output
270'
271
272test_expect_success 'OPT_NEGBIT() works' '
273 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
274 test ! -s output.err &&
275 test_cmp expect output
276'
010a2dac 277
beb47437 278test_done