comparison tests/common.t @ 809:6cf3e1021862

common.subr: Change handling of internal errors. They now - return 64 or 70 - print always an error message to stderr - set the given option variable to the null value - unset OPTARG in *every* case. This is independent whether the short options start with an `:' character or not.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 05 Nov 2024 18:42:40 +0100
parents 2cd233b137ec
children
comparison
equal deleted inserted replaced
808:ab21dd56f99e 809:6cf3e1021862
44 $ checkyesno opt_a 44 $ checkyesno opt_a
45 $ checkyesno opt_b 45 $ checkyesno opt_b
46 $ [ "${opt_b_val}" = "value-of-b" ] 46 $ [ "${opt_b_val}" = "value-of-b" ]
47 $ [ -z "${error}" ] 47 $ [ -z "${error}" ]
48 48
49 Not null-terminated long options speccifications 49 Not null-terminated long options specifications
50 50
51 $ OPTIND=1 51 $ OPTIND=1
52 > getopts "ab:-:" opt --long-a --long-b arg 52 > getopts "ab:-:" opt --long-a --long-b arg
53 > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b" 53 > postprocess_getopts_for_long "ab:-:" opt "long-a" "long-b"
54 /bin/sh: ERROR: Missing null terminator for long option specifications 54 /bin/sh: ERROR: Missing null terminator for long option specifications
55 [1] 55 [64]
56 $ [ "${opt}" = '?' ] 56 $ [ -z "${opt}" ]
57 $ [ -z "${OPTARG+SET}" ] 57 $ [ -z "${OPTARG+SET}" ]
58 58
59 Not null-terminated long options specs (alternate error handling) 59 Not null-terminated long options specs (alternate error handling)
60 60
61 $ OPTIND=1 61 $ OPTIND=1
62 > getopts ":ab:-:" opt --long-a --long-b arg 62 > getopts ":ab:-:" opt --long-a --long-b arg
63 > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b" 63 > postprocess_getopts_for_long ":ab:-:" opt "long-a" "long-b"
64 [1] 64 /bin/sh: ERROR: Missing null terminator for long option specifications
65 $ [ "${opt}" = ':' ] 65 [64]
66 $ [ -n "${OPTARG+SET}" ] 66 $ [ -z "${opt}" ]
67 $ [ -z "${OPTARG}" ] 67 $ [ -z "${OPTARG+SET}" ]
68 68
69 Illegal long option 69 Illegal long option
70 70
71 $ error='' 71 $ error=''
72 > opt_a=no 72 > opt_a=no
232 $ [ "${error}" = "long-a" ] 232 $ [ "${error}" = "long-a" ]
233 233
234 Invalid character in long option 234 Invalid character in long option
235 235
236 $ error='' 236 $ error=''
237 > opt_a=no 237 > OPTIND=1
238 > OPTIND=1 238 > getopts "ab:-:" opt --long=a
239 > while getopts "ab:-:" opt --long=a ; do 239 > postprocess_getopts_for_long "ab:-:" opt "long=a" "" --long=a arg
240 > postprocess_getopts_for_long "ab:-:" opt "long=a" "" --long=a arg
241 > case "$opt" in
242 > a|long=a) opt_a=yes
243 > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG"
244 > ;;
245 > \?)
246 > error=foo
247 > ;;
248 > *)
249 > error=bar
250 > ;;
251 > esac
252 > done
253 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=a 240 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=a
254 $ [ "${error}" = "foo" ] 241 [64]
242 $ [ -z "${opt}" ]
243 $ [ -z "${OPTARG+SET}" ]
255 244
256 Invalid character in long option (alternate error handling) 245 Invalid character in long option (alternate error handling)
257 246
258 $ error='' 247 $ error=''
259 > opt_a=no 248 > OPTIND=1
260 > OPTIND=1 249 > getopts ":a-:" opt --long=a
261 > while getopts ":a-:" opt --long=a ; do 250 > postprocess_getopts_for_long ":a-:" opt "long=a" "" --long=a arg
262 > postprocess_getopts_for_long ":a-:" opt "long=a" "" --long=a arg 251 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=a
263 > case "$opt" in 252 [64]
264 > a|long=a) opt_a=yes 253 $ [ -z "${opt}" ]
265 > [ -z "${OPTARG+SET}" ] || echo "ERROR: OPTARG" 254 $ [ -z "${OPTARG+SET}" ]
266 > ;;
267 > \?)
268 > error="${OPTARG}"
269 > ;;
270 > *)
271 > error=bar
272 > ;;
273 > esac
274 > done
275 $ [ "${error}" = "long=a" ]
276 255
277 Invalid character in long option 256 Invalid character in long option
278 257
279 $ error='' 258 $ error=''
280 > opt_b=no 259 > OPTIND=1
281 > OPTIND=1 260 > getopts "b:-:" opt --long=a
282 > while getopts "b:-:" opt --long=a ; do 261 > postprocess_getopts_for_long "b:-:" opt "long=b=" "" --long=b=value arg
283 > postprocess_getopts_for_long "b:-:" opt "long=b=" "" --long=b=value arg
284 > case "$opt" in
285 > b|long=b) opt_b=yes
286 > ;;
287 > \?)
288 > error=foo
289 > ;;
290 > *)
291 > error=bar
292 > ;;
293 > esac
294 > done
295 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=b= 262 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=b=
296 $ [ "${error}" = "foo" ] 263 [64]
264 $ [ -z "${opt}" ]
265 $ [ -z "${OPTARG+SET}" ]
297 266
298 Invalid character in long option (alternate error handling) 267 Invalid character in long option (alternate error handling)
299 268
300 $ error='' 269 $ error=''
301 > opt_b=no 270 > OPTIND=1
302 > OPTIND=1 271 > getopts ":b:-:" opt --long=a
303 > while getopts ":b:-:" opt --long=a ; do 272 > postprocess_getopts_for_long ":b:-:" opt "long=b=" "" --long=b=value arg
304 > postprocess_getopts_for_long ":b:-:" opt "long=b=" "" --long=b=value arg 273 /bin/sh: ERROR: Long option specification contains a forbidden `=' character: long=b=
305 > case "$opt" in 274 [64]
306 > b|long=b) opt_b=yes 275 $ [ -z "${opt}" ]
307 > ;; 276 $ [ -z "${OPTARG+SET}" ]
308 > \?)
309 > error="${OPTARG}"
310 > ;;
311 > *)
312 > error=bar
313 > ;;
314 > esac
315 > done
316 $ [ "${error}" = "long=b=" ]