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.3464
1 files changed, 464 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..53737d8f25
--- /dev/null
+++ b/src/lib/libc/stdlib/getopt_long.3
@@ -0,0 +1,464 @@
1.\" $OpenBSD: getopt_long.3,v 1.16 2010/09/19 22:22:13 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 $Mdocdate: September 19 2010 $
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 *longindex"
49.Ft int
50.Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex"
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 -offset indent
82$ myprogram --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
96Abbreviated long option names are accepted when
97.Fn getopt_long
98processes long options if the abbreviation is unique.
99An exact match is always preferred for a defined long option.
100.Pp
101The
102.Fn getopt_long
103call requires a structure to be initialized describing the long
104options.
105The structure is:
106.Bd -literal -offset indent
107struct option {
108 char *name;
109 int has_arg;
110 int *flag;
111 int val;
112};
113.Ed
114.Pp
115The
116.Fa name
117field should contain the option name without the leading double dash.
118.Pp
119The
120.Fa has_arg
121field should be one of:
122.Pp
123.Bl -tag -width "optional_argument" -compact -offset indent
124.It Dv no_argument
125no argument to the option is expected.
126.It Dv required_argument
127an argument to the option is required.
128.It Dv optional_argument
129an argument to the option may be presented.
130.El
131.Pp
132If
133.Fa flag
134is not
135.Dv NULL ,
136then the integer pointed to by it will be set to the value in the
137.Fa val
138field.
139If the
140.Fa flag
141field is
142.Dv NULL ,
143then the
144.Fa val
145field will be returned.
146Setting
147.Fa flag
148to
149.Dv NULL
150and setting
151.Fa val
152to the corresponding short option will make this function act just
153like
154.Xr getopt 3 .
155.Pp
156If the
157.Fa longindex
158field is not
159.Dv NULL ,
160then the integer pointed to by it will be set to the index of the long
161option relative to
162.Fa longopts .
163.Pp
164The last element of the
165.Fa longopts
166array has to be filled with zeroes.
167.Pp
168The
169.Fn getopt_long_only
170function behaves identically to
171.Fn getopt_long
172with the exception that long options may start with
173.Sq -
174in addition to
175.Sq -- .
176If an option starting with
177.Sq -
178does not match a long option but does match a single-character option,
179the single-character option is returned.
180.Sh RETURN VALUES
181If the
182.Fa flag
183field in
184.Li struct option
185is
186.Dv NULL ,
187.Fn getopt_long
188and
189.Fn getopt_long_only
190return the value specified in the
191.Fa val
192field, which is usually just the corresponding short option.
193If
194.Fa flag
195is not
196.Dv NULL ,
197these functions return 0 and store
198.Fa val
199in the location pointed to by
200.Fa flag .
201These functions return
202.Sq \:
203if there was a missing option argument,
204.Sq \&?
205if the user specified an unknown or ambiguous option, and
206\-1 when the argument list has been exhausted.
207.Sh IMPLEMENTATION DIFFERENCES
208This section describes differences to the GNU implementation
209found in glibc-2.1.3:
210.Bl -bullet
211.It
212handling of
213.Ql -
214as the first character of the option string in the presence of the
215environment variable
216.Ev POSIXLY_CORRECT :
217.Bl -tag -width "OpenBSD"
218.It GNU
219ignores
220.Ev POSIXLY_CORRECT
221and returns non-options as arguments to option
222.Ql \e1 .
223.It OpenBSD
224honors
225.Ev POSIXLY_CORRECT
226and stops at the first non-option.
227.El
228.It
229handling of
230.Ql -
231within the option string (not the first character):
232.Bl -tag -width "OpenBSD"
233.It GNU
234treats a
235.Ql -
236on the command line as a non-argument.
237.It OpenBSD
238a
239.Ql -
240within the option string matches a
241.Ql -
242(single dash) on the command line.
243This functionality is provided for backward compatibility with
244programs, such as
245.Xr su 1 ,
246that use
247.Ql -
248as an option flag.
249This practice is wrong, and should not be used in any current development.
250.El
251.It
252handling of
253.Ql ::
254in the option string in the presence of
255.Ev POSIXLY_CORRECT :
256.Bl -tag -width "OpenBSD"
257.It Both
258GNU and
259.Ox
260ignore
261.Ev POSIXLY_CORRECT
262here and take
263.Ql ::
264to mean the preceding option takes an optional argument.
265.El
266.It
267return value in case of missing argument if first character
268(after
269.Ql +
270or
271.Ql - )
272in the option string is not
273.Ql \&: :
274.Bl -tag -width "OpenBSD"
275.It GNU
276returns
277.Ql \&?
278.It OpenBSD
279returns
280.Ql \&:
281(since
282.Ox Ns 's
283.Xr getopt 3
284does).
285.El
286.It
287handling of
288.Ql --a
289in
290.Xr getopt 3 :
291.Bl -tag -width "OpenBSD"
292.It GNU
293parses this as option
294.Ql - ,
295option
296.Ql a .
297.It OpenBSD
298parses this as
299.Ql -- ,
300and returns \-1 (ignoring the
301.Ql a )
302(because the original
303.Fn getopt
304did.)
305.El
306.It
307setting of
308.Va optopt
309for long options with
310.Va flag
311.No non- Ns Dv NULL :
312.Bl -tag -width "OpenBSD"
313.It GNU
314sets
315.Va optopt
316to
317.Va val .
318.It OpenBSD
319sets
320.Va optopt
321to 0 (since
322.Va val
323would never be returned).
324.El
325.It
326handling of
327.Ql -W
328with
329.Ql W;
330in the option string in
331.Xr getopt 3
332(not
333.Fn getopt_long ) :
334.Bl -tag -width "OpenBSD"
335.It GNU
336causes a segmentation fault.
337.It OpenBSD
338no special handling is done;
339.Ql W;
340is interpreted as two separate options, neither of which take an argument.
341.El
342.It
343setting of
344.Va optarg
345for long options without an argument that are invoked via
346.Ql -W
347(with
348.Ql W;
349in the option string):
350.Bl -tag -width "OpenBSD"
351.It GNU
352sets
353.Va optarg
354to the option name (the argument of
355.Ql -W ) .
356.It OpenBSD
357sets
358.Va optarg
359to
360.Dv NULL
361(the argument of the long option).
362.El
363.It
364handling of
365.Ql -W
366with an argument that is not (a prefix to) a known long option
367(with
368.Ql W;
369in the option string):
370.Bl -tag -width "OpenBSD"
371.It GNU
372returns
373.Ql -W
374with
375.Va optarg
376set to the unknown option.
377.It OpenBSD
378treats this as an error (unknown option) and returns
379.Ql \&?
380with
381.Va optopt
382set to 0 and
383.Va optarg
384set to
385.Dv NULL
386(as GNU's man page documents).
387.El
388.It
389The error messages are different.
390.It
391.Ox
392does not permute the argument vector at the same points in
393the calling sequence as GNU does.
394The aspects normally used by the caller
395(ordering after \-1 is returned, value of
396.Va optind
397relative to current positions) are the same, though.
398(We do fewer variable swaps.)
399.El
400.Sh ENVIRONMENT
401.Bl -tag -width Ev
402.It Ev POSIXLY_CORRECT
403If set, option processing stops when the first non-option is found and
404a leading
405.Sq -
406or
407.Sq +
408in the
409.Ar optstring
410is ignored.
411.El
412.Sh EXAMPLES
413.Bd -literal
414int bflag, ch, fd;
415int daggerset;
416
417/* options descriptor */
418static struct option longopts[] = {
419 { "buffy", no_argument, NULL, 'b' },
420 { "fluoride", required_argument, NULL, 'f' },
421 { "daggerset", no_argument, &daggerset, 1 },
422 { NULL, 0, NULL, 0 }
423};
424
425bflag = 0;
426while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
427 switch (ch) {
428 case 'b':
429 bflag = 1;
430 break;
431 case 'f':
432 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
433 err(1, "unable to open %s", optarg);
434 break;
435 case 0:
436 if (daggerset)
437 fprintf(stderr, "Buffy will use her dagger to "
438 "apply fluoride to dracula's teeth\en");
439 break;
440 default:
441 usage();
442 /* NOTREACHED */
443 }
444argc -= optind;
445argv += optind;
446.Ed
447.Sh SEE ALSO
448.Xr getopt 3
449.Sh HISTORY
450The
451.Fn getopt_long
452and
453.Fn getopt_long_only
454functions first appeared in GNU libiberty.
455This implementation first appeared in
456.Ox 3.3 .
457.Sh BUGS
458The
459.Ar argv
460argument is not really
461.Dv const
462as its elements may be permuted (unless
463.Ev POSIXLY_CORRECT
464is set).