Merge branch 'jc/maint-reset' into maint
[git/vmiklos.git] / t / t0040-parse-options.sh
blob6309aed4511738b2f68e16974a5a5ceadf2617e5
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
16 -i, --integer <n> get a integer
17 -j <n> get a integer, too
18 --set23 set integer to 23
19 -t <time> get timestamp of <time>
20 -L, --length <str> get length of <str>
22 String options
23 -s, --string <string>
24 get a string
25 --string2 <str> get another string
26 --st <st> get another string (pervert ordering)
27 -o <str> get another string
28 --default-string set string to default
30 Magic arguments
31 --quux means --quux
33 Standard options
34 --abbrev[=<n>] use <n> digits to display SHA-1s
35 -v, --verbose be verbose
36 -n, --dry-run dry run
37 -q, --quiet be quiet
39 EOF
41 test_expect_success 'test help' '
42 test_must_fail test-parse-options -h > output 2> output.err &&
43 test ! -s output &&
44 test_cmp expect.err output.err
47 cat > expect << EOF
48 boolean: 2
49 integer: 1729
50 string: 123
51 abbrev: 7
52 verbose: 2
53 quiet: no
54 dry run: yes
55 EOF
57 test_expect_success 'short options' '
58 test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
59 test_cmp expect output &&
60 test ! -s output.err
63 cat > expect << EOF
64 boolean: 2
65 integer: 1729
66 string: 321
67 abbrev: 10
68 verbose: 2
69 quiet: no
70 dry run: no
71 EOF
73 test_expect_success 'long options' '
74 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
75 --verbose --verbose --no-dry-run --abbrev=10 \
76 > output 2> output.err &&
77 test ! -s output.err &&
78 test_cmp expect output
81 cat > expect << EOF
82 boolean: 1
83 integer: 13
84 string: 123
85 abbrev: 7
86 verbose: 0
87 quiet: no
88 dry run: no
89 arg 00: a1
90 arg 01: b1
91 arg 02: --boolean
92 EOF
94 test_expect_success 'intermingled arguments' '
95 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
96 > output 2> output.err &&
97 test ! -s output.err &&
98 test_cmp expect output
101 cat > expect << EOF
102 boolean: 0
103 integer: 2
104 string: (not set)
105 abbrev: 7
106 verbose: 0
107 quiet: no
108 dry run: no
111 test_expect_success 'unambiguously abbreviated option' '
112 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
113 test ! -s output.err &&
114 test_cmp expect output
117 test_expect_success 'unambiguously abbreviated option with "="' '
118 test-parse-options --int=2 > output 2> output.err &&
119 test ! -s output.err &&
120 test_cmp expect output
123 test_expect_success 'ambiguously abbreviated option' '
124 test-parse-options --strin 123;
125 test $? = 129
128 cat > expect << EOF
129 boolean: 0
130 integer: 0
131 string: 123
132 abbrev: 7
133 verbose: 0
134 quiet: no
135 dry run: no
138 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
139 test-parse-options --st 123 > output 2> output.err &&
140 test ! -s output.err &&
141 test_cmp expect output
144 cat > typo.err << EOF
145 error: did you mean \`--boolean\` (with two dashes ?)
148 test_expect_success 'detect possible typos' '
149 test_must_fail test-parse-options -boolean > output 2> output.err &&
150 test ! -s output &&
151 test_cmp typo.err output.err
154 cat > expect <<EOF
155 boolean: 0
156 integer: 0
157 string: (not set)
158 abbrev: 7
159 verbose: 0
160 quiet: no
161 dry run: no
162 arg 00: --quux
165 test_expect_success 'keep some options as arguments' '
166 test-parse-options --quux > output 2> output.err &&
167 test ! -s output.err &&
168 test_cmp expect output
171 cat > expect <<EOF
172 boolean: 0
173 integer: 1
174 string: default
175 abbrev: 7
176 verbose: 0
177 quiet: yes
178 dry run: no
179 arg 00: foo
182 test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
183 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
184 foo -q > output 2> output.err &&
185 test ! -s output.err &&
186 test_cmp expect output
189 cat > expect <<EOF
190 Callback: "four", 0
191 boolean: 5
192 integer: 4
193 string: (not set)
194 abbrev: 7
195 verbose: 0
196 quiet: no
197 dry run: no
200 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
201 test-parse-options --length=four -b -4 > output 2> output.err &&
202 test ! -s output.err &&
203 test_cmp expect output
206 cat > expect <<EOF
207 Callback: "not set", 1
210 test_expect_success 'OPT_CALLBACK() and callback errors work' '
211 test_must_fail test-parse-options --no-length > output 2> output.err &&
212 test_cmp expect output &&
213 test_cmp expect.err output.err
216 cat > expect <<EOF
217 boolean: 1
218 integer: 23
219 string: (not set)
220 abbrev: 7
221 verbose: 0
222 quiet: no
223 dry run: no
226 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
227 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
228 test ! -s output.err &&
229 test_cmp expect output
232 # --or4
233 # --no-or4
235 test_done