.\" Copyright (c) 1988 Regents of the University of California.
.\" Redistribution and use in source and binary forms are permitted
.\" provided that this notice is preserved and that due credit is given
.\" to the University of California at Berkeley. The name of the University
.\" may not be used to endorse or promote products derived from this
.\" software without specific prior written permission. This software
.\" is provided ``as is'' without express or implied warranty.
.\" @(#)getopt.3 6.7 (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
Setting \fIopterr\fP to a zero will disable this error message.
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++) {
Written by Henry Spencer, working from a Bell Labs manual page.
Modified by Keith Bostic to behave more like the System V version.
``-'' may be specified as an option letter, however it should never have
an argument associated with it. This allows getopt to be used with
programs that think that ``-'' means standard input.
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