Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

getopt, optarg, optind, opterr, optopt - command option parsing

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

int getopt(int argc, char * const argv[ ], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt; 

Description

getopt() is a command-line parser that can be used by applications that follow the specific

conventions for entering commands defined in the XPG4 specification (see the manual "POSIX Commands" [2]. The remaining guidelines are the responsibility of the application.

getopt() returns the next option character from argv that matches a character in optstring.

argc is the argument count, as passed to main() (see exec).

argv points to an array of argc +1 elements containing argc pointers to character strings, followed by a null pointer. It contains the option names, as passed to main() (see exec).

optstring is a string of recognized option characters (see the manual "POSIX Commands" [2]). If a character in this string is followed by a colon (:), the option is expected to take one or more arguments.

optind is an external variable that represents the index of the next element of the argv [] vector to be evaluated. It is initialized to 1 by the system, and getopt() updates it when it finishes evaluating each element of argv []. If an element of argv [] contains multiple option characters, it is unspecified how getopt() determines which options have already been processed.

optarg is an external variable that is set by getopt() when an option takes an argument. This is done as follows.

  1. If the option was the last character in the string pointed to by an element of argv, then optarg contains the next element of argv, and optind is incremented by 2. If the resulting value of optind is not less than argc, this indicates a missing option-argument, and getopt() reports an error.

  2. Otherwise, optarg is set to point to the string following the option character, and optind is incremented by 1.

opterr is an external variable that controls the output of error messages in the event of an error. If it is set to 0, the output of an error message is suppressed.

optopt is an external variable containing the option character that caused getopt() to fail.

Return val.

Next option character from the command line



upon successful completion.


:

if an option-argument is missing and the first character in optstring was a colon; getopt() sets the variable optopt to the option character that caused the error.


?

if an option character that is not contained in optstring is found or if an option-argument is missing and the first character in optstring was not a colon or if the next option character is the question mark (?) from the command line. In these cases, getopt() sets the variable optopt to the option character that caused the error. If the application has not set the variable opterr to 0, getopt() prints a diagnostic message to stderr in the format specified for the getopt()s command (see also the manual "POSIX Commands" [2]).

An error has occurred only if the optopt variable does not contain a question mark (?). Otherwise the question mark is the next option character from the command line, and the function was concluded successfully.


-1

if argv [optind] is a null pointer, or
if *argv [optind] is not the character "–" , or
if argv [optind] points to the string "–";
optind is not changed In these cases.


-1

if argv [optind] points to the string "– –".
optind is incremented.

Notes

getopt() does not fully check for mandatory arguments. That is, given an option string a:b and the input -a -b, getopt() will assume that -b is the mandatory argument for option -a and not that a mandatory argument is missing for -a.

Multiple options cannot be combined if the last option requires an argument. For example, if a and b are normal options and option o requires the argument xxx, then cmd -ab -o xxx should be specified, not cmd -abo xxx. Although the latter grouped syntax is still supported by the current implementation, it may not be supported in future releases.

BS2000
When a program is started in the BS2000 environment, the program parameters are supplied as described in the manual "C/C++ Compiler" [4]. (End)

If the integer value returned by getchar() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, since no sign-extension of a variable of type char on widening to integer occurs. 

See also

exec, unistd.h, getopts command (see also the manual "POSIX Commands" [2]).