1 .\" $NetBSD: getopt_long.3,v 1.18 2007/07/02 17:56:17 ginsbach 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
37 .Nd get long options from command line argument list
43 .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long_options" "int *index"
47 function is similar to
49 but it accepts options in two forms: words and characters.
52 function provides a superset of the functionality of
55 can be used in two ways.
56 In the first way, every long option understood by the program has a
57 corresponding short option, and the option structure is only used to
58 translate from long options to short options.
59 When used in this fashion,
61 behaves identically to
63 This is a good way to add long option processing to an existing program
64 with the minimum of rewriting.
66 In the second mechanism, a long option sets a flag in the
68 structure passed, or will store a pointer to the command line argument
71 structure passed to it for options that take arguments.
72 Additionally, the long option's argument may be specified as a single
73 argument with an equal sign, e.g.
75 myprogram --myoption=somevalue
78 When a long option is processed the call to
81 For this reason, long option processing without
82 shortcuts is not backwards compatible with
85 It is possible to combine these methods, providing for long options
86 processing with short option equivalents for some options.
87 Less frequently used options would be processed as long options only.
89 Abbreviated long option names are accepted when
91 processes long options if the abbreviation is unique.
92 An exact match is always preferred for a defined long option.
96 call requires a structure to be initialized describing the long options.
109 field should contain the option name without the leading double dash.
113 field should be one of:
114 .Bl -tag -width "optional_argument"
116 no argument to the option is expect.
117 .It Li required_argument
118 an argument to the option is required.
119 .It Li optional_argument
120 an argument to the option may be presented.
127 then the integer pointed to by it will be set to the value in the
136 field will be returned.
143 to the corresponding short option will make this function act just
151 the integer it points to will be set to the index of the long option
156 The last element of the
158 array has to be filled with zeroes (see
162 .Bd -literal -compact
168 /* options descriptor */
169 static struct option longopts[] = {
170 { "buffy", no_argument, 0, 'b' },
171 { "fluoride", required_argument, 0, 'f' },
172 { "daggerset", no_argument, \*[Am]daggerset, 1 },
177 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
183 if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
184 (void)fprintf(stderr,
185 "myname: %s: %s\en", optarg, strerror(errno));
191 fprintf(stderr,"Buffy will use her dagger to "
192 "apply fluoride to dracula's teeth\en");
202 .Sh IMPLEMENTATION DIFFERENCES
203 This section describes differences to the GNU implementation
204 found in glibc-2.1.3:
205 .Bl -tag -width "xxx"
207 handling of - as first char of option string in presence of
208 environment variable POSIXLY_CORRECT:
209 .Bl -tag -width "NetBSD"
211 ignores POSIXLY_CORRECT and returns non-options as
212 arguments to option '\e1'.
214 honors POSIXLY_CORRECT and stops at the first non-option.
217 handling of :: in options string in presence of POSIXLY_CORRECT:
218 .Bl -tag -width "NetBSD"
220 GNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
221 mean the preceding option takes an optional argument.
224 return value in case of missing argument if first character
225 (after + or -) in option string is not ':':
226 .Bl -tag -width "NetBSD"
230 returns ':' (since NetBSD's getopt does).
233 handling of --a in getopt:
234 .Bl -tag -width "NetBSD"
236 parses this as option '-', option 'a'.
238 parses this as '--', and returns \-1 (ignoring the a).
239 (Because the original getopt does.)
242 setting of optopt for long options with flag !=
244 .Bl -tag -width "NetBSD"
248 sets optopt to 0 (since val would never be returned).
251 handling of -W with W; in option string in getopt (not getopt_long):
252 .Bl -tag -width "NetBSD"
256 returns \-1, with optind pointing past the argument of -W
257 (as if `-W arg' were `--arg', and thus '--' had been found).
258 .\" How should we treat W; in the option string when called via
259 .\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning?
262 setting of optarg for long options without an argument that are
263 invoked via -W (W; in option string):
264 .Bl -tag -width "NetBSD"
266 sets optarg to the option name (the argument of -W).
270 (the argument of the long option).
273 handling of -W with an argument that is not (a prefix to) a known
274 long option (W; in option string):
275 .Bl -tag -width "NetBSD"
277 returns -W with optarg set to the unknown option.
279 treats this as an error (unknown option) and returns '?' with
280 optopt set to 0 and optarg set to
282 (as GNU's man page documents).
285 The error messages are different.
287 NetBSD does not permute the argument vector at the same points in
288 the calling sequence as GNU does.
289 The aspects normally used by the caller
290 (ordering after \-1 is returned, value of optind relative
291 to current positions) are the same, though.
292 (We do fewer variable swaps.)
299 function first appeared in GNU libiberty.
302 implementation appeared in 1.5.
304 The implementation can completely replace
306 but right now we are using separate code.
310 argument is not really const.