1 .\" Copyright (c) 1988, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
32 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
33 .\" $FreeBSD: src/lib/libc/stdlib/getopt.3,v 1.20 2001/09/07 14:46:35 asmodai Exp $
40 .Nd get option character from command line argument list
45 .Vt extern char *optarg ;
46 .Vt extern int optind ;
47 .Vt extern int optopt ;
48 .Vt extern int opterr ;
49 .Vt extern int optreset ;
51 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
55 function incrementally parses a command line argument list
60 An option character is
62 if it has been specified in the string of accepted option characters,
67 may contain the following elements: individual characters, and
68 characters followed by a colon to indicate an option argument
70 For example, an option string
76 recognizes an option and argument
77 .Dq Fl x Ar argument .
80 if a following argument has leading white space.
85 points to an option argument, if it is anticipated,
88 contains the index to the next
90 argument for a subsequent call
97 option character returned by
104 are both initialized to 1.
107 variable may be set to another value before a set of calls to
109 in order to skip over more or less argv entries.
113 to evaluate multiple sets of arguments, or to evaluate a single set of
114 arguments multiple times,
117 must be set to 1 before the second and each additional set of calls to
121 must be reinitialized.
127 when the argument list is exhausted, or
130 option is encountered.
131 The interpretation of options in the argument list may be cancelled
134 (double dash) which causes
136 to signal the end of argument processing and return \-1.
137 When all options have been processed (i.e., up to the first non-option
144 function encounters a character not found in the string
147 a missing option argument it writes an error message to the
153 to a zero will disable these error messages.
158 then a missing option argument causes a
160 to be returned in addition to suppressing any error messages.
162 Option arguments are allowed to begin with
164 this is reasonable but
165 reduces the amount of error checking possible.
169 variable was added to make it possible to call the
171 function multiple times.
172 This is an extension to the
176 .Bd -literal -compact
180 while ((ch = getopt(argc, argv, "bf:")) != -1)
186 if ((fd = open(optarg, O_RDONLY, 0)) < 0)
187 err(1, "%s", optarg);
204 function was once specified to return
216 may be specified as a character in
220 have an argument associated with it.
223 to be used with programs that expect
226 This practice is wrong, and should not be used in any current development.
227 It is provided for backward compatibility
229 By default, a single dash causes
232 This is, we believe, compatible with System V.
234 It is also possible to handle digits as option letters.
237 to be used with programs that expect a number
240 This practice is wrong, and should not be used in any current development.
241 It is provided for backward compatibility
243 The following code fragment works in most (but not all) cases.
244 .Bd -literal -offset indent
248 while ((ch = getopt(argc, argv, "0123456789")) != -1)
250 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9':
252 p = argv[optind - 1];
253 if (p[0] == '-' && p[1] == ch && !p[2])
254 length = strtol(++p, &ep, 10);
255 else if (argv[optind] && argv[optind][1] == ch) {
256 length = strtol((p = argv[optind] + 1),
263 errx(EX_USAGE, "illegal number -- %s", p);