]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/getopt_long.3
Libc-763.13.tar.gz
[apple/libc.git] / stdlib / FreeBSD / getopt_long.3
CommitLineData
3d9156a7
A
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 $
9385eb3d
A
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.
3d9156a7 15.\" 3. Neither the name of the University nor the names of its contributors
9385eb3d
A
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
1f2f436a 32.\" $FreeBSD: src/lib/libc/stdlib/getopt_long.3,v 1.13 2005/01/20 09:17:04 ru Exp $
9385eb3d
A
33.\"
34.Dd April 1, 2000
35.Dt GETOPT_LONG 3
36.Os
37.Sh NAME
3d9156a7
A
38.Nm getopt_long ,
39.Nm getopt_long_only
9385eb3d
A
40.Nd get long options from command line argument list
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In getopt.h
3d9156a7
A
45.Vt extern char *optarg ;
46.Vt extern int optind ;
47.Vt extern int optopt ;
48.Vt extern int opterr ;
49.Vt extern int optreset ;
9385eb3d
A
50.Ft int
51.Fo getopt_long
52.Fa "int argc" "char * const *argv" "const char *optstring"
3d9156a7
A
53.Fa "const struct option *longopts" "int *longindex"
54.Fc
55.Ft int
56.Fo getopt_long_only
57.Fa "int argc" "char * const *argv" "const char *optstring"
58.Fa "const struct option *longopts" "int *longindex"
9385eb3d
A
59.Fc
60.Sh DESCRIPTION
61The
62.Fn getopt_long
63function is similar to
64.Xr getopt 3
65but it accepts options in two forms: words and characters.
66The
67.Fn getopt_long
68function provides a superset of the functionality of
69.Xr getopt 3 .
70The
71.Fn getopt_long
72function
73can be used in two ways.
74In the first way, every long option understood
75by the program has a corresponding short option, and the option
76structure is only used to translate from long options to short
77options.
78When used in this fashion,
79.Fn getopt_long
80behaves identically to
81.Xr getopt 3 .
82This is a good way to add long option processing to an existing program
83with the minimum of rewriting.
84.Pp
85In the second mechanism, a long option sets a flag in the
86.Vt option
87structure passed, or will store a pointer to the command line argument
88in the
89.Vt option
90structure passed to it for options that take arguments.
91Additionally,
92the long option's argument may be specified as a single argument with
93an equal sign, e.g.,
94.Pp
95.Dl "myprogram --myoption=somevalue"
96.Pp
97When a long option is processed, the call to
98.Fn getopt_long
99will return 0.
100For this reason, long option processing without
101shortcuts is not backwards compatible with
102.Xr getopt 3 .
103.Pp
104It is possible to combine these methods, providing for long options
105processing with short option equivalents for some options.
106Less
107frequently used options would be processed as long options only.
108.Pp
109The
110.Fn getopt_long
111call requires a structure to be initialized describing the long
112options.
113The structure is:
114.Bd -literal -offset indent
115struct option {
116 char *name;
117 int has_arg;
118 int *flag;
119 int val;
120};
121.Ed
122.Pp
123The
124.Va name
125field should contain the option name without the leading double dash.
126.Pp
127The
128.Va has_arg
129field should be one of:
130.Pp
131.Bl -tag -width ".Dv optional_argument" -offset indent -compact
132.It Dv no_argument
133no argument to the option is expect
134.It Dv required_argument
135an argument to the option is required
1f2f436a 136.It Dv optional_argument
9385eb3d
A
137an argument to the option may be presented.
138.El
139.Pp
140If
141.Va flag
142is not
143.Dv NULL ,
144then the integer pointed to by it will be set to the
145value in the
146.Va val
147field.
148If the
149.Va flag
150field is
151.Dv NULL ,
152then the
153.Va val
154field will be returned.
155Setting
156.Va flag
157to
158.Dv NULL
159and setting
160.Va val
161to the corresponding short option will make this function act just
162like
163.Xr getopt 3 .
3d9156a7
A
164.Pp
165If the
166.Fa longindex
167field is not
168.Dv NULL ,
169then the integer pointed to by it will be set to the index of the long
170option relative to
171.Fa longopts .
172.Pp
173The last element of the
174.Fa longopts
175array has to be filled with zeroes.
176.Pp
177The
178.Fn getopt_long_only
179function behaves identically to
180.Fn getopt_long
181with the exception that long options may start with
182.Ql -
183in addition to
184.Ql -- .
185If an option starting with
186.Ql -
187does not match a long option but does match a single-character option,
188the single-character option is returned.
189.Sh RETURN VALUES
190If the
191.Fa flag
192field in
1f2f436a 193.Vt "struct option"
3d9156a7
A
194is
195.Dv NULL ,
196.Fn getopt_long
197and
198.Fn getopt_long_only
199return the value specified in the
200.Fa val
201field, which is usually just the corresponding short option.
202If
203.Fa flag
204is not
205.Dv NULL ,
206these functions return 0 and store
207.Fa val
208in the location pointed to by
209.Fa flag .
210These functions return
211.Ql \&:
212if there was a missing option argument,
213.Ql \&?
214if the user specified an unknown or ambiguous option, and
215\-1 when the argument list has been exhausted.
1f2f436a
A
216.Sh ENVIRONMENT
217.Bl -tag -width ".Ev POSIXLY_CORRECT"
218.It Ev POSIXLY_CORRECT
219If set, option processing stops when the first non-option is found and
220a leading
221.Ql -
222or
223.Ql +
224in the
225.Fa optstring
226is ignored.
227.El
9385eb3d
A
228.Sh EXAMPLES
229.Bd -literal -compact
9385eb3d
A
230int bflag, ch, fd;
231int daggerset;
232
233/* options descriptor */
234static struct option longopts[] = {
3d9156a7
A
235 { "buffy", no_argument, NULL, 'b' },
236 { "fluoride", required_argument, NULL, 'f' },
9385eb3d 237 { "daggerset", no_argument, \*[Am]daggerset, 1 },
3d9156a7 238 { NULL, 0, NULL, 0 }
9385eb3d
A
239};
240
241bflag = 0;
242while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
3d9156a7 243 switch (ch) {
9385eb3d
A
244 case 'b':
245 bflag = 1;
246 break;
247 case 'f':
3d9156a7
A
248 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
249 err(1, "unable to open %s", optarg);
9385eb3d
A
250 break;
251 case 0:
3d9156a7 252 if (daggerset) {
9385eb3d 253 fprintf(stderr,"Buffy will use her dagger to "
3d9156a7 254 "apply fluoride to dracula's teeth\en");
9385eb3d
A
255 }
256 break;
9385eb3d
A
257 default:
258 usage();
259}
260argc -= optind;
261argv += optind;
262.Ed
263.Sh IMPLEMENTATION DIFFERENCES
264This section describes differences to the
265.Tn GNU
266implementation
267found in glibc-2.1.3:
268.Bl -bullet
3d9156a7
A
269.\" .It
270.\" Handling of
271.\" .Ql -
272.\" as first char of option string in presence of
273.\" environment variable
274.\" .Ev POSIXLY_CORRECT :
275.\" .Bl -tag -width ".Bx"
276.\" .It Tn GNU
277.\" ignores
278.\" .Ev POSIXLY_CORRECT
279.\" and returns non-options as
280.\" arguments to option '\e1'.
281.\" .It Bx
282.\" honors
283.\" .Ev POSIXLY_CORRECT
284.\" and stops at the first non-option.
285.\" .El
286.\" .It
287.\" Handling of
288.\" .Ql -
289.\" within the option string (not the first character):
290.\" .Bl -tag -width ".Bx"
291.\" .It Tn GNU
292.\" treats a
293.\" .Ql -
294.\" on the command line as a non-argument.
295.\" .It Bx
296.\" a
297.\" .Ql -
298.\" within the option string matches a
299.\" .Ql -
300.\" (single dash) on the command line.
301.\" This functionality is provided for backward compatibility with
302.\" programs, such as
303.\" .Xr su 1 ,
304.\" that use
305.\" .Ql -
306.\" as an option flag.
307.\" This practice is wrong, and should not be used in any current development.
308.\" .El
309.\" .It
310.\" Handling of
311.\" .Ql ::
312.\" in options string in presence of
313.\" .Ev POSIXLY_CORRECT :
314.\" .Bl -tag -width ".Bx"
315.\" .It Both
316.\" .Tn GNU
317.\" and
318.\" .Bx
319.\" ignore
320.\" .Ev POSIXLY_CORRECT
321.\" here and take
322.\" .Ql ::
323.\" to
324.\" mean the preceding option takes an optional argument.
325.\" .El
326.\" .It
327.\" Return value in case of missing argument if first character
328.\" (after
329.\" .Ql +
330.\" or
331.\" .Ql - )
332.\" in option string is not
333.\" .Ql \&: :
334.\" .Bl -tag -width ".Bx"
335.\" .It Tn GNU
336.\" returns
337.\" .Ql \&?
338.\" .It Bx
339.\" returns
340.\" .Ql \&:
341.\" (since
342.\" .Bx Ns 's
343.\" .Fn getopt
344.\" does).
345.\" .El
346.\" .It
347.\" Handling of
348.\" .Ql --a
349.\" in getopt:
350.\" .Bl -tag -width ".Bx"
351.\" .It Tn GNU
352.\" parses this as option
353.\" .Ql - ,
354.\" option
355.\" .Ql a .
356.\" .It Bx
357.\" parses this as
358.\" .Ql -- ,
359.\" and returns \-1 (ignoring the
360.\" .Ql a ) .
361.\" (Because the original
362.\" .Fn getopt
363.\" does.)
364.\" .El
9385eb3d
A
365.It
366Setting of
367.Va optopt
368for long options with
369.Va flag
370!=
371.Dv NULL :
3d9156a7 372.Bl -tag -width ".Bx"
9385eb3d
A
373.It Tn GNU
374sets
375.Va optopt
376to
377.Va val .
3d9156a7 378.It Bx
9385eb3d
A
379sets
380.Va optopt
381to 0 (since
382.Va val
383would never be returned).
384.El
3d9156a7
A
385.\" .It
386.\" Handling of
387.\" .Ql -W
388.\" with
389.\" .Ql W;
390.\" in option string in
391.\" .Fn getopt
392.\" (not
393.\" .Fn getopt_long ) :
394.\" .Bl -tag -width ".Bx"
395.\" .It Tn GNU
396.\" causes a segfault.
397.\" .It Bx
398.\" no special handling is done;
399.\" .Ql W;
400.\" is interpreted as two separate options, neither of which take an argument.
401.\" .El
9385eb3d
A
402.It
403Setting of
404.Va optarg
405for long options without an argument that are
406invoked via
407.Ql -W
3d9156a7 408.Ql ( W;
9385eb3d 409in option string):
3d9156a7 410.Bl -tag -width ".Bx"
9385eb3d
A
411.It Tn GNU
412sets
413.Va optarg
414to the option name (the argument of
415.Ql -W ) .
3d9156a7 416.It Bx
9385eb3d
A
417sets
418.Va optarg
419to
420.Dv NULL
421(the argument of the long option).
422.El
423.It
424Handling of
425.Ql -W
426with an argument that is not (a prefix to) a known
427long option
3d9156a7 428.Ql ( W;
9385eb3d 429in option string):
3d9156a7 430.Bl -tag -width ".Bx"
9385eb3d
A
431.It Tn GNU
432returns
433.Ql -W
434with
435.Va optarg
436set to the unknown option.
3d9156a7 437.It Bx
9385eb3d
A
438treats this as an error (unknown option) and returns
439.Ql \&?
440with
441.Va optopt
442set to 0 and
443.Va optarg
444set to
445.Dv NULL
446(as
447.Tn GNU Ns 's
448man page documents).
449.El
3d9156a7
A
450.\" .It
451.\" The error messages are different.
9385eb3d 452.It
3d9156a7 453.Bx
9385eb3d
A
454does not permute the argument vector at the same points in
455the calling sequence as
456.Tn GNU
457does.
458The aspects normally used by
459the caller (ordering after \-1 is returned, value of
460.Va optind
461relative
462to current positions) are the same, though.
463(We do fewer variable swaps.)
464.El
465.Sh SEE ALSO
466.Xr getopt 3
467.Sh HISTORY
468The
469.Fn getopt_long
3d9156a7
A
470and
471.Fn getopt_long_only
472functions first appeared in
9385eb3d
A
473.Tn GNU
474libiberty.
475The first
3d9156a7
A
476.Bx
477implementation of
478.Fn getopt_long
479appeared in
480.Nx 1.5 ,
481the first
482.Bx
483implementation of
484.Fn getopt_long_only
485in
486.Ox 3.3 .
487.Fx
488first included
489.Fn getopt_long
490in
491.Fx 5.0 ,
492.Fn getopt_long_only
493in
494.Fx 5.2 .
9385eb3d 495.Sh BUGS
3d9156a7 496The
1f2f436a 497.Fa argv
3d9156a7 498argument is not really
1f2f436a 499.Vt const
3d9156a7
A
500as its elements may be permuted (unless
501.Ev POSIXLY_CORRECT
502is set).
503.Pp
9385eb3d
A
504The implementation can completely replace
505.Xr getopt 3 ,
506but right now we are using separate code.