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.3366
1 files changed, 366 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..6226dfe99f
--- /dev/null
+++ b/src/lib/libc/stdlib/getopt_long.3
@@ -0,0 +1,366 @@
1.\" $OpenBSD: getopt_long.3,v 1.9 2003/09/02 18:24:21 jmc 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. 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.
18.\"
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
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
32.\"
33.Dd April 1, 2000
34.Dt GETOPT_LONG 3
35.Os
36.Sh NAME
37.Nm getopt_long ,
38.Nm getopt_long_only
39.Nd get long options from command line argument list
40.Sh SYNOPSIS
41.Fd #include <getopt.h>
42.Vt extern char *optarg;
43.Vt extern int optind;
44.Vt extern int optopt;
45.Vt extern int opterr;
46.Vt extern int optreset;
47.Ft int
48.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index"
49.Ft int
50.Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index"
51.Sh DESCRIPTION
52The
53.Fn getopt_long
54function is similar to
55.Xr getopt 3
56but it accepts options in two forms: words and characters.
57The
58.Fn getopt_long
59function provides a superset of the functionality of
60.Xr getopt 3 .
61.Fn getopt_long
62can be used in two ways.
63In the first way, every long option understood by the program has a
64corresponding short option, and the option structure is only used to
65translate from long options to short options.
66When used in this fashion,
67.Fn getopt_long
68behaves identically to
69.Xr getopt 3 .
70This is a good way to add long option processing to an existing program
71with the minimum of rewriting.
72.Pp
73In the second mechanism, a long option sets a flag in the
74.Fa option
75structure passed, or will store a pointer to the command line argument
76in the
77.Fa option
78structure passed to it for options that take arguments.
79Additionally, the long option's argument may be specified as a single
80argument with an equal sign, e.g.
81.Bd -literal
82myprogram --myoption=somevalue
83.Ed
84.Pp
85When a long option is processed the call to
86.Fn getopt_long
87will return 0.
88For this reason, long option processing without
89shortcuts is not backwards compatible with
90.Xr getopt 3 .
91.Pp
92It is possible to combine these methods, providing for long options
93processing with short option equivalents for some options.
94Less frequently used options would be processed as long options only.
95.Pp
96The
97.Fn getopt_long
98call requires a structure to be initialized describing the long
99options.
100The structure is:
101.Bd -literal
102struct option {
103 char *name;
104 int has_arg;
105 int *flag;
106 int val;
107};
108.Ed
109.Pp
110The
111.Fa name
112field should contain the option name without the leading double dash.
113.Pp
114The
115.Fa has_arg
116field should be one of:
117.Bl -tag -width "optional_argument"
118.It Li no_argument
119no argument to the option is expect.
120.It Li required_argument
121an argument to the option is required.
122.It Li optional_argument
123an argument to the option may be presented.
124.El
125.Pp
126If
127.Fa flag
128is not
129.Dv NULL ,
130then the integer pointed to by it will be set to the value in the
131.Fa val
132field.
133If the
134.Fa flag
135field is
136.Dv NULL ,
137then the
138.Fa val
139field will be returned.
140Setting
141.Fa flag
142to
143.Dv NULL
144and setting
145.Fa val
146to the corresponding short option will make this function act just
147like
148.Xr getopt 3 .
149.Pp
150The
151.Fn getopt_long_only
152function behaves identically to
153.Fn getopt_long
154with the exception that long options may start with
155.Sq -
156in addition to
157.Sq -- .
158If an option starting with
159.Sq -
160does not match a long option but does match a single-character option,
161the single-character option is returned.
162.Sh RETURN VALUES
163If the
164.Fa flag
165field in
166.Li struct option
167is
168.Dv NULL ,
169.Fn getopt_long
170and
171.Fn getopt_long_only
172return the value specified in the
173.Fa val
174field, which is usually just the corresponding short option.
175If
176.Fa flag
177is not
178.Dv NULL ,
179these functions return 0 and store
180.Fa val
181in the location pointed to by
182.Fa flag .
183These functions return
184.Sq \:
185if there was a missing option argument,
186.Sq \&?
187if the user specified an unknown or ambiguous option, and
188\-1 when the argument list has been exhausted.
189.Sh EXAMPLES
190.Bd -literal -compact
191int bflag, ch, fd;
192int daggerset;
193
194/* options descriptor */
195static struct option longopts[] = {
196 { "buffy", no_argument, 0, 'b' },
197 { "fluoride", required_argument, 0, 'f' },
198 { "daggerset", no_argument, &daggerset, 1 },
199 { 0, 0, 0, 0 }
200};
201
202bflag = 0;
203while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
204 switch(ch) {
205 case 'b':
206 bflag = 1;
207 break;
208 case 'f':
209 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
210 err(1, "unable to open %s", optarg);
211 break;
212 case 0:
213 if (daggerset) {
214 fprintf(stderr,"Buffy will use her dagger to "
215 "apply fluoride to dracula's teeth\en");
216 }
217 break;
218 case '?':
219 default:
220 usage();
221}
222argc -= optind;
223argv += optind;
224.Ed
225.Sh IMPLEMENTATION DIFFERENCES
226This section describes differences to the GNU implementation
227found in glibc-2.1.3:
228.Bl -tag -width "xxx"
229.It Li o
230handling of - as first char of option string in presence of
231environment variable POSIXLY_CORRECT:
232.Bl -tag -width "OpenBSD"
233.It Li GNU
234ignores POSIXLY_CORRECT and returns non-options as
235arguments to option '\e1'.
236.It Li OpenBSD
237honors POSIXLY_CORRECT and stops at the first non-option.
238.El
239.It Li o
240handling of - within the option string (not the first character):
241.Bl -tag -width "OpenBSD"
242.It Li GNU
243treats a
244.Ql -
245on the command line as a non-argument.
246.It Li OpenBSD
247a
248.Ql -
249within the option string matches a
250.Ql -
251(single dash) on the command line.
252This functionality is provided for backward compatibility with
253programs, such as
254.Xr su 1 ,
255that use
256.Ql -
257as an option flag.
258This practice is wrong, and should not be used in any current development.
259.El
260.It Li o
261handling of :: in options string in presence of POSIXLY_CORRECT:
262.Bl -tag -width "OpenBSD"
263.It Li Both
264GNU and OpenBSD ignore POSIXLY_CORRECT here and take :: to
265mean the preceding option takes an optional argument.
266.El
267.It Li o
268return value in case of missing argument if first character
269(after + or -) in option string is not ':':
270.Bl -tag -width "OpenBSD"
271.It Li GNU
272returns '?'
273.It OpenBSD
274returns ':' (since OpenBSD's getopt does).
275.El
276.It Li o
277handling of --a in getopt:
278.Bl -tag -width "OpenBSD"
279.It Li GNU
280parses this as option '-', option 'a'.
281.It Li OpenBSD
282parses this as '--', and returns \-1 (ignoring the a).
283(Because the original getopt does.)
284.El
285.It Li o
286setting of optopt for long options with flag !=
287.Dv NULL :
288.Bl -tag -width "OpenBSD"
289.It Li GNU
290sets optopt to val.
291.It Li OpenBSD
292sets optopt to 0 (since val would never be returned).
293.El
294.It Li o
295handling of -W with W; in option string in getopt (not getopt_long):
296.Bl -tag -width "OpenBSD"
297.It Li GNU
298causes a segfault.
299.It Li OpenBSD
300no special handling is done;
301.Dq W;
302is interpreted as two separate options, neither of which take an argument.
303.El
304.It Li o
305setting of optarg for long options without an argument that are
306invoked via -W (W; in option string):
307.Bl -tag -width "OpenBSD"
308.It Li GNU
309sets optarg to the option name (the argument of -W).
310.It Li OpenBSD
311sets optarg to
312.Dv NULL
313(the argument of the long option).
314.El
315.It Li o
316handling of -W with an argument that is not (a prefix to) a known
317long option (W; in option string):
318.Bl -tag -width "OpenBSD"
319.It Li GNU
320returns -W with optarg set to the unknown option.
321.It Li OpenBSD
322treats this as an error (unknown option) and returns '?' with
323optopt set to 0 and optarg set to
324.Dv NULL
325(as GNU's man page documents).
326.El
327.It Li o
328The error messages are different.
329.It Li o
330OpenBSD does not permute the argument vector at the same points in
331the calling sequence as GNU does.
332The aspects normally used by the caller
333(ordering after \-1 is returned, value of optind relative
334to current positions) are the same, though.
335(We do fewer variable swaps.)
336.El
337.Sh ENVIRONMENT
338.Bl -tag -width POSIXLY_CORRECT
339.It Ev POSIXLY_CORRECT
340If set, option processing stops when the first non-option is found and
341a leading
342.Sq -
343or
344.Sq +
345in the
346.Ar optstring
347is ignored.
348.El
349.Sh SEE ALSO
350.Xr getopt 3
351.Sh HISTORY
352The
353.Fn getopt_long
354and
355.Fn getopt_long_only
356functions first appeared in GNU libiberty.
357This implementation first appeared in
358.Ox 3.3 .
359.Sh BUGS
360The
361.Ar argv
362argument is not really
363.Dv const
364as its elements may be permuted (unless
365.Ev POSIXLY_CORRECT
366is set).