]> git.ipfire.org Git - thirdparty/util-linux.git/blob - tests/ts/getopt/options
tests: split stdout and stderr
[thirdparty/util-linux.git] / tests / ts / getopt / options
1 #!/bin/bash
2
3 # This file is part of util-linux.
4 #
5 # This file is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This file is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 TS_TOPDIR="${0%/*}/../.."
16 TS_DESC="options"
17
18 . $TS_TOPDIR/functions.sh
19
20 ts_init "$*"
21 ts_check_test_command "$TS_CMD_GETOPT"
22
23 export LANG=C
24
25 function gnu_getopt_clean() {
26 sed --in-place \
27 -e "s/: \(.*\) -- \(.\)$/: \1 -- '\2'/" \
28 -e "s/ ['\`]\(--[a-zA-Z0-9]\{1,\}\)'/ '\1'/g" \
29 -e "s/unrecognized option: \(.\)$/invalid option -- '\1'/" \
30 -e "s/option requires an argument: \(.\)$/option requires an argument -- '\1'/" \
31 -e "s/unrecognized option: \([^'-].\{1,\}\)$/unrecognized option '--\1'/" \
32 $TS_OUTPUT $TS_ERRLOG
33 }
34
35 ts_init_subtest alternative_option_clash
36 $TS_CMD_GETOPT -a -o abcde -l abcde -- -a -ab -abc -ac >> $TS_OUTPUT 2>> $TS_ERRLOG
37 echo "exit value: $?" >> $TS_OUTPUT
38 gnu_getopt_clean
39 ts_finalize_subtest
40
41 ts_init_subtest alternative_option_long
42 $TS_CMD_GETOPT --alternative -o a -l one,two:,three::,four:: -- -a -one -two=MANDATORY -three=OPTIONAL >> $TS_OUTPUT 2>> $TS_ERRLOG
43 echo "exit value: $?" >> $TS_OUTPUT
44 gnu_getopt_clean
45 ts_finalize_subtest
46
47 ts_init_subtest alternative_option_short
48 $TS_CMD_GETOPT -a -o a -l one,two:,three::,four:: -- -a -one -two=MANDATORY -three=OPTIONAL >> $TS_OUTPUT 2>> $TS_ERRLOG
49 echo "exit value: $?" >> $TS_OUTPUT
50 gnu_getopt_clean
51 ts_finalize_subtest
52
53 ts_init_subtest invalid_getopt_option
54 $TS_CMD_GETOPT -b >> $TS_OUTPUT 2>> $TS_ERRLOG
55 echo "exit value: $?" >> $TS_OUTPUT
56 sed -i '1,1s/.*\///' $TS_OUTPUT
57 gnu_getopt_clean
58 ts_finalize_subtest
59
60 ts_init_subtest invocation_model_one
61 $TS_CMD_GETOPT abc:d::e::f::g: ARG0 -b ARG1 -d -eOPTIONAL1 -f NOT_OPTIONAL ARG2 -a -c -ISREQUIRED ARG3 -g "BECOMES THREE ARGUMENTS" >> $TS_OUTPUT 2>> $TS_ERRLOG
62 echo "exit value: $?" >> $TS_OUTPUT
63 gnu_getopt_clean
64 ts_finalize_subtest
65
66 ts_init_subtest invocation_model_three_as_one
67 $TS_CMD_GETOPT -o abc:d::e::f::g: -- ARG0 -b ARG1 -d -eOPTIONAL1 -f NOT_OPTIONAL ARG2 -a -c -ISREQUIRED ARG3 -g "BECOMES THREE ARGUMENTS" >> $TS_OUTPUT 2>> $TS_ERRLOG
68 echo "exit value: $?" >> $TS_OUTPUT
69 gnu_getopt_clean
70 ts_finalize_subtest
71
72 ts_init_subtest invocation_model_two_as_one
73 $TS_CMD_GETOPT -- abc:d::e::f::g: ARG0 -b ARG1 -d -eOPTIONAL1 -f NOT_OPTIONAL ARG2 -a -c -ISREQUIRED ARG3 -g "BECOMES THREE ARGUMENTS" >> $TS_OUTPUT 2>> $TS_ERRLOG
74 echo "exit value: $?" >> $TS_OUTPUT
75 gnu_getopt_clean
76 ts_finalize_subtest
77
78 ts_init_subtest invocation_without_parameters
79 $TS_CMD_GETOPT >> $TS_OUTPUT 2>> $TS_ERRLOG
80 echo "exit value: $?" >> $TS_OUTPUT
81 gnu_getopt_clean
82 ts_finalize_subtest
83
84 ts_init_subtest long_option_ambiguous_1
85 $TS_CMD_GETOPT -o a -l long1,long2 -- --long >> $TS_OUTPUT 2>> $TS_ERRLOG
86 echo "exit value: $?" >> $TS_OUTPUT
87 sed -i -e '1,1s/.*\///' -e "1s/ambiguous$/ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
88 sed -i -e "1s/is ambiguous: long$/'--long' is ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
89 gnu_getopt_clean
90 ts_finalize_subtest
91
92 ts_init_subtest longopts
93 $TS_CMD_GETOPT -o a -l long1,secondlong:,thirdlong::,abbreviation: -- --long1 EXTRA1 --thirdlong=ARGUMENT --secondlong=ARG --abbr ABBRARG -a >> $TS_OUTPUT 2>> $TS_ERRLOG
94 echo "exit value: $?" >> $TS_OUTPUT
95 gnu_getopt_clean
96 ts_finalize_subtest
97
98 ts_init_subtest name_option_long
99 $TS_CMD_GETOPT -n THIS_PROGRAM -o a -l long -- -b --long --whatever -a >> $TS_OUTPUT 2>> $TS_ERRLOG
100 echo "exit value: $?" >> $TS_OUTPUT
101 gnu_getopt_clean
102 ts_finalize_subtest
103
104 ts_init_subtest name_option_short
105 $TS_CMD_GETOPT --name THIS_PROGRAM -o a -l long -- -b --long --whatever -a >> $TS_OUTPUT 2>> $TS_ERRLOG
106 echo "exit value: $?" >> $TS_OUTPUT
107 gnu_getopt_clean
108 ts_finalize_subtest
109
110 ts_init_subtest quiet_option_long
111 $TS_CMD_GETOPT --quiet -o a,b: -l long1,long2 -- -c --unknown --long -b >> $TS_OUTPUT 2>> $TS_ERRLOG
112 echo "exit value: $?" >> $TS_OUTPUT
113 gnu_getopt_clean
114 ts_finalize_subtest
115
116 ts_init_subtest quiet_option_short
117 $TS_CMD_GETOPT -q -o a,b: -l long1,long2 -- -c --unknown --long -b >> $TS_OUTPUT 2>> $TS_ERRLOG
118 echo "exit value: $?" >> $TS_OUTPUT
119 gnu_getopt_clean
120 ts_finalize_subtest
121
122 ts_init_subtest quiet_output_option_long
123 $TS_CMD_GETOPT --quiet-output -o a,b: -l long1,long2 -- -c --unknown --long -b >> $TS_OUTPUT 2>> $TS_ERRLOG
124 echo "exit value: $?" >> $TS_OUTPUT
125 sed -i -e '1,4s/.*\///' -e "3s/ambiguous$/ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
126 sed -i -e "3s/is ambiguous: long$/'--long' is ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
127 gnu_getopt_clean
128 ts_finalize_subtest
129
130 ts_init_subtest quiet_output_option_short
131 $TS_CMD_GETOPT -Q -o a,b: -l long1,long2 -- -c --unknown --long -b >> $TS_OUTPUT 2>> $TS_ERRLOG
132 echo "exit value: $?" >> $TS_OUTPUT
133 sed -i -e '1,4s/.*\///' -e "3s/ambiguous$/ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
134 sed -i "3s/is ambiguous: long$/'--long' is ambiguous; possibilities: '--long1' '--long2'/" $TS_OUTPUT
135 gnu_getopt_clean
136 ts_finalize_subtest
137
138 ts_init_subtest same_long_short_options
139 $TS_CMD_GETOPT -o a -l a -- -a --a >> $TS_OUTPUT 2>> $TS_ERRLOG
140 echo "exit value: $?" >> $TS_OUTPUT
141 gnu_getopt_clean
142 ts_finalize_subtest
143
144 ts_init_subtest test_for_enhanced_getopt
145 $TS_CMD_GETOPT -T >> $TS_OUTPUT 2>> $TS_ERRLOG
146 echo "exit value: $?" >> $TS_OUTPUT
147 gnu_getopt_clean
148 ts_finalize_subtest
149
150 ts_init_subtest unknown_options
151 $TS_CMD_GETOPT -o a -l long -- -b --long --whatever -a >> $TS_OUTPUT 2>> $TS_ERRLOG
152 echo "exit value: $?" >> $TS_OUTPUT
153 sed -i '1,2s/.*\///' $TS_OUTPUT
154 gnu_getopt_clean
155 ts_finalize_subtest
156
157 ts_init_subtest unquoted_option_bash
158 $TS_CMD_GETOPT -s bash -u -o a: -l long1: -- -a '$?!* ' --long \"\\\' 'More
159 than
160 one
161 line' >> $TS_OUTPUT 2>> $TS_ERRLOG
162 echo "exit value: $?" >> $TS_OUTPUT
163 gnu_getopt_clean
164 ts_finalize_subtest
165
166 ts_init_subtest unquoted_option_tcsh
167 $TS_CMD_GETOPT -s tcsh --unquoted -o a: -l long1: -- -a '$?!* ' --long \"\\\' 'More
168 than
169 one
170 line' >> $TS_OUTPUT 2>> $TS_ERRLOG
171 echo "exit value: $?" >> $TS_OUTPUT
172 gnu_getopt_clean
173 ts_finalize_subtest
174
175 ts_init_subtest weird_quoting_bash
176 $TS_CMD_GETOPT -s bash -o a: -l long1: -- -a '$?!* ' --long \"\\\' 'More
177 than
178 one
179 line' >> $TS_OUTPUT 2>> $TS_ERRLOG
180 echo "exit value: $?" >> $TS_OUTPUT
181 gnu_getopt_clean
182 ts_finalize_subtest
183
184 ts_init_subtest weird_quoting_tcsh
185 $TS_CMD_GETOPT --shell tcsh -o a: -l long1: -- -a '$?!* ' --long \"\\\' 'More
186 than
187 one
188 line' >> $TS_OUTPUT 2>> $TS_ERRLOG
189 echo "exit value: $?" >> $TS_OUTPUT
190 gnu_getopt_clean
191 ts_finalize_subtest
192
193 ts_init_subtest compatible
194 $TS_CMD_GETOPT >> $TS_OUTPUT 2>> $TS_ERRLOG
195 echo "exit value: $?" >> $TS_OUTPUT
196 GETOPT_COMPATIBLE='' $TS_CMD_GETOPT >> $TS_OUTPUT 2>> $TS_ERRLOG
197 echo "exit value: $?" >> $TS_OUTPUT
198 sed -i '1,1s/.*\///' $TS_OUTPUT
199 ts_finalize_subtest
200
201 ts_init_subtest sh
202 $TS_CMD_GETOPT --shell=sh -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.sh' -- -a -b 1 -c2 --a-long --b-long 3 --c-long foo bar >> $TS_OUTPUT 2>> $TS_ERRLOG
203 echo "exit value: $?" >> $TS_OUTPUT
204 ts_finalize_subtest
205
206 ts_init_subtest csh
207 $TS_CMD_GETOPT --shell=csh -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.csh' -- -a -b 1 -c2 --a-long --b-long 3 --c-long foo bar >> $TS_OUTPUT 2>> $TS_ERRLOG
208 echo "exit value: $?" >> $TS_OUTPUT
209 ts_finalize_subtest
210
211 ts_init_subtest no-arguments
212 $TS_CMD_GETOPT >> $TS_OUTPUT 2>> $TS_ERRLOG
213 echo "exit value: $?" >> $TS_OUTPUT
214 sed -i '1,1s/.*\///' $TS_OUTPUT
215 ts_finalize_subtest
216
217 ts_init_subtest posix_correctly
218 $TS_CMD_GETOPT -o +ab:c:: --long a-long,b-long:,c-long:: -n 'example' -- -a -b 1 -c2 --a-long --b-long 3 --c-long foo bar xyxxy --a-long >> $TS_OUTPUT 2>> $TS_ERRLOG
219 echo "exit value: $?" >> $TS_OUTPUT
220 POSIXLY_CORRECT='' $TS_CMD_GETOPT -o ab:c:: --long a-long,b-long:,c-long:: -n 'example' -- -a -b 1 -c2 --a-long --b-long 3 --c-long foo bar xyxxy --a-long >> $TS_OUTPUT 2>> $TS_ERRLOG
221 echo "exit value: $?" >> $TS_OUTPUT
222 ts_finalize_subtest
223
224 ts_init_subtest non-option
225 $TS_CMD_GETOPT -o +a:b: -- -a x y -b z w >> $TS_OUTPUT 2>> $TS_ERRLOG
226 $TS_CMD_GETOPT -o a:b: -- -a x y -b z w >> $TS_OUTPUT 2>> $TS_ERRLOG
227 $TS_CMD_GETOPT -o -a:b: -- -a x y -b z w >> $TS_OUTPUT 2>> $TS_ERRLOG
228 echo "exit value: $?" >> $TS_OUTPUT
229 ts_finalize_subtest
230
231 ts_finalize