.\" Copyright (c) 1985 Regents of the University of California.
.\" All rights reserved. The Berkeley software License Agreement
.\" specifies the terms and conditions for redistribution.
.\" @(#)getopt.3 6.1 (Berkeley) %G%
getopt \- get option letter from argv
int getopt(argc, argv, optstring)
returns the next option letter in
is a string of recognized option letters;
if a letter is followed by a colon, the option is expected to have
an argument that may or may not be separated from it by white space.
is set to point to the start of the option argument on return from
index of the next argument to be processed.
is external, it is normally initialized to zero automatically
When all options have been processed (i.e., up to the first
may be used to delimit the end of the options;
prints an error message on
and returns a question mark
when it encounters an option letter not included in
The following code fragment shows how one might process the arguments
for a command that can take the mutually exclusive options
both of which require arguments:
while ((c = getopt(argc, argv, "abf:o:")) != EOF)
fprintf(stderr, "Usage: ...");
for (; optind < argc; optind++) {
A template similar to this can be found in
.IR /usr/pub/template.c .
Written by Henry Spencer, working from a Bell Labs manual page.
Behavior believed identical to the Bell version.
Modified by Keith Bostic to behave closer to the System V version.
standing alone should be treated; this version treats it as
a non-option argument, which is not always right.
Option arguments are allowed to begin with `\-';
this is reasonable but reduces the amount of error checking possible.
is quite flexible but the obvious price must be paid: there is much
it could do that it doesn't, like
checking mutually exclusive options, checking type of