GETOPT(3C) INTERACTIVE UNIX System GETOPT(3C)
NAME
getopt - get option letter from argument vector
SYNOPSIS
int getopt (argc, argv, optstring)
int argc;
char **argv, *opstring;
extern char *optarg;
extern int optind, opterr;
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(1)]. So all
new commands will adhere to the command syntax standard,
they should use getopts(1) or getopt(3C) to parse positional
parameters and check for options that are legal for that
command.
optstring must contain the option letters the command using
getopt will recognize; 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.
optarg is set to point to the start of the option-argument
on return from getopt.
getopt places in optind the argv index of the next argument
to be processed. optind is external and is initialized to 1
before the first call to getopt.
When all options have been processed (i.e., up to the first
non-option argument), getopt returns -1. The special option
``--'' may be used to delimit the end of the options; when
it is encountered, -1 will be returned, and ``--'' will be
skipped.
The following rules comprise the System V standard for
command-line syntax:
RULE 1 Command names must be between two and nine
characters.
RULE 2 Command names must include lowercase letters
and digits only.
RULE 3 Option names must be a single character in
length.
RULE 4 All options must be delimited by the - char-
acter.
Rev. C Software Development Set Page 1
GETOPT(3C) INTERACTIVE UNIX System GETOPT(3C)
RULE 5 Options with no arguments may be grouped
behind one delimiter.
RULE 6 The first option-argument following an option
must be preceded by white space.
RULE 7 Option arguments cannot be optional.
RULE 8 Groups of option arguments following an
option must be separated by commas or
separated by white space and quoted.
RULE 9 All options must precede operands on the com-
mand line.
RULE 10 The characters -- may be used to delimit the
end of the options.
RULE 11 The order of options relative to one another
should not matter.
RULE 12 The order of operands may matter and
position-related interpretations should be
determined on a command-specific basis.
RULE 13 The - character preceded and followed by
white space should be used only to mean stan-
dard input.
The function getopt is the command-line parser that will
enforce the rules of this command syntax standard.
EXAMPLE
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 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++;
Rev. C Software Development Set Page 2
GETOPT(3C) INTERACTIVE UNIX System GETOPT(3C)
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)) {
.
.
.
}
SEE ALSO
getopts(1), intro(1) in the INTERACTIVE UNIX System
User's/System Administrator's Reference Manual.
DIAGNOSTICS
getopt prints an error message on standard error 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 may be disabled by
setting opterr to 0.
WARNING
Although the following command syntax rule [see intro(1)]
relaxations are permitted under the current implementation,
they should not be used because they may not be supported in
future releases of the system. As in the EXAMPLE section
above, a and b are options, and the option o requires an
option-argument:
cmd -aboxxx file (Rule 5 violation:
options with option-
arguments must not be
grouped with other
options)
cmd -ab -oxxx file (Rule 6 violation:
Rev. C Software Development Set Page 3
GETOPT(3C) INTERACTIVE UNIX System GETOPT(3C)
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 may lead to unexpected
results.
Rev. C Software Development Set Page 4