Next: Getopt Long Option Example, Previous: Example of Getopt, Up: Getopt
getopt_longTo accept GNU-style long options as well as single-character options,
use getopt_long instead of getopt. This function is
declared in getopt.h, not unistd.h. You should make every
program accept long options if it uses any options, for this takes
little extra work and helps beginners remember how to use the program.
This structure describes a single long option name for the sake of
getopt_long. The argument longopts must be an array of these structures, one for each long option. Terminate the array with an element containing all zeros.The
struct optionstructure has these fields:
const char *name- This field is the name of the option. It is a string.
int has_arg- This field says whether the option takes an argument. It is an integer, and there are three legitimate values:
no_argument,required_argumentandoptional_argument.int *flagint val- These fields control how to report or act on the option when it occurs.
If
flagis a null pointer, then thevalis a value which identifies this option. Often these values are chosen to uniquely identify particular long options.If
flagis not a null pointer, it should be the address of anintvariable which is the flag for this option. The value invalis the value to store in the flag to indicate that the option was seen.
Decode options from the vector argv (whose length is argc). The argument shortopts describes the short options to accept, just as it does in
getopt. The argument longopts describes the long options to accept (see above).When
getopt_longencounters a short option, it does the same thing thatgetoptwould do: it returns the character code for the option, and stores the options argument (if it has one) inoptarg.When
getopt_longencounters a long option, it takes actions based on theflagandvalfields of the definition of that option.If
flagis a null pointer, thengetopt_longreturns the contents ofvalto indicate which option it found. You should arrange distinct values in thevalfield for options with different meanings, so you can decode these values aftergetopt_longreturns. If the long option is equivalent to a short option, you can use the short option's character code inval.If
flagis not a null pointer, that means this option should just set a flag in the program. The flag is a variable of typeintthat you define. Put the address of the flag in theflagfield. Put in thevalfield the value you would like this option to store in the flag. In this case,getopt_longreturns0.For any long option,
getopt_longtells you the index in the array longopts of the options definition, by storing it into*indexptr. You can get the name of the option with longopts[*indexptr].name. So you can distinguish among long options either by the values in theirvalfields or by their indices. You can also distinguish in this way among long options that set flags.When a long option has an argument,
getopt_longputs the argument value in the variableoptargbefore returning. When the option has no argument, the value inoptargis a null pointer. This is how you can tell whether an optional argument was supplied.When
getopt_longhas no more options to handle, it returns-1, and leaves in the variableoptindthe index in argv of the next remaining argument.
Since long option names were used before before the getopt_long
options was invented there are program interfaces which require programs
to recognize options like `-option value' instead of
`--option value'. To enable these programs to use the GNU
getopt functionality there is one more function available.
The
getopt_long_onlyfunction is equivalent to thegetopt_longfunction but it allows to specify the user of the application to pass long options with only `-' instead of `--'. The `--' prefix is still recognized but instead of looking through the short options if a `-' is seen it is first tried whether this parameter names a long option. If not, it is parsed as a short option.Assuming
getopt_long_onlyis used starting an application withapp -foothe
getopt_long_onlywill first look for a long option named `foo'. If this is not found, the short options `f', `o', and again `o' are recognized.