1 .\" $NetBSD: getopt.3,v 1.31 2003/09/23 10:26:54 wiz Exp $
3 .\" Copyright (c) 1988, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
35 .\" $FreeBSD: src/lib/libc/stdlib/getopt.3,v 1.25 2004/07/31 01:00:50 imp Exp $
42 .Nd get option character from command line argument list
47 .Vt extern char *optarg ;
48 .Vt extern int optind ;
49 .Vt extern int optopt ;
50 .Vt extern int opterr ;
51 .Vt extern int optreset ;
53 .Fn getopt "int argc" "char * const argv[]" "const char *optstring"
57 function incrementally parses a command line argument list
62 An option character is
64 if it has been specified in the string of accepted option characters,
69 may contain the following elements: individual characters, and
70 characters followed by a colon to indicate an option argument
72 For example, an option string
78 recognizes an option and argument
79 .Dq Fl x Ar argument .
82 if a following argument has leading white space.
87 points to an option argument, if it is anticipated,
90 contains the index to the next
92 argument for a subsequent call
99 option character returned by
106 are both initialized to 1.
109 variable may be set to another value before a set of calls to
111 in order to skip over more or less argv entries.
115 to evaluate multiple sets of arguments, or to evaluate a single set of
116 arguments multiple times,
119 must be set to 1 before the second and each additional set of calls to
123 must be reinitialized.
127 function returns \-1 when the argument list is exhausted.
128 The interpretation of options in the argument list may be cancelled
131 (double dash) which causes
133 to signal the end of argument processing and return \-1.
134 When all options have been processed (i.e., up to the first non-option
141 function returns the next known option character in
145 encounters a character not found in
147 or if it detects a missing option argument,
155 then a missing option argument causes
157 to be returned instead of
159 In either case, the variable
161 is set to the character that caused the error.
164 function returns \-1 when the argument list is exhausted.
166 .Bd -literal -compact
171 while ((ch = getopt(argc, argv, "bf:")) != -1) {
177 if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
178 (void)fprintf(stderr,
179 "myname: %s: %s\en", optarg, strerror(errno));
194 function encounters a character not found in the string
197 a missing option argument it writes an error message to the
203 to a zero will disable these error messages.
208 then a missing option argument causes a
210 to be returned in addition to suppressing any error messages.
212 Option arguments are allowed to begin with
214 this is reasonable but reduces the amount of error checking possible.
222 variable was added to make it possible to call the
224 function multiple times.
225 This is an extension to the
236 function was once specified to return
248 may be specified as a character in
252 have an argument associated with it.
255 to be used with programs that expect
258 This practice is wrong, and should not be used in any current development.
259 It is provided for backward compatibility
261 Care should be taken not to use
263 as the first character in
265 to avoid a semantic conflict with
268 which assigns different meaning to an
272 By default, a single dash causes
276 It is also possible to handle digits as option letters.
279 to be used with programs that expect a number
282 This practice is wrong, and should not be used in any current development.
283 It is provided for backward compatibility
285 The following code fragment works in most cases.
286 .Bd -literal -offset indent
291 while ((ch = getopt(argc, argv, "0123456789")) != -1)
293 case '0': case '1': case '2': case '3': case '4':
294 case '5': case '6': case '7': case '8': case '9':
295 p = argv[optind - 1];
296 if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) {
299 } else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) {
300 length = strtol((p = argv[optind] + 1),
307 errx(EX_USAGE, "illegal number -- %s", p);