1 .\" $OpenBSD: getopt_long.3,v 1.10 2004/01/06 23:44:28 fgsch Exp $
2 .\" $NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc Exp $
4 .\" Copyright (c) 1988, 1991, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
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.
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
37 .Nd get long options from command line argument list
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 ;
49 .Fa "int argc" "char * const *argv" "const char *optstring"
50 .Fa "const struct option *longopts" "int *longindex"
54 .Fa "int argc" "char * const *argv" "const char *optstring"
55 .Fa "const struct option *longopts" "int *longindex"
60 function is similar to
62 but it accepts options in two forms: words and characters.
65 function provides a superset of the functionality of
70 can be used in two ways.
71 In the first way, every long option understood
72 by the program has a corresponding short option, and the option
73 structure is only used to translate from long options to short
75 When used in this fashion,
77 behaves identically to
79 This is a good way to add long option processing to an existing program
80 with the minimum of rewriting.
82 In the second mechanism, a long option sets a flag in the
84 structure passed, or will store a pointer to the command line argument
87 structure passed to it for options that take arguments.
89 the long option's argument may be specified as a single argument with
92 .Dl "myprogram --myoption=somevalue"
94 When a long option is processed, the call to
97 For this reason, long option processing without
98 shortcuts is not backwards compatible with
101 It is possible to combine these methods, providing for long options
102 processing with short option equivalents for some options.
104 frequently used options would be processed as long options only.
108 call requires a structure to be initialized describing the long
111 .Bd -literal -offset indent
122 field should contain the option name without the leading double dash.
126 field should be one of:
128 .Bl -tag -width ".Dv optional_argument" -offset indent -compact
130 no argument to the option is expected
131 .It Dv required_argument
132 an argument to the option is required
133 .It Dv optional_argument
134 an argument to the option may be presented
141 then the integer pointed to by it will be set to the
151 field will be returned.
158 to the corresponding short option will make this function act just
166 then the integer pointed to by it will be set to the index of the long
170 The last element of the
172 array has to be filled with zeroes.
176 function behaves identically to
178 with the exception that long options may start with
182 If an option starting with
184 does not match a long option but does match a single-character option,
185 the single-character option is returned.
196 return the value specified in the
198 field, which is usually just the corresponding short option.
203 these functions return 0 and store
205 in the location pointed to by
208 These functions return
210 if there was a missing option argument and error messages are suppressed,
212 if the user specified an unknown or ambiguous option, and
213 \-1 when the argument list has been exhausted.
214 The default behavior when a missing option argument is encountered is to write
221 will cause the error message to be suppressed and
223 to be returned instead.
233 also has special meaning.
234 If either of these are specified, they must appear before
239 indicates that processing should be halted at the first non-option argument,
240 matching the default behavior of
242 The default behavior without
244 is to permute non-option arguments to the end of
249 indicates that all non-option arguments should be treated as if they are
250 arguments to a literal
252 flag (i.e., the function call will return the value 1, rather than the char
255 .Bl -tag -width ".Ev POSIXLY_CORRECT"
256 .It Ev POSIXLY_CORRECT
257 If set, option processing stops when the first non-option is found and
267 .Bd -literal -compact
271 /* options descriptor */
272 static struct option longopts[] = {
273 { "buffy", no_argument, NULL, 'b' },
274 { "fluoride", required_argument, NULL, 'f' },
275 { "daggerset", no_argument, \*[Am]daggerset, 1 },
280 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
286 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
287 err(1, "unable to open %s", optarg);
291 fprintf(stderr,"Buffy will use her dagger to "
292 "apply fluoride to dracula's teeth\en");
302 .Sh IMPLEMENTATION DIFFERENCES
303 This section describes differences to the
306 found in glibc-2.1.3:
311 .\" as first char of option string in presence of
312 .\" environment variable
313 .\" .Ev POSIXLY_CORRECT :
314 .\" .Bl -tag -width ".Bx"
317 .\" .Ev POSIXLY_CORRECT
318 .\" and returns non-options as
319 .\" arguments to option '\e1'.
322 .\" .Ev POSIXLY_CORRECT
323 .\" and stops at the first non-option.
328 .\" within the option string (not the first character):
329 .\" .Bl -tag -width ".Bx"
333 .\" on the command line as a non-argument.
337 .\" within the option string matches a
339 .\" (single dash) on the command line.
340 .\" This functionality is provided for backward compatibility with
341 .\" programs, such as
345 .\" as an option flag.
346 .\" This practice is wrong, and should not be used in any current development.
351 .\" in options string in presence of
352 .\" .Ev POSIXLY_CORRECT :
353 .\" .Bl -tag -width ".Bx"
359 .\" .Ev POSIXLY_CORRECT
363 .\" mean the preceding option takes an optional argument.
366 .\" Return value in case of missing argument if first character
371 .\" in option string is not
373 .\" .Bl -tag -width ".Bx"
389 .\" .Bl -tag -width ".Bx"
391 .\" parses this as option
398 .\" and returns \-1 (ignoring the
400 .\" (Because the original
407 for long options with
411 .Bl -tag -width ".Bx"
422 would never be returned).
429 .\" in option string in
432 .\" .Fn getopt_long ) :
433 .\" .Bl -tag -width ".Bx"
435 .\" causes a segfault.
437 .\" no special handling is done;
439 .\" is interpreted as two separate options, neither of which take an argument.
444 for long options without an argument that are
449 .Bl -tag -width ".Bx"
453 to the option name (the argument of
460 (the argument of the long option).
465 with an argument that is not (a prefix to) a known
469 .Bl -tag -width ".Bx"
475 set to the unknown option.
477 treats this as an error (unknown option) and returns
490 .\" The error messages are different.
493 does not permute the argument vector at the same points in
494 the calling sequence as
497 The aspects normally used by
498 the caller (ordering after \-1 is returned, value of
501 to current positions) are the same, though.
502 (We do fewer variable swaps.)
511 functions first appeared in the
537 argument is not really
539 as its elements may be permuted (unless
543 The implementation can completely replace
545 but right now we are using separate code.
548 makes the assumption that the first argument should always be skipped because
549 it's typically the program name.
552 to 0 will indicate that
556 will be set to 1 in the process.
557 This behavior differs from
561 value of 0 as expected and process the first element.