Experimental package creation tool for MinGW.org
リビジョン | 8fa3b46617399a6fa97c44d7d018d91f9012374b (tree) |
---|---|
日時 | 2018-05-20 06:48:12 |
作者 | Keith Marshall <keith@user...> |
コミッター | Keith Marshall |
Diagnose arguments to options which don't support them.
* src/modules/optdefn.sh (optchk_getopt_long) [--*=*]: Check...
[${optmatch}_optional_argument]: ...this; it must be true, otherwise
diagnose an attempt to specify an illegitimate option argument, and
abort. The dynamically formatted diagnostic message requires...
(optstring_get_name): ...this new helper function; implement it,
favouring POSIX.1 parameter substring expansion, but fall back to
subprocess IFS word splitting, if such expansion is unsupported.
(optstring_get_value, opstring_get_optchr, optstring_get_remnant):
Similarly, implement these new helper functions, favouring POSIX.1
parameter expansion over subprocess forking to run "sed" or "cut"
commands; use them, in place of such commands, as previously used...
(optchk, optchk_getopt_long): ...in each of these functions.
@@ -31,6 +31,36 @@ | ||
31 | 31 | # |
32 | 32 | # ----------------------------------------------------------------------------- |
33 | 33 | # |
34 | + if ( foo=foo=bar=baz; test "${foo%%=*}" = foo ) > /dev/null 2>&1 | |
35 | + then | |
36 | + # Option string manipulator (helper) functions, for separation of | |
37 | + # option name and optarg value parts of compound option strings. | |
38 | + # | |
39 | + # To avoid forking, prefer POSIX.1 variable substring extractors, | |
40 | + # when they are supported; for long form option parsing, and for | |
41 | + # variable assignment... | |
42 | + # | |
43 | + optstring_get_name(){ echo "${1%%=*}"; } | |
44 | + optstring_get_value(){ echo "${1#*=}"; } | |
45 | + | |
46 | + # ...and for short form option string interpretation. | |
47 | + # | |
48 | + optstring_get_optchr(){ echo ${1%"${1#?}"}; } | |
49 | + optstring_get_remnant(){ echo "${1#?}"; } | |
50 | + | |
51 | + else | |
52 | + # Ultimately, more portable, but may require forking "sed" | |
53 | + # for long form manipulations... | |
54 | + # | |
55 | + optstring_get_name(){ ( IFS='='; set -- $1; echo $1 ); } | |
56 | + optstring_get_value(){ echo "$1" | sed 's,^[^=]*=,,'; } | |
57 | + | |
58 | + # ...and "cut", for short form. | |
59 | + # | |
60 | + optstring_get_optchr(){ echo "$1" | cut -c1; } | |
61 | + optstring_get_remnant(){ echo "$1" | cut -c2-; } | |
62 | + fi | |
63 | + | |
34 | 64 | optdefine(){ |
35 | 65 | # Usage: shortname [longname] [argument_property] |
36 | 66 | # or: longname [shortname] [argument_property] |
@@ -61,7 +91,7 @@ | ||
61 | 91 | # |
62 | 92 | die "optdefine: '$optname' not allowed after '$argmode'" |
63 | 93 | } |
64 | - test "x`echo $optname | cut -c1`" = "x$optname" && { | |
94 | + test x`optstring_get_optchr "$optname"` = "x$optname" && { | |
65 | 95 | # At most, one short option name argument is permitted, and |
66 | 96 | # it may not collide with any already defined. |
67 | 97 | # |
@@ -134,7 +164,7 @@ | ||
134 | 164 | |
135 | 165 | optunhandled(){ |
136 | 166 | # Default "opteval" interpreter; if the user neglects to define an |
137 | - # interpreter for any defined option, this will diagnose the ommision, | |
167 | + # interpreter for any defined option, this will diagnose the omission, | |
138 | 168 | # at eventual point of use. |
139 | 169 | # |
140 | 170 | die "'$1':option '$optmatch' declared but no handler implemented" |
@@ -186,7 +216,7 @@ | ||
186 | 216 | # unambiguous long form interpretation has been identified; if not, |
187 | 217 | # prepare $arg for short form interpretation... |
188 | 218 | # |
189 | - test "x$optmatch" = x && arg=`echo $1 | cut -c2-` || { | |
219 | + test "x$optmatch" = x && arg=`optstring_get_remnant $1` || { | |
190 | 220 | # ...whereas, in the case of successful long form identification, |
191 | 221 | # we clear $arg, to indicate that no further option identification |
192 | 222 | # is required, and complete long form option evaluation, (which is |
@@ -253,8 +283,8 @@ | ||
253 | 283 | # the variable name and value components, complete the assignment, |
254 | 284 | # then continue scanning for further options, or assignments. |
255 | 285 | # |
256 | - optarg="`echo "$1" | sed 's,^[^=]*=,,'`" | |
257 | - arg="`IFS='='; set -- $1; defn_parameter_list $1`" | |
286 | + arg=`optstring_get_name "$1"` | |
287 | + optarg=`optstring_get_value "$1"` | |
258 | 288 | eval $arg='"$optarg"' |
259 | 289 | ;; |
260 | 290 | *) # Anything else is just a regular argument; leave it for subsequent |
@@ -278,8 +308,8 @@ | ||
278 | 308 | # Any option with an assigned value: separate the |
279 | 309 | # name and value parts. |
280 | 310 | # |
281 | - *=*) optarg="`echo "$arg" | sed 's,^[^=]*=,,'`" | |
282 | - arg="`IFS='='; set -- $arg; defn_parameter_list $1`" | |
311 | + *=*) optarg=`optstring_get_value "$arg"` | |
312 | + arg=`optstring_get_name "$arg"` | |
283 | 313 | argshift=1 |
284 | 314 | ;; |
285 | 315 | # North american users don't distinguish "licence" |
@@ -347,8 +377,13 @@ | ||
347 | 377 | # |
348 | 378 | $optchk_ambiguous_match && die "option '$1' is ambiguous" |
349 | 379 | |
350 | - # ...otherwise, a successfully matched option is evaluated here. | |
380 | + # ...otherwise, a successfully matched option is evaluated here, | |
381 | + # (subject to checking that no unexpected optarg was attached). | |
351 | 382 | # |
383 | + case $1 in *=*) | |
384 | + eval '${'$optmatch'_optional_argument-false}' || | |
385 | + die "option '`optstring_get_name $1`' does not accept an argument" ;; | |
386 | + esac | |
352 | 387 | opteval "$@" |
353 | 388 | fi |
354 | 389 | } |