Here are the details about how to call the
getopt function. To
use this facility, your program must include the header file
getoptprints 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,
getoptdoes not print any messages, but it still returns the character
?to indicate an error.
getoptencounters 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.
getoptto the index of the next element of the argv array to be processed. Once
getopthas 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
getoptto point at the value of the option argument, for those options that accept arguments.
getoptfunction 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
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 (`:') to indicate that it takes a required argument.
If the options argument string begins with a hyphen (`-'), this is treated specially. It permits arguments that are not options to be returned as if they were associated with option character `\0'.
getopt function returns the option character for the next
command line option. When no more option arguments are available, it
-1. There may still be more non-option arguments; you
must compare the external variable
optind against the argc
parameter to check this.
If the option has an argument,
getopt returns the argument by
storing it in the variable optarg. You don't ordinarily need to
optarg string, since it is a pointer into the original
argv array, not into a static area that might be overwritten.
getopt finds an option character in argv that was not
included in options, or a missing option argument, it returns
`?' and sets the external variable
optopt to the actual
option character. If the first character of options is a colon
getopt returns `:' instead of `?' to
indicate a missing option argument. In addition, if the external
opterr is nonzero (which is the default),
prints an error message.
Go to the first, previous, next, last section, table of contents.