Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | .\" Copyright (c) 1988, 1991 Regents of the University of California. |
2 | .\" 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 | .\" | |
dee6c867 C |
32 | .\" from: @(#)getopt.3 6.16 (Berkeley) 4/19/91 |
33 | .\" $Id: getopt.3,v 1.5 1993/10/13 17:23:39 jtc Exp $ | |
15637ed4 RG |
34 | .\" |
35 | .Dd April 19, 1991 | |
36 | .Dt GETOPT 3 | |
37 | .Os BSD 4.3 | |
38 | .Sh NAME | |
39 | .Nm getopt | |
40 | .Nd get option letter from argv | |
41 | .Sh SYNOPSIS | |
dee6c867 | 42 | .Fd #include <unistd.h> |
15637ed4 RG |
43 | .Ft int |
44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | |
dee6c867 C |
45 | .Vt extern char *optarg; |
46 | .Vt extern int optind; | |
47 | .Vt extern int opterr; | |
48 | .Vt extern int optopt; | |
15637ed4 RG |
49 | .Sh DESCRIPTION |
50 | The | |
51 | .Fn getopt | |
52 | function gets | |
53 | the next | |
54 | .Em known | |
55 | option character from | |
56 | .Fa argv . | |
57 | An option character is | |
58 | .Em known | |
59 | if it has been specified in the string of accepted option characters, | |
60 | .Fa optstring . | |
61 | .Pp | |
62 | The option string | |
63 | .Fa optstring | |
64 | may contain the following characters; letters and | |
65 | letters followed by a colon to indicate an option argument | |
66 | is to follow. It does not matter to | |
67 | .Fn getopt | |
68 | if a following argument has leading white space. | |
69 | .Pp | |
70 | On return from | |
71 | .Fn getopt , | |
72 | .Va optarg | |
73 | points to an option argument, if it is anticipated, | |
74 | and the variable | |
75 | .Va optind | |
76 | contains the index to the next | |
77 | .Fa argv | |
78 | argument for a subsequent call | |
79 | to | |
80 | .Fn getopt . | |
81 | .Pp | |
82 | The variable | |
83 | .Va opterr | |
84 | and | |
85 | .Va optind | |
86 | are both initialized to 1. | |
87 | In order to use | |
88 | .Fn getopt | |
89 | to evaluate multiple sets of arguments, or to evaluate a single set of | |
90 | arguments multiple times, | |
91 | .Va optind | |
92 | must be initialized to the number of argv entries to be skipped in each | |
93 | evaluation. | |
94 | .Pp | |
95 | The | |
96 | .Fn getopt | |
97 | function | |
dee6c867 C |
98 | returns \-1 |
99 | when the argument list is exhausted. | |
15637ed4 RG |
100 | The interpretation of options in the argument list may be cancelled |
101 | by the option | |
102 | .Ql -- | |
103 | (double dash) which causes | |
104 | .Fn getopt | |
dee6c867 | 105 | to signal the end of argument processing and return \-1. |
15637ed4 RG |
106 | When all options have been processed (i.e., up to the first non-option |
107 | argument), | |
108 | .Fn getopt | |
dee6c867 | 109 | returns \-1. |
15637ed4 | 110 | .Sh DIAGNOSTICS |
dee6c867 | 111 | If the |
15637ed4 | 112 | .Fn getopt |
dee6c867 C |
113 | function encounters an option character that is not contained in |
114 | .Fa optstring , | |
115 | it returns a question mark | |
116 | .Pq ? | |
117 | character. | |
118 | If it detects a missing option argument, it returns a colon | |
119 | .Pq \: | |
120 | character if the first character of | |
121 | .Fa optstring | |
122 | is a colon, otherwise it returns a question mark. | |
123 | In either case, a diagnostic message is written to | |
124 | .Em stderr | |
125 | unless the application has set | |
15637ed4 | 126 | .Va opterr |
dee6c867 C |
127 | to zero or the first character of |
128 | .Fa optstring | |
129 | is a colon. | |
15637ed4 | 130 | .Sh EXAMPLE |
dee6c867 C |
131 | .\" The following example comes from section E.9.7 of the IEEE 1003.2-90 |
132 | .\" standard (POSIX.2). | |
133 | The following code fragment shows how one might process the arguments for | |
134 | a utility that can take the mutually exclusive options | |
135 | .Em a | |
136 | and | |
137 | .Em b | |
138 | and the options | |
139 | .Em f | |
140 | and | |
141 | .Em o , | |
142 | both of which require arguments: | |
143 | .Pp | |
15637ed4 | 144 | .Bd -literal -compact |
dee6c867 | 145 | #include <unistd.h> |
15637ed4 | 146 | |
dee6c867 C |
147 | int |
148 | main (argc, argv) | |
149 | int argc; | |
150 | char *argv[]; | |
151 | { | |
152 | int c, bflg, aflg, errflg = 0; | |
153 | char *ifile, *ofile; | |
154 | extern char *optarg; | |
155 | extern int optind, optopt; | |
156 | ||
157 | . . . | |
158 | ||
159 | while ((c = getopt(argc, argv, ":abf:o:")) != -1) { | |
160 | switch(ch) { | |
161 | case 'a': | |
162 | if (bflg) | |
163 | errflg = 1; | |
164 | else | |
165 | aflg = 1; | |
166 | break; | |
167 | case 'b': | |
168 | if (aflg) | |
169 | errflg = 1; | |
170 | else | |
171 | bflg = 1; | |
172 | break; | |
173 | case 'f': | |
174 | ifile = optarg; | |
175 | break; | |
176 | case 'o': | |
177 | ofile = optarg; | |
178 | break; | |
179 | case ':': /* -f or -o without option-arg */ | |
180 | fprintf (stderr, | |
181 | "Option -%c requires an option-argument\\n", | |
182 | optopt); | |
183 | errflg = 1; | |
184 | break; | |
185 | case '?': | |
186 | fprintf (stderr, | |
187 | "Unrecognized option: -%c\\n", | |
188 | optopt); | |
189 | errflg = 1; | |
190 | break; | |
15637ed4 | 191 | } |
dee6c867 C |
192 | } |
193 | ||
194 | if (errflg) { | |
195 | fprintf (stderr, "usage: . . .\\n"); | |
196 | exit (2); | |
197 | } | |
198 | ||
199 | argc -= optind; | |
200 | argv += optind; | |
201 | ||
202 | . . . | |
203 | ||
15637ed4 | 204 | } |
15637ed4 | 205 | .Ed |
dee6c867 C |
206 | .Sh STANDARDS |
207 | The | |
208 | .Fn getopt | |
209 | function conforms to | |
210 | .St -p1003.2-92 . | |
15637ed4 RG |
211 | .Sh HISTORY |
212 | The | |
213 | .Fn getopt | |
214 | function appeared | |
215 | .Bx 4.3 . | |
216 | .Sh BUGS | |
dee6c867 C |
217 | The |
218 | .Fn getopt | |
219 | function was once specified to return | |
220 | .Dv EOF | |
221 | instead of \-1. | |
222 | This was changed by | |
223 | .St -p1003.2-92 | |
224 | to decouple | |
225 | .Fn getopt | |
226 | from | |
227 | .Pa <stdio.h> . | |
228 | .Pp | |
15637ed4 RG |
229 | Option arguments are allowed to begin with |
230 | .Dq Li \- ; | |
231 | this is reasonable but | |
232 | reduces the amount of error checking possible. | |
233 | .Pp | |
234 | A single dash | |
235 | .Dq Li - | |
236 | may be specified as an character in | |
237 | .Fa optstring , | |
238 | however it should | |
239 | .Em never | |
240 | have an argument associated with it. | |
241 | This allows | |
242 | .Fn getopt | |
243 | to be used with programs that expect | |
244 | .Dq Li - | |
245 | as an option flag. | |
246 | This practice is wrong, and should not be used in any current development. | |
247 | It is provided for backward compatibility | |
248 | .Em only . | |
249 | By default, a single dash causes | |
250 | .Fn getopt | |
dee6c867 | 251 | to returns \-1. |
15637ed4 RG |
252 | This is, we believe, compatible with System V. |
253 | .Pp | |
254 | It is also possible to handle digits as option letters. | |
255 | This allows | |
256 | .Fn getopt | |
257 | to be used with programs that expect a number | |
258 | .Pq Dq Li \&-\&3 | |
259 | as an option. | |
260 | This practice is wrong, and should not be used in any current development. | |
261 | It is provided for backward compatibility | |
262 | .Em only . | |
263 | The following code fragment works fairly well. | |
264 | .Bd -literal -offset indent | |
265 | int length; | |
266 | char *p; | |
267 | ||
dee6c867 | 268 | while ((c = getopt(argc, argv, "0123456789")) != -1) |
15637ed4 RG |
269 | switch (c) { |
270 | case '0': case '1': case '2': case '3': case '4': | |
271 | case '5': case '6': case '7': case '8': case '9': | |
272 | p = argv[optind - 1]; | |
273 | if (p[0] == '-' && p[1] == ch && !p[2]) | |
274 | length = atoi(++p); | |
275 | else | |
276 | length = atoi(argv[optind] + 1); | |
277 | break; | |
278 | } | |
279 | } | |
280 | .Ed |