date and time created 90/05/16 12:49:58 by bostic
[unix-history] / usr / src / lib / libc / stdlib / getopt.3
CommitLineData
006fcfe1
KB
1.\" Copyright (c) 1988 Regents of the University of California.
2.\" All rights reserved.
53e9b2ee 3.\"
006fcfe1 4.\" Redistribution and use in source and binary forms are permitted
57a981eb
KB
5.\" provided that the above copyright notice and this paragraph are
6.\" duplicated in all such forms and that any documentation,
7.\" advertising materials, and other materials related to such
8.\" distribution and use acknowledge that the software was developed
9.\" by the University of California, Berkeley. The name of the
10.\" University may not be used to endorse or promote products derived
11.\" from this software without specific prior written permission.
12.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14.\" WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
006fcfe1 15.\"
dd71e7d8 16.\" @(#)getopt.3 6.13 (Berkeley) %G%
53e9b2ee
KM
17.\"
18.TH GETOPT 3 ""
19.UC 6
20.SH NAME
21getopt \- get option letter from argv
22.SH SYNOPSIS
23.ft B
09885840 24.nf
53e9b2ee 25int getopt(argc, argv, optstring)
53e9b2ee 26int argc;
53e9b2ee 27char **argv;
53e9b2ee
KM
28char *optstring;
29.sp
30extern char *optarg;
53e9b2ee 31extern int optind;
0b64d222 32extern int opterr;
53e9b2ee
KM
33.ft
34.SH DESCRIPTION
35.I Getopt
36returns the next option letter in
37.I argv
38that matches a letter in
39.IR optstring .
40.I Optstring
09885840
KB
41is a string of recognized option letters; if a letter is followed by a
42colon, the option is expected to have an argument that may or may not
43be separated from it by white space.
53e9b2ee 44.PP
09885840
KB
45On return from
46.IR getopt ,
47optarg is set to point to the start of any option argument.
48.I Optind
49contains the
53e9b2ee
KM
50.I argv
51index of the next argument to be processed.
09885840
KB
52.PP
53.I Opterr
54and
53e9b2ee 55.I optind
09885840 56are both initialized to 1.
53e9b2ee 57.PP
09885840
KB
58When all options have been processed (i.e., up to the first non-option
59argument),
53e9b2ee 60.I getopt
09885840
KB
61returns EOF.
62The special option ``\-\-'' may be used to delimit the end of the options;
63EOF will be returned, and the ``\-\-'' will be skipped.
53e9b2ee
KM
64.SH DIAGNOSTICS
65.I Getopt
66prints an error message on
67.I stderr
09885840
KB
68and returns a question mark (``?'') when it encounters an option
69letter not included in
70.IR optstring ,
71or it encounters an option that requires an argument which is not
72supplied.
73Setting
74.I opterr
75to a zero will disable these error messages.
53e9b2ee 76.SH EXAMPLE
53e9b2ee 77.nf
09885840
KB
78.in +5
79extern char *optarg;
80extern int optind;
81int bflag, ch, fd;
82
83bflag = 0;
84while ((ch = getopt(argc, argv, "bf:")) != EOF)
85 switch(ch) {
86 case 'b':
87 bflag = 1;
88 break;
89 case 'f':
90 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
91 (void)fprintf(stderr,
92 "myname: unable to read file %s.\en", optarg);
93 exit(1);
53e9b2ee 94 }
09885840
KB
95 break;
96 case '?':
97 default:
98 usage();
53e9b2ee 99 }
09885840
KB
100argc -= optind;
101argv += optind;
102.fi
53e9b2ee 103.SH BUGS
09885840
KB
104Option arguments are allowed to begin with ``\-''; this is reasonable but
105reduces the amount of error checking possible.
53e9b2ee 106.PP
09885840
KB
107A single dash (``-'') may be specified as an character in
108.IR optstring ,
109however it should
110.B never
111have an argument associated with it.
112This allows
113.I getopt
114to be used with programs that expect ``-'' as an option flag.
115This practice is wrong, and should not be used in any current development.
116It is provided for backward compatibility
117.BR only .
dd71e7d8
KB
118By default, a single dash causes
119.I getopt
120to return EOF.
121This is, we believe, compatible with System V.
09885840
KB
122.PP
123It is also possible to handle digits as option letters.
124This allows
125.I getopt
126to be used with programs that expect a number (``-3'') as an option.
127This practice is wrong, and should not be used in any current development.
128It is provided for backward compatibility
129.BR only .
130The following code fragment works fairly well.
131.sp
7edf3dad 132.nf
09885840
KB
133.in +5
134int length;
135char *p;
7edf3dad 136
09885840
KB
137while ((c = getopt(argc, argv, "0123456789")) != EOF)
138 switch (c) {
139 case '0': case '1': case '2': case '3': case '4':
140 case '5': case '6': case '7': case '8': case '9':
141 p = argv[optind - 1];
142 if (p[0] == '-' && p[1] == ch && !p[2])
143 length = atoi(++p);
144 else
145 length = atoi(argv[optind] + 1);
146 break;
7edf3dad 147 }
09885840 148}
7edf3dad 149.fi