#include <unistd.h> int getopt(int argc, char * const *argv, const char *options); extern char *optarg; extern int optind, opterr; extern char optopt;
Parse options from the command line. The options are a string of valid option characters. If a given option takes a parameter, that character should be followed by a colon.
For each valid switch, this function sets optarg
to the argument
(if the switch takes one), sets optind
to the index in argv
that it is using, sets optopt
to the option letter found, and
returns the option letter found.
If an unexpected option is found, getopt
will return ?
,
and if opterr
is nonzero, will print an error message to stderr.
The special option --
indicates that no more options follow on
the command line, and cause getopt
to stop looking.
The option found, or -1 if no more options.
not ANSI, POSIX
int c; opterr = 0; while ((c=getopt(argc, argv, "vbf:")) != -1) { switch (c) { case 'v': verbose_flag ++; break; case 'b': binary_flag ++; break; case 'f': output_filename = optarg; break; case '?': printf("Unknown option %c\n", c); usage(); exit(1); } }
Go to the first, previous, next, last section, table of contents.