.\" Copyright (c) 1988 Regents of the University of California.
.\" %sccs.include.redist.man%
.\" @(#)getopt.3 6.15 (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.
optarg is set to point to the start of any option argument.
index of the next argument to be processed.
are both initialized to 1.
to evaluate multiple sets of arguments, or to evaluate a single set of
arguments multiple times,
must be initialized to the number of argv entries to be skipped in each
When all options have been processed (i.e., up to the first non-option
The special option ``\-\-'' may be used to delimit the end of the options;
EOF will be returned, and the ``\-\-'' will be skipped.
prints an error message on
and returns a question mark (``?'') when it encounters an option
or it encounters an option that requires an argument which is not
to a zero will disable these error messages.
while ((ch = getopt(argc, argv, "bf:")) != EOF)
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
"myname: unable to read file %s.\en", optarg);
Option arguments are allowed to begin with ``\-''; this is reasonable but
reduces the amount of error checking possible.
A single dash (``-'') may be specified as an character in
have an argument associated with it.
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
By default, a single dash causes
This is, we believe, compatible with System V.
It is also possible to handle digits as option letters.
to be used with programs that expect a number (``-3'') as an option.
This practice is wrong, and should not be used in any current development.
It is provided for backward compatibility
The following code fragment works fairly well.
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])
length = atoi(argv[optind] + 1);