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. 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.
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
30 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
31 .\" $FreeBSD: src/lib/libc/stdlib/getopt.3,v 1.26 2007/01/09 00:28:10 imp Exp $
38 .Nd get option character from command line argument list
43 .Vt extern char *optarg ;
44 .Vt extern int optind ;
45 .Vt extern int optopt ;
46 .Vt extern int opterr ;
47 .Vt extern int optreset ;
49 .Fn getopt "int argc" "char * const argv[]" "const char *optstring"
53 function incrementally parses a command line argument list
58 An option character is
60 if it has been specified in the string of accepted option characters,
65 may contain the following elements:
66 .Bl -bullet -offset indent
68 An individual character.
69 For example, an option string
74 A character followed by a colon, which indicates the option requires
76 It does not matter if a following argument has leading white space.
77 For example, an option string
79 recognizes an option and argument
82 .Dq Fl x Ns Ar argument .
84 A character followed by two colons, which indicates the option argument
86 The following argument must be in the
88 word as the option name itself.
91 is set to the rest of the
96 if there were no more characters in the current word.
100 For example, an option string
104 or an option and argument
105 .Dq Fl x Ns Ar argument .
111 points to an option argument, if it is anticipated,
114 contains the index to the next
116 argument for a subsequent call
123 option character returned by
130 are both initialized to 1.
133 variable may be set to another value before a set of calls to
135 in order to skip over more or less
141 to evaluate multiple sets of arguments, or to evaluate a single set of
142 arguments multiple times,
145 must be set to 1 before the second and each additional set of calls to
149 must be reinitialized.
153 function returns \-1 when the argument list is exhausted.
154 The interpretation of options in the argument list may be cancelled
157 (double dash) which causes
159 to signal the end of argument processing and return \-1.
160 When all options have been processed (i.e., up to the first non-option
167 function returns the next known option character in
171 encounters a character not found in
173 or if it detects a missing option argument,
181 then a missing option argument causes
183 to be returned instead of
185 In either case, the variable
187 is set to the character that caused the error.
190 function returns \-1 when the argument list is exhausted.
192 .Bd -literal -compact
197 while ((ch = getopt(argc, argv, "bf:")) != -1) {
203 if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
205 "myname: %s: %s\en", optarg, strerror(errno));
219 function encounters a character not found in the string
222 a missing option argument it writes an error message to the
228 to a zero will disable these error messages.
233 then a missing option argument causes a
235 to be returned in addition to suppressing any error messages.
237 Option arguments are allowed to begin with
239 this is reasonable but reduces the amount of error checking possible.
247 variable was added to make it possible to call the
249 function multiple times.
250 This is an extension to the
261 function was once specified to return
273 may be specified as a character in
277 have an argument associated with it.
280 to be used with programs that expect
283 This practice is wrong, and should not be used in any current development.
284 It is provided for backward compatibility
286 Care should be taken not to use
288 as the first character in
290 to avoid a semantic conflict with
293 which assigns different meaning to an
297 By default, a single dash causes
301 It is also possible to handle digits as option letters.
304 to be used with programs that expect a number
307 This practice is wrong, and should not be used in any current development.
308 It is provided for backward compatibility
310 The following code fragment works in most cases.
311 .Bd -literal -offset indent
316 while ((ch = getopt(argc, argv, "0123456789")) != -1) {
318 case '0': case '1': case '2': case '3': case '4':
319 case '5': case '6': case '7': case '8': case '9':
320 p = argv[optind - 1];
321 if (p[0] == '-' \*[Am]\*[Am] p[1] == ch \*[Am]\*[Am] !p[2]) {
324 } else if (argv[optind] \*[Am]\*[Am] argv[optind][1] == ch) {
325 length = strtol((p = argv[optind] + 1),
332 errx(EX_USAGE, "illegal number -- %s", p);