p In the second mechanism, a long option sets a flag in the .Fa option structure passed, or will store a pointer to the command line argument in the .Fa 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. d -literal myprogram --myoption=somevalue .Ed
p When a long option is processed the call to .Fn getopt_long will return 0. For this reason, long option processing without shortcuts is not backwards compatible with .Xr getopt 3 .
p 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.
p The .Fn getopt_long call requires a structure to be initialized describing the long options. The structure is: d -literal struct option { char *name; int has_arg; int *flag; int val; }; .Ed
p The .Fa name field should contain the option name without the leading double dash.
p The .Fa has_arg field should be one of: l -tag -width "optional_argument" t Li no_argument no argument to the option is expect. t Li required_argument an argument to the option is required. t Li optional_argument an argument to the option may be presented. .El
p
If
.Fa flag
is non-NULL, then the integer pointed to by it will be set to the
value in the
.Fa val
field.
If the
.Fa flag
field is NULL, then the
.Fa val
field will be returned.
Setting
.Fa flag
to NULL and setting
.Fa val
to the corresponding short option will make this function act just
like
.Xr getopt 3 .
.Sh EXAMPLES
d -literal -compact extern char *optarg;
extern int optind;
int bflag, ch, fd;
int daggerset;
/* options descriptor */
static struct option longopts[] = {
{ "buffy", no_argument, 0, 'b' },
{ "floride", required_argument, 0, 'f' },
{ "daggerset", no_argument, \*[Am]daggerset, 1 },
{ 0, 0, 0, 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)) \*[Lt] 0) {
(void)fprintf(stderr,
"myname: %s: %s\en", optarg, strerror(errno));
exit(1);
}
break;
case 0:
if(daggerset) {
fprintf(stderr,"Buffy will use her dagger to "
"apply floride to dracula's teeth\en");
}
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
.Ed
.Sh IMPLEMENTATION DIFFERENCES
This section describes differences to the GNU implementation
found in glibc-2.1.3:
l -tag -width "xxx" t Li o handling of - as first char of option string in presence of
environment variable POSIXLY_CORRECT:
l -tag -width "NetBSD" t Li GNU ignores POSIXLY_CORRECT and returns non-options as
arguments to option '\e1'.
t Li NetBSD honors POSIXLY_CORRECT and stops at the first non-option.
.El
t Li o handling of :: in options string in presence of POSIXLY_CORRECT:
l -tag -width "NetBSD" t Li Both GNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
mean the preceding option takes an optional argument.
.El
t Li o return value in case of missing argument if first character
(after + or -) in option string is not ':':
l -tag -width "NetBSD" t Li GNU returns '?'
t NetBSD returns ':' (since NetBSD's getopt does).
.El
t Li o handling of --a in getopt:
l -tag -width "NetBSD" t Li GNU parses this as option '-', option 'a'.
t Li NetBSD parses this as '--', and returns -1 (ignoring the a).
(Because the original getopt does.)
.El
t Li o setting of optopt for long options with flag != NULL:
l -tag -width "NetBSD" t Li GNU sets optopt to val.
t Li NetBSD sets optopt to 0 (since val would never be returned).
.El
t Li o handling of -W with W; in option string in getopt (not getopt_long):
l -tag -width "NetBSD" t Li GNU causes a segfault.
t Li NetBSD returns -1, with optind pointing past the argument of -W
(as if `-W arg' were `--arg', and thus '--' had been found).
How should we treat W; in the option string when called via
getopt? Ignore the ';' or treat it as a ':'? Issue a warning?
.El
t Li o setting of optarg for long options without an argument that are
invoked via -W (W; in option string):
l -tag -width "NetBSD" t Li GNU sets optarg to the option name (the argument of -W).
t Li NetBSD sets optarg to NULL (the argument of the long option).
.El
t Li o handling of -W with an argument that is not (a prefix to) a known
long option (W; in option string):
l -tag -width "NetBSD" t Li GNU returns -W with optarg set to the unknown option.
t Li NetBSD treats this as an error (unknown option) and returns '?' with
optopt set to 0 and optarg set to NULL (as GNU's man page documents).
.El
t Li o The error messages are different.
t Li o NetBSD does not permute the argument vector at the same points in
the calling sequence as GNU does.
The aspects normally used by the caller
(ordering after -1 is returned, value of optind relative
to current positions) are the same, though.
(We do fewer variable swaps.)
.El
.Sh SEE ALSO
.Xr getopt 3
.Sh HISTORY
The
.Fn getopt_long
function first appeared in GNU libiberty.
The first
.Nx
implementation appeared in 1.5.
.Sh BUGS
The implementation, can completely replace
.Xr getopt 3 ,
but right now we are using separate code.