xref: /freebsd/lib/libc/stdlib/getopt.3 (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1.\"	$NetBSD: getopt.3,v 1.34 2014/06/05 22:09:50 wiz Exp $
2.\"
3.\" Copyright (c) 1988, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
31.\"
32.Dd June 5, 2014
33.Dt GETOPT 3
34.Os
35.Sh NAME
36.Nm getopt
37.Nd get option character from command line argument list
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.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.
67If an individual character is followed by two colons, then the
68option argument is optional;
69.Va optarg
70is set to the rest of the current
71.Va argv
72word, or
73.Dv NULL
74if there were no more characters in the current word.
75This is a
76.Tn GNU
77extension.
78For example, an option string
79.Li \&"x"
80recognizes an option
81.Dq Fl x ,
82and an option string
83.Li \&"x:"
84recognizes an option and argument
85.Dq Fl x Ar argument .
86It does not matter to
87.Fn getopt
88if a following argument has leading white space.
89.Pp
90On return from
91.Fn getopt ,
92.Va optarg
93points to an option argument, if it is anticipated,
94and the variable
95.Va optind
96contains the index to the next
97.Fa argv
98argument for a subsequent call
99to
100.Fn getopt .
101The variable
102.Va optopt
103saves the last
104.Em known
105option character returned by
106.Fn getopt .
107.Pp
108The variables
109.Va opterr
110and
111.Va optind
112are both initialized to 1.
113The
114.Va optind
115variable may be set to another value before a set of calls to
116.Fn getopt
117in order to skip over more or less argv entries.
118.Pp
119In order to use
120.Fn getopt
121to evaluate multiple sets of arguments, or to evaluate a single set of
122arguments multiple times,
123the variable
124.Va optreset
125must be set to 1 before the second and each additional set of calls to
126.Fn getopt ,
127and the variable
128.Va optind
129must be reinitialized.
130.Pp
131The
132.Fn getopt
133function returns \-1 when the argument list is exhausted.
134The interpretation of options in the argument list may be cancelled
135by the option
136.Ql --
137(double dash) which causes
138.Fn getopt
139to signal the end of argument processing and return \-1.
140When all options have been processed (i.e., up to the first non-option
141argument),
142.Fn getopt
143returns \-1.
144.Sh RETURN VALUES
145The
146.Fn getopt
147function returns the next known option character in
148.Fa optstring .
149If
150.Fn getopt
151encounters a character not found in
152.Fa optstring
153or if it detects a missing option argument,
154it returns
155.Ql \&?
156(question mark).
157If
158.Fa optstring
159has a leading
160.Ql \&:
161then a missing option argument causes
162.Ql \&:
163to be returned instead of
164.Ql \&? .
165In either case, the variable
166.Va optopt
167is set to the character that caused the error.
168The
169.Fn getopt
170function returns \-1 when the argument list is exhausted.
171.Sh EXAMPLES
172.Bd -literal -compact
173#include <unistd.h>
174int bflag, ch, fd;
175
176bflag = 0;
177while ((ch = getopt(argc, argv, "bf:")) != -1) {
178	switch (ch) {
179	case 'b':
180		bflag = 1;
181		break;
182	case 'f':
183		if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
184			(void)fprintf(stderr,
185			    "myname: %s: %s\en", optarg, strerror(errno));
186			exit(1);
187		}
188		break;
189	case '?':
190	default:
191		usage();
192	}
193}
194argc -= optind;
195argv += optind;
196.Ed
197.Sh DIAGNOSTICS
198If the
199.Fn getopt
200function encounters a character not found in the string
201.Fa optstring
202or detects
203a missing option argument it writes an error message to the
204.Dv stderr
205and returns
206.Ql \&? .
207Setting
208.Va opterr
209to a zero will disable these error messages.
210If
211.Fa optstring
212has a leading
213.Ql \&:
214then a missing option argument causes a
215.Ql \&:
216to be returned in addition to suppressing any error messages.
217.Pp
218Option arguments are allowed to begin with
219.Dq Li \- ;
220this is reasonable but reduces the amount of error checking possible.
221.Sh SEE ALSO
222.Xr getopt 1 ,
223.Xr getopt_long 3 ,
224.Xr getsubopt 3
225.Sh STANDARDS
226The
227.Va optreset
228variable was added to make it possible to call the
229.Fn getopt
230function multiple times.
231This is an extension to the
232.St -p1003.2
233specification.
234.Sh HISTORY
235The
236.Fn getopt
237function appeared in
238.Bx 4.3 .
239.Sh BUGS
240The
241.Fn getopt
242function was once specified to return
243.Dv EOF
244instead of \-1.
245This was changed by
246.St -p1003.2-92
247to decouple
248.Fn getopt
249from
250.In stdio.h .
251.Pp
252A single dash
253.Dq Li -
254may be specified as a character in
255.Fa optstring ,
256however it should
257.Em never
258have an argument associated with it.
259This allows
260.Fn getopt
261to be used with programs that expect
262.Dq Li -
263as an option flag.
264This practice is wrong, and should not be used in any current development.
265It is provided for backward compatibility
266.Em only .
267Care should be taken not to use
268.Ql \&-
269as the first character in
270.Fa optstring
271to avoid a semantic conflict with
272.Tn GNU
273.Fn getopt ,
274which assigns different meaning to an
275.Fa optstring
276that begins with a
277.Ql \&- .
278By default, a single dash causes
279.Fn getopt
280to return \-1.
281.Pp
282It is also possible to handle digits as option letters.
283This allows
284.Fn getopt
285to be used with programs that expect a number
286.Pq Dq Li \&-\&3
287as an option.
288This practice is wrong, and should not be used in any current development.
289It is provided for backward compatibility
290.Em only .
291The following code fragment works in most cases.
292.Bd -literal -offset indent
293int ch;
294long length;
295char *p, *ep;
296
297while ((ch = getopt(argc, argv, "0123456789")) != -1)
298	switch (ch) {
299	case '0': case '1': case '2': case '3': case '4':
300	case '5': case '6': case '7': case '8': case '9':
301		p = argv[optind - 1];
302		if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) {
303			length = ch - '0';
304			ep = "";
305		} else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) {
306			length = strtol((p = argv[optind] + 1),
307			    \*[Am]ep, 10);
308			optind++;
309			optreset = 1;
310		} else
311			usage();
312		if (*ep != '\e0')
313			errx(EX_USAGE, "illegal number -- %s", p);
314		break;
315	}
316.Ed
317