.TH getopt() "" "" "General Function (libc)"
.PC "Get option letter from argv"
.B "#include <unistd.h>"
\fBint getopt(\fIargc\^\fB, \fIargv\^\fB, \fIoptstring\^\fB)\fR
\fBint \fIargc\^\fB;\fR
\fBchar **\fIargv\^\fB;\fR
\fBchar *\fIoptstring\^\fB;\fR
\fBextern char *\fIoptarg\^\fB;\fR
\fBextern int \fIoptind\^\fB;\fR
.PP
.B getopt()
returns the next option letter in
.B 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 must have
an argument, which may or may not be separated from it by white space.
.I optarg
points to the start of the option argument on return from
.BR getopt() .
.PP
.B getopt()
writes into
.I optind
the
.B argv
index of the next argument to be processed.
Because
.I optind
is external, it is normally initialized to one automatically
before the first call to
.BR getopt() .
.PP
When all options have been processed (i.e., up to the first
non-option argument),
.B getopt()
returns EOF.
The special option ``\-\-''
may be used to delimit the end of the options:
.B getopt()
returns EOF and skip ``\-\-''.
.SH "See Also"
.Xr "libc" libc
.SH Diagnostics
.B getopt()
prints an error message and returns a question mark
when it encounters an option letter not included in
.IR optstring .
.SH Notes
It is not obvious how `\-'
standing alone should be treated.
This version treats it as
a non-option argument, which is not always right.
.PP
Option arguments are allowed to begin with `\-'.
This is reasonable, but reduces the amount of error checking possible.
.PP
.B getopt()
returns the parsed letter option in the external \fBint\fR
.BR optopt ,
which is overwritten by each call to
.BR getopt() .
When
.B getopt()
returns `?', it can be helpful to examine the contents of this variable.
