Node:Using Getopt, Next:Example of Getopt, Up:Getopt
getopt
functionHere are the details about how to call the getopt
function. To
use this facility, your program must include the header file
unistd.h
.
int opterr | Variable |
If the value of this variable is nonzero, then getopt prints an
error message to the standard error stream if it encounters an unknown
option character or an option with a missing required argument. This is
the default behavior. If you set this variable to zero, getopt
does not print any messages, but it still returns the character ?
to indicate an error.
|
int optopt | Variable |
When getopt encounters an unknown option character or an option
with a missing required argument, it stores that option character in
this variable. You can use this for providing your own diagnostic
messages.
|
int optind | Variable |
This variable is set by getopt to the index of the next element
of the argv array to be processed. Once getopt has found
all of the option arguments, you can use this variable to determine
where the remaining non-option arguments begin. The initial value of
this variable is 1 .
|
char * optarg | Variable |
This variable is set by getopt to point at the value of the
option argument, for those options that accept arguments.
|
int getopt (int argc, char **argv, const char *options) | Function |
The getopt function gets the next option argument from the
argument list specified by the argv and argc arguments.
Normally these values come directly from the arguments received by
main .
The options argument is a string that specifies the option
characters that are valid for this program. An option character in this
string can be followed by a colon (
The If the option has an argument, If |