getopt(3) CLIX getopt(3)
NAME
getopt - Gets an option letter from argument vector
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
int getopt(
int argc ,
char **argv ,
char *optstring );
extern char *optarg ;
extern int optind ;
extern int opterr ;
PARAMETERS
argc The number of command line arguments.
argv A pointer to a pointer to a character string.
optstring A pointer to a character string representing command options.
DESCRIPTION
The getopt() function returns the next option letter in argv that matches
a letter in optstring. It supports all the rules of the command syntax
standard (see intro). So that all new commands adhere to the command
syntax standard, they should use getopts or getopt() to parse positional
parameters and check for options that are legal for that command.
The optstring parameter must contain the option letters the command using
getopt() recognizes. If a letter is followed by a colon, the option is
expected to have an argument or group of arguments which must be separated
from it by white space.
The optarg variable is set to point to the start of the option-argument on
return from getopt().
The getopt() function places in the optind variable the argv index of the
next argument to be processed. The optind variable is external and is
initialized to 1 before the first call to getopt().
EXAMPLES
The following code fragment shows how one might process the arguments for
a command that can take the mutually exclusive options a and b, and the
2/94 - Intergraph Corporation 1
getopt(3) CLIX getopt(3)
option o, which requires an option-argument:
main (argc, argv)
int argc;
char **argv;
{
int c;
extern char *optarg;
extern int optind;
.
.
.
while ((c = getopt(argc, argv, "abo:")) != -1)
switch (c) {
case 'a':
if (bflg)
errflg++;
else
aflg++;
break;
case 'b':
if (aflg)
errflg++;
else
bproc();
break;
case 'o':
ofile = optarg;
break;
case '?':
errflg++;
}
if (errflg) {
(void)fprintf(stderr, "usage: ... ");
exit (2);
}
for ( ; optind < argc; optind++) {
if (access(argv[optind], 4)) {
.
.
.
}
CAUTIONS
Although the following command, syntax rule (see intro) relaxations are
permitted under the current implementation, they should not be used since
they may not be supported in future releases of the system. As in the
EXAMPLES section, a and b are options, and the option o requires an
option-argument:
2 Intergraph Corporation - 2/94
getopt(3) CLIX getopt(3)
cmd -aboxxx file
Rule 5 violation: options with option-arguments must not be
grouped with other options.
cmd -ab -oxxx file
Rule 6 violation: there must be white space after an option that
takes an option-argument.
Changing the value of the variable optind or calling getopt() with
different values of argv can lead to unexpected results.
RETURN VALUES
When all options are processed (up to the first nonoption argument),
getopt() returns -1. The special -- option may be used to delimit the end
of the options. When -- is encountered, -1 is returned, and -- is
skipped.
ERRORS
The getopt() function displays an error message on stderr and returns a
question mark (?) when it encounters an option letter not included in
optstring or no option-argument after an option that expects one. This
error message is disabled by setting opterr to 0.
RELATED INFORMATION
Commands: getopt(1), intro(1)
2/94 - Intergraph Corporation 3