xref: /original-bsd/lib/libc/stdlib/getopt.3 (revision 0edb85a7)
Copyright (c) 1988 Regents of the University of California.
All rights reserved.

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.14 (Berkeley) 05/21/90

GETOPT 3 ""
C 6
NAME
getopt - get option letter from argv
SYNOPSIS
int getopt(argc, argv, optstring)
int argc;
char **argv;
char *optstring;
extern char *optarg;
extern int optind;
extern int opterr;
DESCRIPTION
Getopt returns the next option letter in argv that matches a letter in optstring . Optstring 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.

On return from getopt , optarg is set to point to the start of any option argument. Optind contains the argv index of the next argument to be processed.

Opterr and optind are both initialized to 1. In order to use getopt to evaluate multiple sets of arguments, or to evaluate a single set of arguments multiple times, optind must be initialized to the number of argv entries to be skipped in each evaluation.

When all options have been processed (i.e., up to the first non-option argument), getopt returns EOF. The special option ``--'' may be used to delimit the end of the options; EOF will be returned, and the ``--'' will be skipped.

DIAGNOSTICS
Getopt prints an error message on stderr and returns a question mark (``?'') when it encounters an option letter not included in optstring , or it encounters an option that requires an argument which is not supplied. Setting opterr to a zero will disable these error messages.
EXAMPLE
extern char *optarg;
extern int optind;
int bflag, ch, fd;

bflag = 0;
while ((ch = getopt(argc, argv, "bf:")) != EOF)
 switch(ch) {
 case 'b':
 bflag = 1;
 break;
 case 'f':
 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
 (void)fprintf(stderr,
 "myname: unable to read file %s.\en", optarg);
 exit(1);
 }
 break;
 case '?':
 default:
 usage();
 }
argc -= optind;
argv += optind;
BUGS
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 optstring , however it should never have an argument associated with it. This allows getopt 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 only . By default, a single dash causes getopt to return EOF. This is, we believe, compatible with System V.

It is also possible to handle digits as option letters. This allows getopt 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 only . The following code fragment works fairly well.

int length;
char *p;

while ((c = getopt(argc, argv, "0123456789")) != EOF)
 switch (c) {
 case '0': case '1': case '2': case '3': case '4':
 case '5': case '6': case '7': case '8': case '9':
 p = argv[optind - 1];
 if (p[0] == '-' && p[1] == ch && !p[2])
 length = atoi(++p);
 else
 length = atoi(argv[optind] + 1);
 break;
 }
}