NAME | SYNOPSIS | DESCRIPTION | RETURN VALUES | EXAMPLES | RESTRICTIONS | ATTRIBUTES | SEE ALSO
#include <stdlib.h>int getopt(int argc, char *const *argv, const char *optstring);
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
The getopt function incrementally parses the command line argument list argv and returns the next known option letter. An option letter is known if it has been specified in the string of accepted option letters, optstring.
The option string optstring can contain individual characters and characters followed by a colon indicating that an option argument follows. For example, an option string x indicates an option -x, and an option string x: indicates an option that has an argument, -x argument. It does not matter whether an argument has leading white space., that is --xarg and --x arg are interpreted as being the same.
On return from getopt, optarg points to an option argument, if one is expected, and the variable optind contains the index to the next argv argument for a subsequent call to getopt. The variable optopt saves the last known option letter returned by getopt.
The variables opterr and optind are both initialized to 1. The optind variable may be set to another value before a set of calls to getopt in order to access any given argv entry. In other words, you do not have to process the argv entries in order.
In order to use getopt to evaluate multiple sets of arguments, or to evaluate a single set of arguments several times, the variable optreset must be set to 1 before the second and each additional set of calls to getopt, and the variable optind must be reinitialized.
The getopt function returns an EOF when the argument list is exhausted, or a non-recognized option is encountered. The interpretation of options in the argument list may be cancelled by the option "--" (double dash) which causes getopt to signal the end of argument processing and return an EOF. When all options have been processed (up to the first non-option argument), getopt returns EOF.
If the getopt function encounters a character not found in the string optstring or detects a missing option argument, it prints an error message and returns "?" to stderr. Setting opterr to a zero will disable these error messages. If optstring has a leading ":" then a missing option argument causes a ":" to be returned in addition to suppressing any error messages.
Option arguments are allowed to begin with "-", which reduces the amount of error checking possible.
#include <stdlib.h> #include <stdio.h> main (int argc, char **argv) { int bflag= 0; char* fname; int ch, fd; while ((ch = getopt(argc, argv, "bf:")) != EOF) { switch(ch) { case 'b': bflag = 1; break; case 'f': fname = optarg; break; case '?': default: fprintf(stderr, "usage: cmd [-b] [-f <arg>] \n"); exit(1); } } } argc -= optind; argv += optind; |
The getopt function is not thread safe.
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
---|---|
Interface Stability | Evolving |
NAME | SYNOPSIS | DESCRIPTION | RETURN VALUES | EXAMPLES | RESTRICTIONS | ATTRIBUTES | SEE ALSO