Main index | Section 3 | Deutsch | Options |
#include <getopt.h>extern char *optarg;
In the second mechanism, a long option sets a flag in the option structure passed, or will store a pointer to the command line argument in the option structure passed to it for options that take arguments. Additionally, the long option's argument may be specified as a single argument with an equal sign, e.g.,
myprogram --myoption=somevalue
When a long option is processed, the call to getopt_long() will return 0. For this reason, long option processing without shortcuts is not backwards compatible with getopt(3).
It is possible to combine these methods, providing for long options processing with short option equivalents for some options. Less frequently used options would be processed as long options only.
The getopt_long() call requires a structure to be initialized describing the long options. The structure is:
struct option { char *name; int has_arg; int *flag; int val; };
The name field should contain the option name without the leading double dash.
The has_arg field should be one of:
no_argument | no argument to the option is expected |
required_argument | |
an argument to the option is required | |
optional_argument | |
an argument to the option may be presented | |
If flag is not NULL, then the integer pointed to by it will be set to the value in the val field. If the flag field is NULL, then the val field will be returned. Setting flag to NULL and setting val to the corresponding short option will make this function act just like getopt(3).
If the longindex field is not NULL, then the integer pointed to by it will be set to the index of the long option relative to longopts.
The last element of the longopts array has to be filled with zeroes.
The getopt_long_only() function behaves identically to getopt_long() with the exception that long options may start with ‘-’ in addition to ‘--’. If an option starting with ‘-’ does not match a long option but does match a single-character option, the single-character option is returned.
These functions return ‘amp;:’ if there was a missing option argument and error messages are suppressed, ‘amp;?’ if the user specified an unknown or ambiguous option, and -1 when the argument list has been exhausted. The default behavior when a missing option argument is encountered is to write an error and return ‘amp;?’. Specifying ‘amp;:’ in optstr will cause the error message to be suppressed and ‘amp;:’ to be returned instead.
In addition to ‘amp;:’, a leading ‘amp;+’ or ‘amp;-’ in optstr also has special meaning. If either of these are specified, they must appear before ‘amp;:’.
A leading ‘amp;+’ indicates that processing should be halted at the first non-option argument, matching the default behavior of getopt(3). The default behavior without ‘amp;+’ is to permute non-option arguments to the end of argv.
A leading ‘amp;-’ indicates that all non-option arguments should be treated as if they are arguments to a literal ‘amp;1’ flag (i.e., the function call will return the value 1, rather than the char literal '1').
POSIXLY_CORRECT | |
If set, option processing stops when the first non-option is found and a leading ‘-’ or ‘+’ in the optstring is ignored. | |
int bflag, ch, fd; int daggerset;/* options descriptor */ static struct option longopts[] = { { "buffy", no_argument, NULL, 'b' }, { "fluoride", required_argument, NULL, 'f' }, { "daggerset", no_argument, daggerset, 1 }, { NULL, 0, NULL, 0 } };
bflag = 0; while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) { switch (ch) { case 'b': bflag = 1; break; case 'f': if ((fd = open(optarg, O_RDONLY, 0)) == -1) err(1, "unable to open %s", optarg); break; case 0: if (daggerset) { fprintf(stderr,"Buffy will use her dagger to " "apply fluoride to dracula's teeth\n"); } break; default: usage(); } } argc -= optind; argv += optind;
GNU | sets optopt to val. |
Bx | sets optopt to 0 (since val would never be returned). |
GNU | sets optarg to the option name (the argument of ‘-W’). |
Bx | sets optarg to NULL (the argument of the long option). |
GNU | returns ‘-W’ with optarg set to the unknown option. |
Bx | treats this as an error (unknown option) and returns ‘amp;?’ with optopt set to 0 and optarg set to NULL (as GNU's man page documents). |
The implementation can completely replace getopt(3), but right now we are using separate code.
GETOPT_LONG (3) | May 2, 2018 |
Main index | Section 3 | Deutsch | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | The “N” in NFS stands for Not, or Need, or perhaps Nightmare | ” |
— Harry Spencer |