1 .\" $NetBSD: getopt_long.3,v 1.8 2002/06/03 12:01:43 wiz 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. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
35 .\" $FreeBSD: src/lib/libc/stdlib/getopt_long.3,v 1.3 2002/12/18 12:45:10 ru Exp $
42 .Nd get long options from command line argument list
49 .Fa "int argc" "char * const *argv" "const char *optstring"
50 .Fa "struct option *long options" "int *index"
55 function is similar to
57 but it accepts options in two forms: words and characters.
60 function provides a superset of the functionality of
65 can be used in two ways.
66 In the first way, every long option understood
67 by the program has a corresponding short option, and the option
68 structure is only used to translate from long options to short
70 When used in this fashion,
72 behaves identically to
74 This is a good way to add long option processing to an existing program
75 with the minimum of rewriting.
77 In the second mechanism, a long option sets a flag in the
79 structure passed, or will store a pointer to the command line argument
82 structure passed to it for options that take arguments.
84 the long option's argument may be specified as a single argument with
87 .Dl "myprogram --myoption=somevalue"
89 When a long option is processed, the call to
92 For this reason, long option processing without
93 shortcuts is not backwards compatible with
96 It is possible to combine these methods, providing for long options
97 processing with short option equivalents for some options.
99 frequently used options would be processed as long options only.
103 call requires a structure to be initialized describing the long
106 .Bd -literal -offset indent
117 field should contain the option name without the leading double dash.
121 field should be one of:
123 .Bl -tag -width ".Dv optional_argument" -offset indent -compact
125 no argument to the option is expect
126 .It Dv required_argument
127 an argument to the option is required
128 .It Li optional_argument
129 an argument to the option may be presented.
136 then the integer pointed to by it will be set to the
146 field will be returned.
153 to the corresponding short option will make this function act just
157 .Bd -literal -compact
163 /* options descriptor */
164 static struct option longopts[] = {
165 { "buffy", no_argument, 0, 'b' },
166 { "floride", required_argument, 0, 'f' },
167 { "daggerset", no_argument, \*[Am]daggerset, 1 },
172 while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
178 if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
179 (void)fprintf(stderr,
180 "myname: %s: %s\en", optarg, strerror(errno));
186 fprintf(stderr,"Buffy will use her dagger to "
187 "apply floride to dracula's teeth\en");
197 .Sh IMPLEMENTATION DIFFERENCES
198 This section describes differences to the
201 found in glibc-2.1.3:
206 as first char of option string in presence of
208 .Ev POSIXLY_CORRECT :
209 .Bl -tag -width ".Nx"
213 and returns non-options as
214 arguments to option '\e1'.
218 and stops at the first non-option.
223 in options string in presence of
224 .Ev POSIXLY_CORRECT :
225 .Bl -tag -width ".Nx"
235 mean the preceding option takes an optional argument.
238 Return value in case of missing argument if first character
243 in option string is not
245 .Bl -tag -width ".Nx"
261 .Bl -tag -width ".Nx"
263 parses this as option
270 and returns \-1 (ignoring the
272 (Because the original
279 for long options with
283 .Bl -tag -width ".Nx"
294 would never be returned).
305 .Bl -tag -width ".Nx"
311 pointing past the argument of
320 .\" How should we treat W; in the option string when called via
321 .\" getopt? Ignore the ';' or treat it as a ':'? Issue a warning?
326 for long options without an argument that are
331 .Bl -tag -width ".Nx"
335 to the option name (the argument of
342 (the argument of the long option).
347 with an argument that is not (a prefix to) a known
351 .Bl -tag -width ".Nx"
357 set to the unknown option.
359 treats this as an error (unknown option) and returns
372 The error messages are different.
375 does not permute the argument vector at the same points in
376 the calling sequence as
379 The aspects normally used by
380 the caller (ordering after \-1 is returned, value of
383 to current positions) are the same, though.
384 (We do fewer variable swaps.)
391 function first appeared in
396 implementation appeared in 1.5.
398 The implementation can completely replace
400 but right now we are using separate code.