|
NAMEgetopt_long , getopt_long_only
—
get long options from command line argument list
LIBRARYStandard C Library (libc, -lc)SYNOPSIS#include <getopt.h>
extern char *optarg;
int
int
DESCRIPTIONThegetopt_long () function is similar to
getopt(3)
but it accepts options in two forms: words and characters. The
getopt_long () function provides a superset of the
functionality of
getopt(3).
The getopt_long () function can be used in two ways. In
the first way, every long option understood by the program has a corresponding
short option, and the option structure is only used to translate from long
options to short options. When used in this fashion,
getopt_long () behaves identically to
getopt(3).
This is a good way to add long option processing to an existing program with
the minimum of rewriting.
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
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 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:
If flag is not If the longindex field is not
The last element of the longopts array has to be filled with zeroes. The RETURN VALUESIf the flag field in struct option isNULL , getopt_long () and
getopt_long_only () return the value specified in the
val field, which is usually just the corresponding short
option. If flag is not NULL ,
these functions return 0 and store val in the location
pointed to by flag.
These functions return ‘ In addition to ‘ A leading ‘ A leading ‘ ENVIRONMENT
EXAMPLESint 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; IMPLEMENTATION DIFFERENCESThis section describes differences to the GNU implementation found in glibc-2.1.3:
SEE ALSOgetopt(3)HISTORYThegetopt_long () and
getopt_long_only () functions first appeared in the GNU
libiberty library. The first BSD implementation of
getopt_long () appeared in NetBSD
1.5, the first BSD implementation of
getopt_long_only () in OpenBSD
3.3. FreeBSD first included
getopt_long () in FreeBSD 5.0,
getopt_long_only () in FreeBSD
5.2.
BUGSThe argv argument is not really const as its elements may be permuted (unlessPOSIXLY_CORRECT is set).
The implementation can completely replace getopt(3), but right now we are using separate code.
Visit the GSP FreeBSD Man Page Interface. |