t1502: move optionspec help output to a file
[git/debian.git] / t / t1502-rev-parse-parseopt.sh
blob813ee5872f43e331474d685f37b2ba0c961533ed
1 #!/bin/sh
3 test_description='test git rev-parse --parseopt'
4 . ./test-lib.sh
6 test_expect_success 'setup optionspec' '
7 sed -e "s/^|//" >optionspec <<\EOF
8 |some-command [options] <args>...
10 |some-command does foo and bar!
11 |--
12 |h,help! show the help
14 |foo some nifty option --foo
15 |bar= some cool option --bar with an argument
16 |b,baz a short and long option
18 | An option group Header
19 |C? option C with an optional argument
20 |d,data? short and long option with an optional argument
22 | Argument hints
23 |B=arg short option required argument
24 |bar2=arg long option required argument
25 |e,fuz=with-space short and long option required argument
26 |s?some short option optional argument
27 |long?data long option optional argument
28 |g,fluf?path short and long option optional argument
29 |longest=very-long-argument-hint a very long argument hint
30 |pair=key=value with an equals sign in the hint
31 |aswitch help te=t contains? fl*g characters!`
32 |bswitch=hint hint has trailing tab character
33 |cswitch switch has trailing tab character
34 |short-hint=a with a one symbol hint
36 |Extras
37 |extra1 line above used to cause a segfault but no longer does
38 EOF
41 test_expect_success 'setup optionspec-no-switches' '
42 sed -e "s/^|//" >optionspec_no_switches <<\EOF
43 |some-command [options] <args>...
45 |some-command does foo and bar!
46 |--
47 EOF
50 test_expect_success 'setup optionspec-only-hidden-switches' '
51 sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
52 |some-command [options] <args>...
54 |some-command does foo and bar!
55 |--
56 |hidden1* A hidden switch
57 EOF
60 test_expect_success 'test --parseopt help output' '
61 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
62 test_cmp "$TEST_DIRECTORY/t1502/optionspec.help" output
65 test_expect_success 'test --parseopt help output no switches' '
66 sed -e "s/^|//" >expect <<\END_EXPECT &&
67 |cat <<\EOF
68 |usage: some-command [options] <args>...
70 | some-command does foo and bar!
72 |EOF
73 END_EXPECT
74 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
75 test_cmp expect output
78 test_expect_success 'test --parseopt help output hidden switches' '
79 sed -e "s/^|//" >expect <<\END_EXPECT &&
80 |cat <<\EOF
81 |usage: some-command [options] <args>...
83 | some-command does foo and bar!
85 |EOF
86 END_EXPECT
87 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
88 test_cmp expect output
91 test_expect_success 'test --parseopt help-all output hidden switches' '
92 sed -e "s/^|//" >expect <<\END_EXPECT &&
93 |cat <<\EOF
94 |usage: some-command [options] <args>...
96 | some-command does foo and bar!
98 | --hidden1 A hidden switch
100 |EOF
101 END_EXPECT
102 test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
103 test_cmp expect output
106 test_expect_success 'test --parseopt invalid switch help output' '
108 cat <<-\EOF &&
109 error: unknown option `does-not-exist'\''
111 sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
112 } >expect &&
113 test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
114 test_cmp expect output
117 test_expect_success 'setup expect.1' "
118 cat > expect <<EOF
119 set -- --foo --bar 'ham' -b --aswitch -- 'arg'
123 test_expect_success 'test --parseopt' '
124 git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
125 test_cmp expect output
128 test_expect_success 'test --parseopt with mixed options and arguments' '
129 git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
130 test_cmp expect output
133 test_expect_success 'setup expect.2' "
134 cat > expect <<EOF
135 set -- --foo -- 'arg' '--bar=ham'
139 test_expect_success 'test --parseopt with --' '
140 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
141 test_cmp expect output
144 test_expect_success 'test --parseopt --stop-at-non-option' '
145 git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
146 test_cmp expect output
149 test_expect_success 'setup expect.3' "
150 cat > expect <<EOF
151 set -- --foo -- '--' 'arg' '--bar=ham'
155 test_expect_success 'test --parseopt --keep-dashdash' '
156 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
157 test_cmp expect output
160 test_expect_success 'setup expect.4' "
161 cat >expect <<EOF
162 set -- --foo -- '--' 'arg' '--spam=ham'
166 test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
167 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
168 test_cmp expect output
171 test_expect_success 'setup expect.5' "
172 cat > expect <<EOF
173 set -- --foo -- 'arg' '--spam=ham'
177 test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
178 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
179 test_cmp expect output
182 test_expect_success 'setup expect.6' "
183 cat > expect <<EOF
184 set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
188 test_expect_success 'test --parseopt --stuck-long' '
189 git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
190 test_cmp expect output
193 test_expect_success 'setup expect.7' "
194 cat > expect <<EOF
195 set -- --data='' -C --baz -- 'arg'
199 test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
200 git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
201 test_cmp expect output
204 test_expect_success 'setup expect.8' "
205 cat > expect <<EOF
206 set -- --data --baz -- 'arg'
210 test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
211 git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&
212 test_cmp expect output
215 test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' '
216 git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output &&
217 test_cmp expect output
220 test_expect_success 'test --parseopt help output: "wrapped" options normal "or:" lines' '
221 sed -e "s/^|//" >spec <<-\EOF &&
222 |cmd [--some-option]
223 | [--another-option]
224 |cmd [--yet-another-option]
226 |h,help! show the help
229 sed -e "s/^|//" >expect <<-\END_EXPECT &&
230 |cat <<\EOF
231 |usage: cmd [--some-option]
232 | or: [--another-option]
233 | or: cmd [--yet-another-option]
235 | -h, --help show the help
237 |EOF
238 END_EXPECT
240 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
241 test_cmp expect actual
244 test_expect_success 'test --parseopt invalid opt-spec' '
245 test_write_lines x -- "=, x" >spec &&
246 echo "fatal: missing opt-spec before option flags" >expect &&
247 test_must_fail git rev-parse --parseopt -- <spec 2>err &&
248 test_cmp expect err
251 test_expect_success 'test --parseopt help output: multi-line blurb after empty line' '
252 sed -e "s/^|//" >spec <<-\EOF &&
253 |cmd [--some-option]
254 | [--another-option]
256 |multi
257 |line
258 |blurb
260 |h,help! show the help
263 sed -e "s/^|//" >expect <<-\END_EXPECT &&
264 |cat <<\EOF
265 |usage: cmd [--some-option]
266 | or: [--another-option]
268 | multi
269 | line
270 | blurb
272 | -h, --help show the help
274 |EOF
275 END_EXPECT
277 test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
278 test_cmp expect actual
281 test_done