Options can take arguments and environment variables can have values
(see Parsing the Environment). Some options and environment
variables require arguments or values, while others may optionally take
arguments or have values, while still others might take no arguments at
all or not allow any values. Mu supports all of these types of options
and environment variables through the has_arg
field of the
MU_OPT
structure (see Option Structure).
MU_OPT_NONE
¶This indicates that an option takes no arguments. If the
MU_OPT_BUNDLE
flag (see Option Parsing Flags) is not
specified, then any text following a short option which doesn’t take an
argument will be an error.3 An ‘=’ is an
error in a long option that does not take an argument, because ‘=’
is used to specify arguments to long options.
*has_arg
will always be set to 0
if has_arg
is not
NULL
.
MU_OPT_OPTIONAL
¶This indicates that an option may take an argument, but that the option
doesn’t require an argument. Even if the MU_OPT_BUNDLE
flag is
passed (see Option Parsing Flags), short options with optional
arguments may not be bundled except as the last option in a bundle. The
reason for this is as follows: Suppose short option b takes an
optional argument. And suppose short options a and c
take no argument. Now what should -abc mean (assuming
MU_OPT_BUNDLE
was passed)? Is it three options without arguments,
a, b, and c? Or is it two options, a
and b, the latter of which taking an argument, ‘c’? It is
in fact the latter, two options a and b, with
b taking an argument, ‘c’. Note, however, that if
b is specified as the last option like so: -acb, there
is no ambiguity, because there is nothing following the b
option (and if there is text following it in another argument, it will
be treated as a positional argument; see below).
Short options taking an optional argument must have their arguments specified with the option itself. For example, if a short option, b, takes an optional argument, it must be specified as -barg, not -b arg. The reason for this is because -b arg could be a short option b with an argument arg, or a short option b with no argument, and a positional argument arg. Likewise, long options taking optional arguments must be specified as --long=arg, not --long arg.
If has_arg
is not NULL
, then *has_arg
will be set
to 1
if the option has an argument, or 0
if the option
doesn’t have an argument.
MU_OPT_REQUIRED
¶This indicates that an option requires an argument. If no argument is specified, it is an error. Like short options with optional arguments, short options with required arguments may not be bundled except as the last option in a bundle. See above for an explanation.
For short options with required arguments, the argument may be passed with the option itself like so: -rarg, or immediately after the argument like so: -r arg. Arguments to long options with required arguments may also be specified with the option itself like so: --required=arg, or immediately after the argument like so: --required arg.
*has_arg
will always be set to 1
if has_arg
is not
NULL
.
But only if the text following the short option is in the same argument as the option itself, e.g., -ofoo, not -o foo. If the following text is in a new argument, as in the latter case, it will be treated as a positional argument, not an argument to -o.