The getopt
function shall parse command line arguments
as described in ISO POSIX (2003), with the following exceptions,
where LSB and POSIX specifications vary.
LSB systems shall
implement the modified behaviors described below.
The getopt
function can process command line arguments
referenced by argv in one of three ways:
the order of arguments in argv is altered so that all options (and their arguments) are moved in front of all of the operands. This is the default behavior.
This behavior has undefined results if argv is not modifiable. This is to support historic behavior predating the use of const and ISO C (1999). The function prototype was aligned with ISO POSIX (2003) despite the fact that it modifies argv, and the library maintainers are unwilling to change this. |
The arguments in argv are processed in exactly the order given, and option processing stops when the first non-option argument is reached, or when the element of argv is "--". This ordering can be enforced either by setting the environment variable POSIXLY_CORRECT, or by setting the first character of optstring to '+'.
The order of arguments is not altered, and all arguments are processed. Non-option arguments (operands) are handled as if they were the argument to an option with the value 1 ('\001'). This ordering is selected by setting the first character of optstring to '-';
LSB specifies that:
an element of argv that starts with "-" (and is not exactly "-" or "--") is an option element.
characters of an option element, aside from the initial "-", are option characters.
POSIX specifies that:
applications using getopt
shall obey the following
syntax guidelines:
option name is a single alphanumeric character from the portable character set
option is preceded by the '-' delimiter character
options without option-arguments should be accepted when grouped behind one '-' delimiter
each option and option-argument is a separate argument
option-arguments are not optional
all options should precede operands on the command line
the argument "--" is accepted as a delimiter indicating the end of options and the consideration of subsequent arguments, if any, as operands
historical implementations of getopt
support
other characters as options as an allowed extension, but applications
that use extensions are not maximally portable.
support for multi-byte option characters is only possible when such characters can be represented as type int.
applications that call any utility with a first operand starting with '-' should usually specify "--" to mark the end of the options. Standard utilities that do not support this guideline indicate that fact in the OPTIONS section of the utility description.
LSB specifies that:
if a character is followed by two colons, the option takes an optional argument; if there is text in the current argv element, it is returned in optarg, otherwise optarg is set to 0.
if optstring contains W followed by a semi-colon (;), then -W foo is treated as the long option --foo.
See |
The first character of optstring shall modify
the behavior of getopt
as follows:
if the first character is '+', then
REQUIRE_ORDER
processing shall be in
effect (see above)
if the first character is '-', then
RETURN_IN_ORDER
processing shall be in
effect (see above)
if the first character is ':', then
getopt
shall return ':' instead of '?'
to indicate a missing option argument, and shall not print any
diagnostic message to stderr.
POSIX specifies that:
the -W option is reserved for implementation extensions.
LSB specifies the following additional
getopt
return values:
'\001' is returned
if RETURN_IN_ORDER
argument ordering is in effect,
and the next argument is an operand, not an option. The argument is
available in optarg.
POSIX specifies the following
getopt
return values:
the next option character is returned, if found successfully.
':' is returned if a parameter is missing for one of the options and the first character of optstring is ':'.
'?' is returned if an unknown option
character not in optstring is encountered, or if
getopt
detects a missing argument and the first
character of optstring is not ':'.
-1 is returned for the end of the option list.
LSB specifies that:
if the variable POSIXLY_CORRECT is set, option processing stops as soon as a non-option argument is encountered.
the variable _[PID]_GNU_nonoption_argv_flags_ (where [PID] is the process ID for the current process), contains a space separated list of arguments that should not be treated as arguments even though they appear to be so.
Rationale | |
---|---|
This was used by bash 2.0 to communicate to GNU libc which arguments resulted from wildcard expansion and so should not be considered as options. This behavior was removed in bash version 2.01, but the support remains in GNU libc. |