getopt

Name

getopt -- parse command line options

Synopsis

#include <unistd.h>

int getopt(int argc, char * const argv[], const char * optstring);

extern char *optarg;
extern int optind, opterr, optopt;

Description

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.

Argument Ordering

The getopt() function can process command line arguments referenced by argv in one of three ways:

PERMUTE 

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.

Note: 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.

REQUIRE_ORDER 

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 '+'.

RETURN_IN_ORDER 

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 '-';

Option Characteristics

LSB specifies that:

POSIX specifies that:

Extensions

LSB specifies that:

POSIX specifies that:

Return Values

LSB specifies the following additional getopt() return values:

Any other return value has the same meaning as for POSIX.

POSIX specifies the following getopt() return values:

Environment Variables

LSB specifies that: