getopt#
Synopsis#
#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int opterr, optind, optopt;
Status#
Partially implemented
Conformance#
IEEE Std 1003.1-2017
Description#
The getopt()
function is a command-line parser that shall follow Utility Syntax Guidelines 3
, 4
, 5
, 6
, 7
,
9
, and 10
in XBD Utility Syntax Guidelines
.
The parameters argc and argv are the argument count and argument array as passed to main()
. The argument
optstring is a string of recognized option characters; if a character is followed by a <colon>
, the option
takes an argument. All option characters allowed by Utility Syntax Guideline 3
are allowed in optstring. The
implementation may accept other characters as an extension.
The variable optind is the index of the next element of the argv[]
vector to be processed. It shall be initialized
to 1
by the system, and getopt()
shall update it when it finishes with each element of argv[]
. If the application
sets optind to zero before calling getopt()
, the behavior is unspecified. When an element of argv[]
contains
multiple option characters, it is unspecified how getopt()
determines which options have already been processed.
The getopt()
function shall return the next option character (if one is found) from argv that matches a character in
optstring, if there is one that matches. If the option takes an argument, getopt()
shall set the variable optarg
to point to the option-argument as follows:
If the option was the last character in the string pointed to by an element of
argv
, then optarg shall contain the next element ofargv
, and optind shall be incremented by2
. If the resulting value of optind is greater thanargc
, this indicates a missing option-argument, andgetopt()
shall return an error indication.Otherwise, optarg shall point to the string following the option character in that element of argv, and optind shall be incremented by
1
.
If, when getopt()
is called:
argv[optind] is a null pointer
*argv[optind] is not the character -
argv[optind] points to the string "-"
getopt()
shall return -1
without changing optind. If:
argv[optind] points to the string "--"
getopt()
shall return -1
after incrementing optind.
If getopt()
encounters an option character that is not contained in optstring, it shall return the <question-mark>
('?'
) character. If it detects a missing option-argument, it shall return the <colon>
character (':'
) if the
first character of optstring was a <colon>
, or a <question-mark>
character ('?'
) otherwise. In either case,
getopt()
shall set the variable optopt to the option character that caused the error. If the application has not set
the variable opterr to 0
and the first character of optstring is not a <colon>
, getopt()
shall also print a
diagnostic message to stderr
in the format specified for the getopts
utility, unless the stderr
stream has wide
orientation, in which case the behavior is undefined
Return value#
The getopt()
function shall return the next option character specified on the command line.
A <colon>
(':'
) shall be returned if getopt()
detects a missing argument and the first character of optstring was
a <colon>
(':'
).
A <question-mark>
('?'
) shall be returned if getopt()
encounters an option character not in optstring or
detects a missing argument and the first character of optstring was not a <colon>
(':'
).
Otherwise, getopt()
shall return -1
when all command line options are parsed.
Errors#
If the application has not set the variable opterr to 0
, the first character of optstring is not a <colon>
, and a
write error occurs while getopt()
is printing a diagnostic message to stderr
, then the error indicator for stderr
shall be set; but getopt()
shall still succeed and the value of errno
after getopt()
is unspecified.
Tests#
Untested
Known bugs#
None