diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt_long.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt_long.3 | 445 |
1 files changed, 445 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 new file mode 100644 index 0000000000..eaa5ead1c7 --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,445 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.19 2011/03/21 13:41:50 espie Exp $ | ||
2 | .\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1988, 1991, 1993 | ||
5 | .\" The Regents of the University of California. All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 | ||
32 | .\" | ||
33 | .Dd $Mdocdate: March 21 2011 $ | ||
34 | .Dt GETOPT_LONG 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm getopt_long , | ||
38 | .Nm getopt_long_only | ||
39 | .Nd get long options from command line argument list | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <getopt.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_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" | ||
49 | .Ft int | ||
50 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" | ||
51 | .Sh DESCRIPTION | ||
52 | The | ||
53 | .Fn getopt_long | ||
54 | function is similar to | ||
55 | .Xr getopt 3 | ||
56 | but it accepts options in two forms: words and characters. | ||
57 | The | ||
58 | .Fn getopt_long | ||
59 | function provides a superset of the functionality of | ||
60 | .Xr getopt 3 . | ||
61 | .Fn getopt_long | ||
62 | can be used in two ways. | ||
63 | In the first way, every long option understood by the program has a | ||
64 | corresponding short option, and the option structure is only used to | ||
65 | translate from long options to short options. | ||
66 | When used in this fashion, | ||
67 | .Fn getopt_long | ||
68 | behaves identically to | ||
69 | .Xr getopt 3 . | ||
70 | This is a good way to add long option processing to an existing program | ||
71 | with the minimum of rewriting. | ||
72 | .Pp | ||
73 | In the second mechanism, a long option sets a flag in the | ||
74 | .Fa option | ||
75 | structure passed, or will store a pointer to the command line argument | ||
76 | in the | ||
77 | .Fa option | ||
78 | structure passed to it for options that take arguments. | ||
79 | Additionally, the long option's argument may be specified as a single | ||
80 | argument with an equal sign, e.g. | ||
81 | .Bd -literal -offset indent | ||
82 | $ myprogram --myoption=somevalue | ||
83 | .Ed | ||
84 | .Pp | ||
85 | When a long option is processed, the call to | ||
86 | .Fn getopt_long | ||
87 | will return 0. | ||
88 | For this reason, long option processing without | ||
89 | shortcuts is not backwards compatible with | ||
90 | .Xr getopt 3 . | ||
91 | .Pp | ||
92 | It is possible to combine these methods, providing for long options | ||
93 | processing with short option equivalents for some options. | ||
94 | Less frequently used options would be processed as long options only. | ||
95 | .Pp | ||
96 | Abbreviated long option names are accepted when | ||
97 | .Fn getopt_long | ||
98 | processes long options if the abbreviation is unique. | ||
99 | An exact match is always preferred for a defined long option. | ||
100 | .Pp | ||
101 | The | ||
102 | .Fn getopt_long | ||
103 | call requires an array to be initialized describing the long | ||
104 | options. | ||
105 | Each element of the array is a structure: | ||
106 | .Bd -literal -offset indent | ||
107 | struct option { | ||
108 | char *name; | ||
109 | int has_arg; | ||
110 | int *flag; | ||
111 | int val; | ||
112 | }; | ||
113 | .Ed | ||
114 | .Pp | ||
115 | The | ||
116 | .Fa name | ||
117 | field should contain the option name without the leading double dash. | ||
118 | .Pp | ||
119 | The | ||
120 | .Fa has_arg | ||
121 | field should be one of: | ||
122 | .Pp | ||
123 | .Bl -tag -width "optional_argument" -compact -offset indent | ||
124 | .It Dv no_argument | ||
125 | no argument to the option is expected. | ||
126 | .It Dv required_argument | ||
127 | an argument to the option is required. | ||
128 | .It Dv optional_argument | ||
129 | an argument to the option may be presented. | ||
130 | .El | ||
131 | .Pp | ||
132 | If | ||
133 | .Fa flag | ||
134 | is not | ||
135 | .Dv NULL , | ||
136 | then the integer pointed to by it will be set to the value in the | ||
137 | .Fa val | ||
138 | field. | ||
139 | If the | ||
140 | .Fa flag | ||
141 | field is | ||
142 | .Dv NULL , | ||
143 | then the | ||
144 | .Fa val | ||
145 | field will be returned. | ||
146 | Setting | ||
147 | .Fa flag | ||
148 | to | ||
149 | .Dv NULL | ||
150 | and setting | ||
151 | .Fa val | ||
152 | to the corresponding short option will make this function act just | ||
153 | like | ||
154 | .Xr getopt 3 . | ||
155 | .Pp | ||
156 | If the | ||
157 | .Fa longindex | ||
158 | field is not | ||
159 | .Dv NULL , | ||
160 | then the integer pointed to by it will be set to the index of the long | ||
161 | option relative to | ||
162 | .Fa longopts . | ||
163 | .Pp | ||
164 | The last element of the | ||
165 | .Fa longopts | ||
166 | array has to be filled with zeroes. | ||
167 | .Pp | ||
168 | The | ||
169 | .Fn getopt_long_only | ||
170 | function behaves identically to | ||
171 | .Fn getopt_long | ||
172 | with the exception that long options may start with | ||
173 | .Sq - | ||
174 | in addition to | ||
175 | .Sq -- . | ||
176 | If an option starting with | ||
177 | .Sq - | ||
178 | does not match a long option but does match a single-character option, | ||
179 | the single-character option is returned. | ||
180 | .Sh RETURN VALUES | ||
181 | If the | ||
182 | .Fa flag | ||
183 | field in | ||
184 | .Li struct option | ||
185 | is | ||
186 | .Dv NULL , | ||
187 | .Fn getopt_long | ||
188 | and | ||
189 | .Fn getopt_long_only | ||
190 | return the value specified in the | ||
191 | .Fa val | ||
192 | field, which is usually just the corresponding short option. | ||
193 | If | ||
194 | .Fa flag | ||
195 | is not | ||
196 | .Dv NULL , | ||
197 | these functions return 0 and store | ||
198 | .Fa val | ||
199 | in the location pointed to by | ||
200 | .Fa flag . | ||
201 | These functions return | ||
202 | .Sq \&: | ||
203 | if there was a missing option argument, | ||
204 | .Sq \&? | ||
205 | if the user specified an unknown or ambiguous option, and | ||
206 | \-1 when the argument list has been exhausted. | ||
207 | .Sh IMPLEMENTATION DIFFERENCES | ||
208 | This section describes differences to the GNU implementation | ||
209 | found in glibc-2.1.3: | ||
210 | .Bl -bullet | ||
211 | .It | ||
212 | handling of | ||
213 | .Ql - | ||
214 | within the option string (not the first character): | ||
215 | .Bl -tag -width "OpenBSD" | ||
216 | .It GNU | ||
217 | treats a | ||
218 | .Ql - | ||
219 | on the command line as a non-argument. | ||
220 | .It OpenBSD | ||
221 | a | ||
222 | .Ql - | ||
223 | within the option string matches a | ||
224 | .Ql - | ||
225 | (single dash) on the command line. | ||
226 | This functionality is provided for backward compatibility with | ||
227 | programs, such as | ||
228 | .Xr su 1 , | ||
229 | that use | ||
230 | .Ql - | ||
231 | as an option flag. | ||
232 | This practice is wrong, and should not be used in any current development. | ||
233 | .El | ||
234 | .It | ||
235 | handling of | ||
236 | .Ql :: | ||
237 | in the option string in the presence of | ||
238 | .Ev POSIXLY_CORRECT : | ||
239 | .Bl -tag -width "OpenBSD" | ||
240 | .It Both | ||
241 | GNU and | ||
242 | .Ox | ||
243 | ignore | ||
244 | .Ev POSIXLY_CORRECT | ||
245 | here and take | ||
246 | .Ql :: | ||
247 | to mean the preceding option takes an optional argument. | ||
248 | .El | ||
249 | .It | ||
250 | return value in case of missing argument if first character | ||
251 | (after | ||
252 | .Ql + | ||
253 | or | ||
254 | .Ql - ) | ||
255 | in the option string is not | ||
256 | .Ql \&: : | ||
257 | .Bl -tag -width "OpenBSD" | ||
258 | .It GNU | ||
259 | returns | ||
260 | .Ql \&? | ||
261 | .It OpenBSD | ||
262 | returns | ||
263 | .Ql \&: | ||
264 | (since | ||
265 | .Ox Ns 's | ||
266 | .Xr getopt 3 | ||
267 | does). | ||
268 | .El | ||
269 | .It | ||
270 | handling of | ||
271 | .Ql --a | ||
272 | in | ||
273 | .Xr getopt 3 : | ||
274 | .Bl -tag -width "OpenBSD" | ||
275 | .It GNU | ||
276 | parses this as option | ||
277 | .Ql - , | ||
278 | option | ||
279 | .Ql a . | ||
280 | .It OpenBSD | ||
281 | parses this as | ||
282 | .Ql -- , | ||
283 | and returns \-1 (ignoring the | ||
284 | .Ql a ) | ||
285 | (because the original | ||
286 | .Fn getopt | ||
287 | did.) | ||
288 | .El | ||
289 | .It | ||
290 | setting of | ||
291 | .Va optopt | ||
292 | for long options with | ||
293 | .Va flag | ||
294 | .No non- Ns Dv NULL : | ||
295 | .Bl -tag -width "OpenBSD" | ||
296 | .It GNU | ||
297 | sets | ||
298 | .Va optopt | ||
299 | to | ||
300 | .Va val . | ||
301 | .It OpenBSD | ||
302 | sets | ||
303 | .Va optopt | ||
304 | to 0 (since | ||
305 | .Va val | ||
306 | would never be returned). | ||
307 | .El | ||
308 | .It | ||
309 | handling of | ||
310 | .Ql -W | ||
311 | with | ||
312 | .Ql W; | ||
313 | in the option string in | ||
314 | .Xr getopt 3 | ||
315 | (not | ||
316 | .Fn getopt_long ) : | ||
317 | .Bl -tag -width "OpenBSD" | ||
318 | .It GNU | ||
319 | causes a segmentation fault. | ||
320 | .It OpenBSD | ||
321 | no special handling is done; | ||
322 | .Ql W; | ||
323 | is interpreted as two separate options, neither of which take an argument. | ||
324 | .El | ||
325 | .It | ||
326 | setting of | ||
327 | .Va optarg | ||
328 | for long options without an argument that are invoked via | ||
329 | .Ql -W | ||
330 | (with | ||
331 | .Ql W; | ||
332 | in the option string): | ||
333 | .Bl -tag -width "OpenBSD" | ||
334 | .It GNU | ||
335 | sets | ||
336 | .Va optarg | ||
337 | to the option name (the argument of | ||
338 | .Ql -W ) . | ||
339 | .It OpenBSD | ||
340 | sets | ||
341 | .Va optarg | ||
342 | to | ||
343 | .Dv NULL | ||
344 | (the argument of the long option). | ||
345 | .El | ||
346 | .It | ||
347 | handling of | ||
348 | .Ql -W | ||
349 | with an argument that is not (a prefix to) a known long option | ||
350 | (with | ||
351 | .Ql W; | ||
352 | in the option string): | ||
353 | .Bl -tag -width "OpenBSD" | ||
354 | .It GNU | ||
355 | returns | ||
356 | .Ql -W | ||
357 | with | ||
358 | .Va optarg | ||
359 | set to the unknown option. | ||
360 | .It OpenBSD | ||
361 | treats this as an error (unknown option) and returns | ||
362 | .Ql \&? | ||
363 | with | ||
364 | .Va optopt | ||
365 | set to 0 and | ||
366 | .Va optarg | ||
367 | set to | ||
368 | .Dv NULL | ||
369 | (as GNU's man page documents). | ||
370 | .El | ||
371 | .It | ||
372 | The error messages are different. | ||
373 | .It | ||
374 | .Ox | ||
375 | does not permute the argument vector at the same points in | ||
376 | the calling sequence as GNU does. | ||
377 | The aspects normally used by the caller | ||
378 | (ordering after \-1 is returned, value of | ||
379 | .Va optind | ||
380 | relative to current positions) are the same, though. | ||
381 | (We do fewer variable swaps.) | ||
382 | .El | ||
383 | .Sh ENVIRONMENT | ||
384 | .Bl -tag -width Ev | ||
385 | .It Ev POSIXLY_CORRECT | ||
386 | If set, option processing stops when the first non-option is found and | ||
387 | a leading | ||
388 | .Sq + | ||
389 | in the | ||
390 | .Ar optstring | ||
391 | is ignored. | ||
392 | .El | ||
393 | .Sh EXAMPLES | ||
394 | .Bd -literal | ||
395 | int bflag, ch, fd; | ||
396 | int daggerset; | ||
397 | |||
398 | /* options descriptor */ | ||
399 | static struct option longopts[] = { | ||
400 | { "buffy", no_argument, NULL, 'b' }, | ||
401 | { "fluoride", required_argument, NULL, 'f' }, | ||
402 | { "daggerset", no_argument, &daggerset, 1 }, | ||
403 | { NULL, 0, NULL, 0 } | ||
404 | }; | ||
405 | |||
406 | bflag = 0; | ||
407 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
408 | switch (ch) { | ||
409 | case 'b': | ||
410 | bflag = 1; | ||
411 | break; | ||
412 | case 'f': | ||
413 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
414 | err(1, "unable to open %s", optarg); | ||
415 | break; | ||
416 | case 0: | ||
417 | if (daggerset) | ||
418 | fprintf(stderr, "Buffy will use her dagger to " | ||
419 | "apply fluoride to dracula's teeth\en"); | ||
420 | break; | ||
421 | default: | ||
422 | usage(); | ||
423 | /* NOTREACHED */ | ||
424 | } | ||
425 | argc -= optind; | ||
426 | argv += optind; | ||
427 | .Ed | ||
428 | .Sh SEE ALSO | ||
429 | .Xr getopt 3 | ||
430 | .Sh HISTORY | ||
431 | The | ||
432 | .Fn getopt_long | ||
433 | and | ||
434 | .Fn getopt_long_only | ||
435 | functions first appeared in GNU libiberty. | ||
436 | This implementation first appeared in | ||
437 | .Ox 3.3 . | ||
438 | .Sh BUGS | ||
439 | The | ||
440 | .Ar argv | ||
441 | argument is not really | ||
442 | .Dv const | ||
443 | as its elements may be permuted (unless | ||
444 | .Ev POSIXLY_CORRECT | ||
445 | is set). | ||