SYNOPSIS
#include <getopt.h>
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);
DESCRIPTION
The
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. Addition 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:
- 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
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.
RETURN VALUE
If the flag field in struct option is NULL,
ENVIRONMENT
- POSIXLY_CORRECT
- If set, option processing stops when the first non-option is found and a leading `-' or `+' in the optstring is ignored.
EXAMPLES
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;
CONFORMANCE
BSD UNIX
MULTITHREAD SAFETY LEVEL
MT-Safe.
PORTING ISSUES
This section describes differences to the GNU implementation found in glibc-2.1.3:
- +o
-
Setting of optopt for long options with flag != NULL:
- +o
-
Setting of optarg for long options without an argument that are invoked via '-W' ('W;' in option string):
- +o
-
Handling of '-W' with an argument that is not (a prefix to) a known long option ('W;' in option string):
- +o
-
NuTCRACKER Platform 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.)
AVAILABILITY
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
SEE ALSO
- Functions:
getopt()
PTC MKS Toolkit 10.5 Documentation Build 40.