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
31 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
32 .\" $FreeBSD: src/lib/libc/stdlib/getopt_long.3,v 1.13 2005/01/20 09:17:04 ru Exp $
33 .\" $DragonFly: src/lib/libc/stdlib/getopt_long.3,v 1.7 2008/11/23 21:55:52 swildner Exp $
41 .Nd get long options from command line argument list
46 .Vt extern char *optarg ;
47 .Vt extern int optind ;
48 .Vt extern int optopt ;
49 .Vt extern int opterr ;
50 .Vt extern int optreset ;
53 .Fa "int argc" "char * const *argv" "const char *optstring"
54 .Fa "const struct option *longopts" "int *longindex"
58 .Fa "int argc" "char * const *argv" "const char *optstring"
59 .Fa "const struct option *longopts" "int *longindex"
64 function is similar to
66 but it accepts options in two forms: words and characters.
69 function provides a superset of the functionality of
74 can be used in two ways.
75 In the first way, every long option understood
76 by the program has a corresponding short option, and the option
77 structure is only used to translate from long options to short
79 When used in this fashion,
81 behaves identically to
83 This is a good way to add long option processing to an existing program
84 with the minimum of rewriting.
86 In the second mechanism, a long option sets a flag in the
88 structure passed, or will store a pointer to the command line argument
91 structure passed to it for options that take arguments.
93 the long option's argument may be specified as a single argument with
96 .Dl "myprogram --myoption=somevalue"
98 When a long option is processed, the call to
101 For this reason, long option processing without
102 shortcuts is not backwards compatible with
105 It is possible to combine these methods, providing for long options
106 processing with short option equivalents for some options.
108 frequently used options would be processed as long options only.
112 call requires a structure to be initialized describing the long
115 .Bd -literal -offset indent
126 field should contain the option name without the leading double dash.
130 field should be one of:
132 .Bl -tag -width ".Dv optional_argument" -offset indent -compact
134 no argument to the option is expect
135 .It Dv required_argument
136 an argument to the option is required
137 .It Dv optional_argument
138 an argument to the option may be presented.
145 then the integer pointed to by it will be set to the
155 field will be returned.
162 to the corresponding short option will make this function act just
170 then the integer pointed to by it will be set to the index of the long
174 The last element of the
176 array has to be filled with zeroes.
180 function behaves identically to
182 with the exception that long options may start with
186 If an option starting with
188 does not match a long option but does match a single-character option,
189 the single-character option is returned.
200 return the value specified in the
202 field, which is usually just the corresponding short option.
207 these functions return 0 and store
209 in the location pointed to by
211 These functions return
213 if there was a missing option argument,
215 if the user specified an unknown or ambiguous option, and
216 \-1 when the argument list has been exhausted.
218 .Bl -tag -width ".Ev POSIXLY_CORRECT"
219 .It Ev POSIXLY_CORRECT
220 If set, option processing stops when the first non-option is found and
230 .Bd -literal -compact
234 /* options descriptor */
235 static struct option longopts[] = {
236 { "buffy", no_argument, NULL, 'b' },
237 { "fluoride", required_argument, NULL, 'f' },
238 { "daggerset", no_argument, \*[Am]daggerset, 1 },
243 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
249 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
250 err(1, "unable to open %s", optarg);
254 fprintf(stderr,"Buffy will use her dagger to "
255 "apply fluoride to dracula's teeth\en");
264 .Sh IMPLEMENTATION DIFFERENCES
265 This section describes differences to the
268 found in glibc-2.1.3:
273 .\" as first char of option string in presence of
274 .\" environment variable
275 .\" .Ev POSIXLY_CORRECT :
276 .\" .Bl -tag -width ".Bx"
279 .\" .Ev POSIXLY_CORRECT
280 .\" and returns non-options as
281 .\" arguments to option '\e1'.
284 .\" .Ev POSIXLY_CORRECT
285 .\" and stops at the first non-option.
290 .\" within the option string (not the first character):
291 .\" .Bl -tag -width ".Bx"
295 .\" on the command line as a non-argument.
299 .\" within the option string matches a
301 .\" (single dash) on the command line.
302 .\" This functionality is provided for backward compatibility with
303 .\" programs, such as
307 .\" as an option flag.
308 .\" This practice is wrong, and should not be used in any current development.
313 .\" in options string in presence of
314 .\" .Ev POSIXLY_CORRECT :
315 .\" .Bl -tag -width ".Bx"
321 .\" .Ev POSIXLY_CORRECT
325 .\" mean the preceding option takes an optional argument.
328 .\" Return value in case of missing argument if first character
333 .\" in option string is not
335 .\" .Bl -tag -width ".Bx"
351 .\" .Bl -tag -width ".Bx"
353 .\" parses this as option
360 .\" and returns \-1 (ignoring the
362 .\" (Because the original
369 for long options with
373 .Bl -tag -width ".Bx"
384 would never be returned).
391 .\" in option string in
394 .\" .Fn getopt_long ) :
395 .\" .Bl -tag -width ".Bx"
397 .\" causes a segfault.
399 .\" no special handling is done;
401 .\" is interpreted as two separate options, neither of which take an argument.
406 for long options without an argument that are
411 .Bl -tag -width ".Bx"
415 to the option name (the argument of
422 (the argument of the long option).
427 with an argument that is not (a prefix to) a known
431 .Bl -tag -width ".Bx"
437 set to the unknown option.
439 treats this as an error (unknown option) and returns
452 .\" The error messages are different.
455 does not permute the argument vector at the same points in
456 the calling sequence as
459 The aspects normally used by
460 the caller (ordering after \-1 is returned, value of
463 to current positions) are the same, though.
464 (We do fewer variable swaps.)
473 functions first appeared in
499 argument is not really
501 as its elements may be permuted (unless
505 The implementation can completely replace
507 but right now we are using separate code.