summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/getopt_long.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/getopt_long.3')
-rw-r--r--src/lib/libc/stdlib/getopt_long.3326
1 files changed, 326 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3
new file mode 100644
index 0000000000..d55f4ead53
--- /dev/null
+++ b/src/lib/libc/stdlib/getopt_long.3
@@ -0,0 +1,326 @@
1.\" $OpenBSD: getopt_long.3,v 1.1 2002/12/03 20:24:30 millert Exp $
2.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $
3.\"
4.\" Copyright (c) 1988, 1991, 1993
5.\" The Regents of the University of California. All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
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. All advertising materials mentioning features or use of this software
16.\" must display the following acknowledgement:
17.\" This product includes software developed by the University of
18.\" California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\" may be used to endorse or promote products derived from this software
21.\" without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
36.\"
37.Dd April 1, 2000
38.Dt GETOPT_LONG 3
39.Os
40.Sh NAME
41.Nm getopt_long ,
42.Nm getopt_long_only
43.Nd get long options from command line argument list
44.Sh SYNOPSIS
45.Fd #include <getopt.h>
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;
51.Ft int
52.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index"
53.Ft int
54.Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index"
55.Sh DESCRIPTION
56The
57.Fn getopt_long
58function is similar to
59.Xr getopt 3
60but it accepts options in two forms: words and characters.
61The
62.Fn getopt_long
63function provides a superset of of the functionality of
64.Xr getopt 3 .
65.Fn getopt_long
66can be used in two ways.
67In the first way, every long option understood by the program has a
68corresponding short option, and the option structure is only used to
69translate from long options to short options.
70When used in this fashion,
71.Fn getopt_long
72behaves identically to
73.Xr getopt 3 .
74This is a good way to add long option processing to an existing program
75with the minimum of rewriting.
76.Pp
77In the second mechanism, a long option sets a flag in the
78.Fa option
79structure passed, or will store a pointer to the command line argument
80in the
81.Fa option
82structure passed to it for options that take arguments.
83Additionally, the long option's argument may be specified as a single
84argument with an equal sign, e.g.
85.Bd -literal
86myprogram --myoption=somevalue
87.Ed
88.Pp
89When a long option is processed the call to
90.Fn getopt_long
91will return 0.
92For this reason, long option processing without
93shortcuts is not backwards compatible with
94.Xr getopt 3 .
95.Pp
96It is possible to combine these methods, providing for long options
97processing with short option equivalents for some options.
98Less frequently used options would be processed as long options only.
99.Pp
100The
101.Fn getopt_long
102call requires a structure to be initialized describing the long
103options.
104The structure is:
105.Bd -literal
106struct option {
107 char *name;
108 int has_arg;
109 int *flag;
110 int val;
111};
112.Ed
113.Pp
114The
115.Fa name
116field should contain the option name without the leading double dash.
117.Pp
118The
119.Fa has_arg
120field should be one of:
121.Bl -tag -width "optional_argument"
122.It Li no_argument
123no argument to the option is expect.
124.It Li required_argument
125an argument to the option is required.
126.It Li optional_argument
127an argument to the option may be presented.
128.El
129.Pp
130If
131.Fa flag
132is not
133.Dv NULL ,
134then the integer pointed to by it will be set to the value in the
135.Fa val
136field.
137If the
138.Fa flag
139field is
140.Dv NULL ,
141then the
142.Fa val
143field will be returned.
144Setting
145.Fa flag
146to
147.Dv NULL
148and setting
149.Fa val
150to the corresponding short option will make this function act just
151like
152.Xr getopt 3 .
153.Pp
154The
155.Fn getopt_long_only
156function behaves identically to
157.Fn getopt_long
158with the exception that long options may start with
159.Sq -
160in addition to
161.Sq -- .
162If an option starting with
163.Sq -
164does not match a long option but does match a single-character option,
165the single-character option is returned.
166.Sh EXAMPLES
167.Bd -literal -compact
168int bflag, ch, fd;
169int daggerset;
170
171/* options descriptor */
172static struct option longopts[] = {
173 { "buffy", no_argument, 0, 'b' },
174 { "fluoride", required_argument, 0, 'f' },
175 { "daggerset", no_argument, &daggerset, 1 },
176 { 0, 0, 0, 0 }
177};
178
179bflag = 0;
180while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
181 switch(ch) {
182 case 'b':
183 bflag = 1;
184 break;
185 case 'f':
186 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
187 err(1, "unable to open %s", optarg);
188 break;
189 case 0:
190 if (daggerset) {
191 fprintf(stderr,"Buffy will use her dagger to "
192 "apply fluoride to dracula's teeth\en");
193 }
194 break;
195 case '?':
196 default:
197 usage();
198}
199argc -= optind;
200argv += optind;
201.Ed
202.Sh IMPLEMENTATION DIFFERENCES
203This section describes differences to the GNU implementation
204found in glibc-2.1.3:
205.Bl -tag -width "xxx"
206.It Li o
207handling of - as first char of option string in presence of
208environment variable POSIXLY_CORRECT:
209.Bl -tag -width "OpenBSD"
210.It Li GNU
211ignores POSIXLY_CORRECT and returns non-options as
212arguments to option '\e1'.
213.It Li OpenBSD
214honors POSIXLY_CORRECT and stops at the first non-option.
215.El
216.It Li o
217handling of :: in options string in presence of POSIXLY_CORRECT:
218.Bl -tag -width "OpenBSD"
219.It Li Both
220GNU and OpenBSD ignore POSIXLY_CORRECT here and take :: to
221mean the preceding option takes an optional argument.
222.El
223.It Li o
224return value in case of missing argument if first character
225(after + or -) in option string is not ':':
226.Bl -tag -width "OpenBSD"
227.It Li GNU
228returns '?'
229.It OpenBSD
230returns ':' (since OpenBSD's getopt does).
231.El
232.It Li o
233handling of --a in getopt:
234.Bl -tag -width "OpenBSD"
235.It Li GNU
236parses this as option '-', option 'a'.
237.It Li OpenBSD
238parses this as '--', and returns -1 (ignoring the a). (Because
239the original getopt does.)
240.El
241.It Li o
242setting of optopt for long options with flag !=
243.Dv NULL :
244.Bl -tag -width "OpenBSD"
245.It Li GNU
246sets optopt to val.
247.It Li OpenBSD
248sets optopt to 0 (since val would never be returned).
249.El
250.It Li o
251handling of -W with W; in option string in getopt (not getopt_long):
252.Bl -tag -width "OpenBSD"
253.It Li GNU
254causes a segfault.
255.It Li OpenBSD
256returns \-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?
260.El
261.It Li o
262setting of optarg for long options without an argument that are
263invoked via -W (W; in option string):
264.Bl -tag -width "OpenBSD"
265.It Li GNU
266sets optarg to the option name (the argument of -W).
267.It Li OpenBSD
268sets optarg to
269.Dv NULL
270(the argument of the long option).
271.El
272.It Li o
273handling of -W with an argument that is not (a prefix to) a known
274long option (W; in option string):
275.Bl -tag -width "OpenBSD"
276.It Li GNU
277returns -W with optarg set to the unknown option.
278.It Li OpenBSD
279treats this as an error (unknown option) and returns '?' with
280optopt set to 0 and optarg set to
281.Dv NULL
282(as GNU's man page documents).
283.El
284.It Li o
285The error messages are different.
286.It Li o
287OpenBSD does not permute the argument vector at the same points in
288the calling sequence as GNU does.
289The aspects normally used by the caller
290(ordering after \-1 is returned, value of optind relative
291to current positions) are the same, though.
292(We do fewer variable swaps.)
293.El
294.Sh ENVIRONMENT
295.Bl -tag -width POSIXLY_CORRECT
296.It Ev POSIXLY_CORRECT
297If set, option processing stops when the first non-option is found and
298a leading
299.Sq -
300or
301.Sq +
302in the
303.Ar optstring
304is ignored.
305.El
306.Sh SEE ALSO
307.Xr getopt 3
308.Sh HISTORY
309The
310.Fn getopt_long
311and
312.Fn getopt_long_only
313functions first appeared in GNU libiberty.
314This implementation first appeared in
315.Ox 3.3 .
316.Sh BUGS
317The
318.Ar argv
319argument is not really
320.Dv const
321as its elements may be permuted (unless
322.Ev POSIXLY_CORRECT
323is set).
324.Pp
325In a future release, this implementation should completely replace
326.Xr getopt 3 .