.\" Copyright (c) 1988 Regents of the University of California.
.\" Redistribution and use in source and binary forms are permitted
.\" provided that the above copyright notice and this paragraph are
.\" duplicated in all such forms and that any documentation,
.\" advertising materials, and other materials related to such
.\" distribution and use acknowledge that the software was developed
.\" by the University of California, 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'' AND WITHOUT ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
.\" WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
.\" @(#)getopt.3 6.11 (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.
``-'' may be specified as an option letter, however it should never
have an argument associated with it. This allows \fIgetopt\fP to be
used with programs that expect ``-'' as an option flag. This practice
is wrong, and should not be used in any current development, it is
provided for backward compatibility \fBonly\fP.
It is possible to handle digits as option letters. This allows
\fIgetopt\fP to be used with programs that expect ``-#'' as an
option flag. This practice is wrong, and should not be used in any
current development, it is provided for backward compatibility
\fBonly\fP. The following code fragment, while not perfect, works
while ((c = getopt(argc, argv, "0123456789")) != EOF)
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if (p[0] == '-' && p[1] == ch && !p[2])
minlen = atoi(argv[optind] + 1);
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