V8/usr/man/man3/getopt.3
.TH GETOPT 3
.SH NAME
getopt \- get option letter from argv
.SH SYNOPSIS
.B int getopt (argc, argv, optstring)
.br
.B int argc;
.br
.B char \(**\(**argv;
.br
.B char \(**optstring;
.br
.B extern char \(**optarg;
.br
.B extern int optind;
.br
.SH DESCRIPTION
.I Getopt
returns the next option letter in
.I argv
that matches
a letter in
.IR optstring .
.I Optstring
is a string of recognized option letters;
if a letter is followed by a colon, the option
is expected to have an argument which may or
may not be separated from it by white space.
.I Optarg
is set to point to the start of the option argument
on return from
.IR getopt .
.PP
.I Getopt
places in
.I optind
the
.I argv
index of the next argument to be processed.
Since
.I optind
is external, it is normally initialized to zero
automatically before the first call to
.IR getopt .
.PP
Option letters appear in nonempty clusters preceded by `\-'.
When all options have been processed
(i.e., up to the first non-option argument),
.I getopt
returns
.SM
.BR EOF .
The special option `\-\-' may be used to delimit the end of the options;
.SM
.B EOF
will be returned, and `\-\-' will be skipped.
.SH DIAGNOSTICS
.I Getopt
prints an error message on
.I stderr
and returns a
question mark
.RB ( \'?\' )
when it encounters an option letter not included in
.IR optstring .
.SH EXAMPLE
.PP
This fragment processes arguments
for a command that can take option
.B a
and option
.BR f ,
which requires an argument.
.RS
.PP
.nf
.ta .5i +.5i +\w'case \'a\': 'u
main (argc, argv) char \(**\(**argv;
{
int c;
extern int optind;
extern char \(**\(**optarg, \(**\(**ifile;
while ((c = getopt (argc, argv, "af:")) != \-1)
switch (c) {
case \'a\': aflg++;
break;
case \'f\': ifile = optarg;
break;
case \'?\': errflg++;
}
if (errflg) {
fprintf (stderr, "usage: . . . ");
exit (2);
}
for( ; optind < argc; optind++) {
if (access (argv[optind], 4)) {
.B " ..."
.fi
.RE