Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git rev-parse --parseopt' |
| 4 | . ./test-lib.sh |
| 5 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 6 | test_expect_success 'setup optionspec' ' |
| 7 | sed -e "s/^|//" >optionspec <<\EOF |
| 8 | |some-command [options] <args>... |
| 9 | | |
| 10 | |some-command does foo and bar! |
| 11 | |-- |
| 12 | |h,help show the help |
| 13 | | |
| 14 | |foo some nifty option --foo |
| 15 | |bar= some cool option --bar with an argument |
| 16 | |b,baz a short and long option |
| 17 | | |
| 18 | | An option group Header |
| 19 | |C? option C with an optional argument |
| 20 | |d,data? short and long option with an optional argument |
| 21 | | |
| 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 |
Brandon Casey | f221861 | 2017-09-17 15:28:14 -0700 | [diff] [blame] | 31 | |aswitch help te=t contains? fl*g characters!` |
| 32 | |bswitch=hint hint has trailing tab character |
| 33 | |cswitch switch has trailing tab character |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 34 | |short-hint=a with a one symbol hint |
| 35 | | |
| 36 | |Extras |
| 37 | |extra1 line above used to cause a segfault but no longer does |
| 38 | EOF |
| 39 | ' |
| 40 | |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 41 | test_expect_success 'setup optionspec-no-switches' ' |
| 42 | sed -e "s/^|//" >optionspec_no_switches <<\EOF |
| 43 | |some-command [options] <args>... |
| 44 | | |
| 45 | |some-command does foo and bar! |
| 46 | |-- |
| 47 | EOF |
| 48 | ' |
| 49 | |
| 50 | test_expect_success 'setup optionspec-only-hidden-switches' ' |
| 51 | sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF |
| 52 | |some-command [options] <args>... |
| 53 | | |
| 54 | |some-command does foo and bar! |
| 55 | |-- |
| 56 | |hidden1* A hidden switch |
| 57 | EOF |
| 58 | ' |
| 59 | |
Brandon Casey | 33e7512 | 2017-09-17 15:28:16 -0700 | [diff] [blame] | 60 | test_expect_success 'test --parseopt help output' ' |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 61 | sed -e "s/^|//" >expect <<\END_EXPECT && |
Junio C Hamano | ce7f874 | 2014-03-23 15:26:36 -0700 | [diff] [blame] | 62 | |cat <<\EOF |
| 63 | |usage: some-command [options] <args>... |
| 64 | | |
| 65 | | some-command does foo and bar! |
| 66 | | |
| 67 | | -h, --help show the help |
| 68 | | --foo some nifty option --foo |
| 69 | | --bar ... some cool option --bar with an argument |
| 70 | | -b, --baz a short and long option |
| 71 | | |
| 72 | |An option group Header |
| 73 | | -C[...] option C with an optional argument |
| 74 | | -d, --data[=...] short and long option with an optional argument |
| 75 | | |
| 76 | |Argument hints |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 77 | | -B <arg> short option required argument |
Junio C Hamano | ce7f874 | 2014-03-23 15:26:36 -0700 | [diff] [blame] | 78 | | --bar2 <arg> long option required argument |
| 79 | | -e, --fuz <with-space> |
| 80 | | short and long option required argument |
| 81 | | -s[<some>] short option optional argument |
| 82 | | --long[=<data>] long option optional argument |
| 83 | | -g, --fluf[=<path>] short and long option optional argument |
| 84 | | --longest <very-long-argument-hint> |
| 85 | | a very long argument hint |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 86 | | --pair <key=value> with an equals sign in the hint |
Brandon Casey | f221861 | 2017-09-17 15:28:14 -0700 | [diff] [blame] | 87 | | --aswitch help te=t contains? fl*g characters!` |
| 88 | | --bswitch <hint> hint has trailing tab character |
| 89 | | --cswitch switch has trailing tab character |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 90 | | --short-hint <a> with a one symbol hint |
Junio C Hamano | ce7f874 | 2014-03-23 15:26:36 -0700 | [diff] [blame] | 91 | | |
| 92 | |Extras |
| 93 | | --extra1 line above used to cause a segfault but no longer does |
| 94 | | |
| 95 | |EOF |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 96 | END_EXPECT |
Jonathan Nieder | 2b5ec01 | 2010-10-31 02:35:49 -0500 | [diff] [blame] | 97 | test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 98 | test_cmp expect output |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 99 | ' |
| 100 | |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 101 | test_expect_success 'test --parseopt help output no switches' ' |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 102 | sed -e "s/^|//" >expect <<\END_EXPECT && |
| 103 | |cat <<\EOF |
| 104 | |usage: some-command [options] <args>... |
| 105 | | |
| 106 | | some-command does foo and bar! |
| 107 | | |
| 108 | |EOF |
| 109 | END_EXPECT |
| 110 | test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 111 | test_cmp expect output |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 112 | ' |
| 113 | |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 114 | test_expect_success 'test --parseopt help output hidden switches' ' |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 115 | sed -e "s/^|//" >expect <<\END_EXPECT && |
| 116 | |cat <<\EOF |
| 117 | |usage: some-command [options] <args>... |
| 118 | | |
| 119 | | some-command does foo and bar! |
| 120 | | |
| 121 | |EOF |
| 122 | END_EXPECT |
| 123 | test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 124 | test_cmp expect output |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 125 | ' |
| 126 | |
| 127 | test_expect_success 'test --parseopt help-all output hidden switches' ' |
| 128 | sed -e "s/^|//" >expect <<\END_EXPECT && |
| 129 | |cat <<\EOF |
| 130 | |usage: some-command [options] <args>... |
| 131 | | |
| 132 | | some-command does foo and bar! |
| 133 | | |
| 134 | | --hidden1 A hidden switch |
| 135 | | |
| 136 | |EOF |
| 137 | END_EXPECT |
| 138 | test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 139 | test_cmp expect output |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 140 | ' |
| 141 | |
Brandon Casey | 1a9bf1e | 2017-09-24 21:08:04 -0700 | [diff] [blame] | 142 | test_expect_success 'test --parseopt invalid switch help output' ' |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 143 | sed -e "s/^|//" >expect <<\END_EXPECT && |
| 144 | |error: unknown option `does-not-exist'\'' |
| 145 | |usage: some-command [options] <args>... |
| 146 | | |
| 147 | | some-command does foo and bar! |
| 148 | | |
| 149 | | -h, --help show the help |
| 150 | | --foo some nifty option --foo |
| 151 | | --bar ... some cool option --bar with an argument |
| 152 | | -b, --baz a short and long option |
| 153 | | |
| 154 | |An option group Header |
| 155 | | -C[...] option C with an optional argument |
| 156 | | -d, --data[=...] short and long option with an optional argument |
| 157 | | |
| 158 | |Argument hints |
| 159 | | -B <arg> short option required argument |
| 160 | | --bar2 <arg> long option required argument |
| 161 | | -e, --fuz <with-space> |
| 162 | | short and long option required argument |
| 163 | | -s[<some>] short option optional argument |
| 164 | | --long[=<data>] long option optional argument |
| 165 | | -g, --fluf[=<path>] short and long option optional argument |
| 166 | | --longest <very-long-argument-hint> |
| 167 | | a very long argument hint |
| 168 | | --pair <key=value> with an equals sign in the hint |
| 169 | | --aswitch help te=t contains? fl*g characters!` |
| 170 | | --bswitch <hint> hint has trailing tab character |
| 171 | | --cswitch switch has trailing tab character |
| 172 | | --short-hint <a> with a one symbol hint |
| 173 | | |
| 174 | |Extras |
| 175 | | --extra1 line above used to cause a segfault but no longer does |
| 176 | | |
| 177 | END_EXPECT |
| 178 | test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 179 | test_cmp expect output |
Brandon Casey | c97ee17 | 2017-09-24 21:08:03 -0700 | [diff] [blame] | 180 | ' |
| 181 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 182 | test_expect_success 'setup expect.1' " |
| 183 | cat > expect <<EOF |
Brandon Casey | f221861 | 2017-09-17 15:28:14 -0700 | [diff] [blame] | 184 | set -- --foo --bar 'ham' -b --aswitch -- 'arg' |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 185 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 186 | " |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 187 | |
Brandon Casey | 28a8d0f | 2017-09-17 15:28:15 -0700 | [diff] [blame] | 188 | test_expect_success 'test --parseopt' ' |
Brandon Casey | f221861 | 2017-09-17 15:28:14 -0700 | [diff] [blame] | 189 | git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output && |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 190 | test_cmp expect output |
| 191 | ' |
| 192 | |
Brandon Casey | 28a8d0f | 2017-09-17 15:28:15 -0700 | [diff] [blame] | 193 | test_expect_success 'test --parseopt with mixed options and arguments' ' |
Brandon Casey | f221861 | 2017-09-17 15:28:14 -0700 | [diff] [blame] | 194 | git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output && |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 195 | test_cmp expect output |
| 196 | ' |
| 197 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 198 | test_expect_success 'setup expect.2' " |
| 199 | cat > expect <<EOF |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 200 | set -- --foo -- 'arg' '--bar=ham' |
| 201 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 202 | " |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 203 | |
| 204 | test_expect_success 'test --parseopt with --' ' |
| 205 | git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output && |
| 206 | test_cmp expect output |
| 207 | ' |
| 208 | |
Uwe Kleine-König | 6e0800e | 2009-06-14 01:58:43 +0200 | [diff] [blame] | 209 | test_expect_success 'test --parseopt --stop-at-non-option' ' |
| 210 | git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output && |
| 211 | test_cmp expect output |
| 212 | ' |
| 213 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 214 | test_expect_success 'setup expect.3' " |
| 215 | cat > expect <<EOF |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 216 | set -- --foo -- '--' 'arg' '--bar=ham' |
| 217 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 218 | " |
Uwe Kleine-König | 824af25 | 2009-06-14 01:58:42 +0200 | [diff] [blame] | 219 | |
| 220 | test_expect_success 'test --parseopt --keep-dashdash' ' |
| 221 | git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output && |
| 222 | test_cmp expect output |
| 223 | ' |
| 224 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 225 | test_expect_success 'setup expect.4' " |
| 226 | cat >expect <<EOF |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 227 | set -- --foo -- '--' 'arg' '--spam=ham' |
| 228 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 229 | " |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 230 | |
| 231 | test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' ' |
| 232 | git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output && |
| 233 | test_cmp expect output |
| 234 | ' |
| 235 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 236 | test_expect_success 'setup expect.5' " |
| 237 | cat > expect <<EOF |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 238 | set -- --foo -- 'arg' '--spam=ham' |
| 239 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 240 | " |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 241 | |
| 242 | test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' ' |
| 243 | git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output && |
| 244 | test_cmp expect output |
| 245 | ' |
| 246 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 247 | test_expect_success 'setup expect.6' " |
| 248 | cat > expect <<EOF |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 249 | set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg' |
| 250 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 251 | " |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 252 | |
| 253 | test_expect_success 'test --parseopt --stuck-long' ' |
| 254 | git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output && |
| 255 | test_cmp expect output |
| 256 | ' |
| 257 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 258 | test_expect_success 'setup expect.7' " |
| 259 | cat > expect <<EOF |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 260 | set -- --data='' -C --baz -- 'arg' |
| 261 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 262 | " |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 263 | |
| 264 | test_expect_success 'test --parseopt --stuck-long and empty optional argument' ' |
| 265 | git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output && |
| 266 | test_cmp expect output |
| 267 | ' |
| 268 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 269 | test_expect_success 'setup expect.8' " |
| 270 | cat > expect <<EOF |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 271 | set -- --data --baz -- 'arg' |
| 272 | EOF |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 273 | " |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 274 | |
| 275 | test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' ' |
| 276 | git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output && |
| 277 | test_cmp expect output |
| 278 | ' |
| 279 | |
| 280 | test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' ' |
| 281 | git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output && |
| 282 | test_cmp expect output |
| 283 | ' |
| 284 | |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 285 | test_done |