blob: 83b1300cef91c2a13ef58af4ab7a42618388bd01 [file] [log] [blame]
Jay Soffiane1033432008-02-25 23:07:39 -05001#!/bin/sh
2
3test_description='test git rev-parse --parseopt'
4. ./test-lib.sh
5
Thomas Rast47e9cd22010-06-12 14:57:39 +02006cat > expect <<\END_EXPECT
7cat <<\EOF
Jay Soffiane1033432008-02-25 23:07:39 -05008usage: some-command [options] <args>...
Jeff King44d86e92008-06-14 03:27:21 -04009
Jay Soffiane1033432008-02-25 23:07:39 -050010 some-command does foo and bar!
11
12 -h, --help show the help
13 --foo some nifty option --foo
14 --bar ... some cool option --bar with an argument
Nicolas Vigierf8c87212013-10-31 12:08:29 +010015 -b, --baz a short and long option
Jay Soffiane1033432008-02-25 23:07:39 -050016
17An option group Header
Michele Ballabio6422f632008-06-22 16:39:04 +020018 -C[...] option C with an optional argument
Nicolas Vigierf8c87212013-10-31 12:08:29 +010019 -d, --data[=...] short and long option with an optional argument
Jay Soffiane1033432008-02-25 23:07:39 -050020
21Extras
22 --extra1 line above used to cause a segfault but no longer does
23
24EOF
Thomas Rast47e9cd22010-06-12 14:57:39 +020025END_EXPECT
Jay Soffiane1033432008-02-25 23:07:39 -050026
Uwe Kleine-König824af252009-06-14 01:58:42 +020027cat > optionspec << EOF
Jay Soffiane1033432008-02-25 23:07:39 -050028some-command [options] <args>...
29
30some-command does foo and bar!
31--
32h,help show the help
33
34foo some nifty option --foo
35bar= some cool option --bar with an argument
Nicolas Vigierf8c87212013-10-31 12:08:29 +010036b,baz a short and long option
Jay Soffiane1033432008-02-25 23:07:39 -050037
38 An option group Header
39C? option C with an optional argument
Nicolas Vigierf8c87212013-10-31 12:08:29 +010040d,data? short and long option with an optional argument
Jay Soffiane1033432008-02-25 23:07:39 -050041
42Extras
43extra1 line above used to cause a segfault but no longer does
44EOF
Uwe Kleine-König824af252009-06-14 01:58:42 +020045
46test_expect_success 'test --parseopt help output' '
Jonathan Nieder2b5ec012010-10-31 02:35:49 -050047 test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
Jiang Xin9a001382012-08-27 13:36:55 +080048 test_i18ncmp expect output
Jay Soffiane1033432008-02-25 23:07:39 -050049'
50
Uwe Kleine-König824af252009-06-14 01:58:42 +020051cat > expect <<EOF
Nicolas Vigierf8c87212013-10-31 12:08:29 +010052set -- --foo --bar 'ham' -b -- 'arg'
Uwe Kleine-König824af252009-06-14 01:58:42 +020053EOF
54
55test_expect_success 'test --parseopt' '
Nicolas Vigierf8c87212013-10-31 12:08:29 +010056 git rev-parse --parseopt -- --foo --bar=ham --baz arg < optionspec > output &&
Uwe Kleine-König824af252009-06-14 01:58:42 +020057 test_cmp expect output
58'
59
60test_expect_success 'test --parseopt with mixed options and arguments' '
Nicolas Vigierf8c87212013-10-31 12:08:29 +010061 git rev-parse --parseopt -- --foo arg --bar=ham --baz < optionspec > output &&
Uwe Kleine-König824af252009-06-14 01:58:42 +020062 test_cmp expect output
63'
64
65cat > expect <<EOF
66set -- --foo -- 'arg' '--bar=ham'
67EOF
68
69test_expect_success 'test --parseopt with --' '
70 git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
71 test_cmp expect output
72'
73
Uwe Kleine-König6e0800e2009-06-14 01:58:43 +020074test_expect_success 'test --parseopt --stop-at-non-option' '
75 git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
76 test_cmp expect output
77'
78
Uwe Kleine-König824af252009-06-14 01:58:42 +020079cat > expect <<EOF
80set -- --foo -- '--' 'arg' '--bar=ham'
81EOF
82
83test_expect_success 'test --parseopt --keep-dashdash' '
84 git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
85 test_cmp expect output
86'
87
Uwe Kleine-König29981382010-07-06 16:46:05 +020088cat >expect <<EOF
89set -- --foo -- '--' 'arg' '--spam=ham'
90EOF
91
92test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
93 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
94 test_cmp expect output
95'
96
97cat > expect <<EOF
98set -- --foo -- 'arg' '--spam=ham'
99EOF
100
101test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
102 git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
103 test_cmp expect output
104'
105
Nicolas Vigierf8c87212013-10-31 12:08:29 +0100106cat > expect <<EOF
107set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
108EOF
109
110test_expect_success 'test --parseopt --stuck-long' '
111 git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
112 test_cmp expect output
113'
114
115cat > expect <<EOF
116set -- --data='' -C --baz -- 'arg'
117EOF
118
119test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
120 git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
121 test_cmp expect output
122'
123
124cat > expect <<EOF
125set -- --data --baz -- 'arg'
126EOF
127
128test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
129 git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&
130 test_cmp expect output
131'
132
133test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' '
134 git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output &&
135 test_cmp expect output
136'
137
Jay Soffiane1033432008-02-25 23:07:39 -0500138test_done