getopt
parses command line arguments.
GNU and POSIX specifications
for this function vary in the following areas. LSB systems shall
implement the GNU behaviors described below.
GNU 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.
GNU specifies that:
if a character is followed by two colons, the option takes an optional arg; 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 ;, then -W foo is treated as the long option --foo. (Not available with libraries before GNU libc 2.)
POSIX specifies that:
the -W option is reserved for implementation extensions.
GNU 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.
"?" is returned if an unknown option character is encountered.
-1 is returned for the end of the option list.
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 opstring 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.
GNU specifies that:
if the variable POSIXLY_CORRECT is set, option processing stops as soon as a non-option argument is encountered.
if POSIXLY_CORRECT is set, GNU
getopt
conforms to ISO/IEC 9945:2003 Portable Operating System(POSIX)and The Single UNIX® Specification(SUS) V3.
the variable _[PID]_GNU_nonoption_argv_flags_ 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.