1 .\" $NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc Exp $
2 .\" $DragonFly: src/lib/libc/stdlib/getopt_long.3,v 1.3 2006/02/17 19:35:06 swildner 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
31 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
38 .Nd get long options from command line argument list
44 .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index"
48 function is similar to
50 but it accepts options in two forms: words and characters.
53 function provides a superset of the functionality of
56 can be used in two ways.
57 In the first way, every long option understood by the program has a
58 corresponding short option, and the option structure is only used to
59 translate from long options to short options.
60 When used in this fashion,
62 behaves identically to
64 This is a good way to add long option processing to an existing program
65 with the minimum of rewriting.
67 In the second mechanism, a long option sets a flag in the
69 structure passed, or will store a pointer to the command line argument
72 structure passed to it for options that take arguments.
73 Additionally, the long option's argument may be specified as a single
74 argument with an equal sign, e.g.
76 myprogram --myoption=somevalue
79 When a long option is processed the call to
82 For this reason, long option processing without
83 shortcuts is not backwards compatible with
86 It is possible to combine these methods, providing for long options
87 processing with short option equivalents for some options.
88 Less frequently used options would be processed as long options only.
92 call requires a structure to be initialized describing the long options.
105 field should contain the option name without the leading double dash.
109 field should be one of:
110 .Bl -tag -width "optional_argument"
112 no argument to the option is expect.
113 .It Li required_argument
114 an argument to the option is required.
115 .It Li optional_argument
116 an argument to the option may be presented.
123 then the integer pointed to by it will be set to the value in the
132 field will be returned.
139 to the corresponding short option will make this function act just
142 .Sh IMPLEMENTATION DIFFERENCES
143 This section describes differences to the GNU implementation
144 found in glibc-2.1.3:
145 .Bl -tag -width "xxx"
147 handling of - as first char of option string in presence of
148 environment variable POSIXLY_CORRECT:
149 .Bl -tag -width "NetBSD"
151 ignores POSIXLY_CORRECT and returns non-options as
152 arguments to option '\e1'.
154 honors POSIXLY_CORRECT and stops at the first non-option.
157 handling of :: in options string in presence of POSIXLY_CORRECT:
158 .Bl -tag -width "NetBSD"
160 GNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
161 mean the preceding option takes an optional argument.
164 return value in case of missing argument if first character
165 (after + or -) in option string is not ':':
166 .Bl -tag -width "NetBSD"
170 returns ':' (since NetBSD's getopt does).
173 handling of --a in getopt:
174 .Bl -tag -width "NetBSD"
176 parses this as option '-', option 'a'.
178 parses this as '--', and returns \-1 (ignoring the a).
179 (Because the original getopt does.)
182 setting of optopt for long options with flag !=
184 .Bl -tag -width "NetBSD"
188 sets optopt to 0 (since val would never be returned).
191 handling of -W with W; in option string in getopt (not getopt_long):
192 .Bl -tag -width "NetBSD"
196 returns \-1, with optind pointing past the argument of -W
197 (as if `-W arg' were `--arg', and thus '--' had been found).
198 .\" How should we treat W; in the option string when called via
199 .\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning?
202 setting of optarg for long options without an argument that are
203 invoked via -W (W; in option string):
204 .Bl -tag -width "NetBSD"
206 sets optarg to the option name (the argument of -W).
210 (the argument of the long option).
213 handling of -W with an argument that is not (a prefix to) a known
214 long option (W; in option string):
215 .Bl -tag -width "NetBSD"
217 returns -W with optarg set to the unknown option.
219 treats this as an error (unknown option) and returns '?' with
220 optopt set to 0 and optarg set to
222 (as GNU's man page documents).
225 The error messages are different.
227 NetBSD does not permute the argument vector at the same points in
228 the calling sequence as GNU does.
229 The aspects normally used by the caller
230 (ordering after \-1 is returned, value of optind relative
231 to current positions) are the same, though.
232 (We do fewer variable swaps.)
235 .Bd -literal -compact
241 /* options descriptor */
242 static struct option longopts[] = {
243 { "buffy", no_argument, 0, 'b' },
244 { "floride", required_argument, 0, 'f' },
245 { "daggerset", no_argument, \*[Am]daggerset, 1 },
250 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
256 if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
257 (void)fprintf(stderr,
258 "myname: %s: %s\en", optarg, strerror(errno));
264 fprintf(stderr,"Buffy will use her dagger to "
265 "apply floride to dracula's teeth\en");
280 function first appeared in GNU libiberty.
283 implementation appeared in 1.5.