xref: /freebsd/lib/libc/stdlib/getopt.3 (revision 8e537d168674d6b65869f73c20813001af875738)
1.\" Copyright (c) 1988, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)getopt.3	8.4 (Berkeley) 4/19/94
33.\"
34.Dd April 19, 1994
35.Dt GETOPT 3
36.Os BSD 4.3
37.Sh NAME
38.Nm getopt
39.Nd get option character from command line argument list
40.Sh SYNOPSIS
41.Fd #include <stdlib.h>
42.Vt extern char *optarg;
43.Vt extern int   optind;
44.Vt extern int   optopt;
45.Vt extern int   opterr;
46.Vt extern int   optreset;
47.Ft int
48.Fn getopt "int argc" "char * const *argv" "const char *optstring"
49.Sh DESCRIPTION
50The
51.Fn getopt
52function incrementally parses a command line argument list
53.Fa argv
54and returns the next
55.Em known
56option character.
57An option character is
58.Em known
59if it has been specified in the string of accepted option characters,
60.Fa optstring .
61.Pp
62The option string
63.Fa optstring
64may contain the following elements: individual characters, and
65characters followed by a colon to indicate an option argument
66is to follow.
67For example, an option string
68.Li "\&""x""
69recognizes an option
70.Dq Fl x ,
71and an option string
72.Li "\&""x:""
73recognizes an option and argument
74.Dq Fl x Ar argument .
75It does not matter to
76.Fn getopt
77if a following argument has leading white space.
78.Pp
79On return from
80.Fn getopt ,
81.Va optarg
82points to an option argument, if it is anticipated,
83and the variable
84.Va optind
85contains the index to the next
86.Fa argv
87argument for a subsequent call
88to
89.Fn getopt .
90The variable
91.Va optopt
92saves the last
93.Em known
94option character returned by
95.Fn getopt .
96.Pp
97The variable
98.Va opterr
99and
100.Va optind
101are both initialized to 1.
102The
103.Va optind
104variable may be set to another value before a set of calls to
105.Fn getopt
106in order to skip over more or less argv entries.
107.Pp
108In order to use
109.Fn getopt
110to evaluate multiple sets of arguments, or to evaluate a single set of
111arguments multiple times,
112the variable
113.Va optreset
114must be set to 1 before the second and each additional set of calls to
115.Fn getopt ,
116and the variable
117.Va optind
118must be reinitialized.
119.Pp
120The
121.Fn getopt
122function
123returns an
124.Dv EOF
125when the argument list is exhausted, or
126.Ql ?
127if a non-recognized
128option is encountered.
129The interpretation of options in the argument list may be canceled
130by the option
131.Ql --
132(double dash) which causes
133.Fn getopt
134to signal the end of argument processing and return an
135.Dv EOF .
136When all options have been processed (i.e., up to the first non-option
137argument),
138.Fn getopt
139returns
140.Dv EOF .
141.Sh DIAGNOSTICS
142If the
143.Fn getopt
144function encounters a character not found in the string
145.Va optarg
146or detects
147a missing option argument it writes an error message to the
148.Em stderr
149and returns
150.Ql ? .
151Setting
152.Va opterr
153to a zero will disable these error messages.
154If
155.Va optstring
156has a leading
157.Ql \&:
158then a missing option argument causes a
159.Ql \&:
160to be returned in addition to suppressing any error messages.
161.Pp
162Option arguments are allowed to begin with
163.Dq Li \- ;
164this is reasonable but
165reduces the amount of error checking possible.
166.Sh EXTENSIONS
167The
168.Va optreset
169variable was added to make it possible to call the
170.Fn getopt
171function multiple times.
172This is an extension to the
173.St -p1003.2
174specification.
175.Sh EXAMPLE
176.Bd -literal -compact
177extern char *optarg;
178extern int optind;
179int bflag, ch, fd;
180
181bflag = 0;
182while ((ch = getopt(argc, argv, "bf:")) != EOF)
183	switch(ch) {
184	case 'b':
185		bflag = 1;
186		break;
187	case 'f':
188		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
189			(void)fprintf(stderr,
190			    "myname: %s: %s\en", optarg, strerror(errno));
191			exit(1);
192		}
193		break;
194	case '?':
195	default:
196		usage();
197}
198argc -= optind;
199argv += optind;
200.Ed
201.Sh HISTORY
202The
203.Fn getopt
204function appeared
205.Bx 4.3 .
206.Sh BUGS
207A single dash
208.Dq Li -
209may be specified as an character in
210.Fa optstring ,
211however it should
212.Em never
213have an argument associated with it.
214This allows
215.Fn getopt
216to be used with programs that expect
217.Dq Li -
218as an option flag.
219This practice is wrong, and should not be used in any current development.
220It is provided for backward compatibility
221.Em only .
222By default, a single dash causes
223.Fn getopt
224to return
225.Dv EOF .
226This is, we believe, compatible with System V.
227.Pp
228It is also possible to handle digits as option letters.
229This allows
230.Fn getopt
231to be used with programs that expect a number
232.Pq Dq Li \&-\&3
233as an option.
234This practice is wrong, and should not be used in any current development.
235It is provided for backward compatibility
236.Em only .
237The following code fragment works in most cases.
238.Bd -literal -offset indent
239int length;
240char *p;
241
242while ((c = getopt(argc, argv, "0123456789")) != EOF)
243	switch (c) {
244	case '0': case '1': case '2': case '3': case '4':
245	case '5': case '6': case '7': case '8': case '9':
246		p = argv[optind - 1];
247		if (p[0] == '-' && p[1] == ch && !p[2])
248			length = atoi(++p);
249		else
250			length = atoi(argv[optind] + 1);
251		break;
252	}
253}
254.Ed
255