xref: /minix3/lib/libc/stdlib/getopt_long.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\"	$NetBSD: getopt_long.3,v 1.18 2007/07/02 17:56:17 ginsbach Exp $
2*2fe8fb19SBen Gras.\"
3*2fe8fb19SBen Gras.\" Copyright (c) 1988, 1991, 1993
4*2fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras.\" are met:
9*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
15*2fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
16*2fe8fb19SBen Gras.\"    without specific prior written permission.
17*2fe8fb19SBen Gras.\"
18*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*2fe8fb19SBen Gras.\" SUCH DAMAGE.
29*2fe8fb19SBen Gras.\"
30*2fe8fb19SBen Gras.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
31*2fe8fb19SBen Gras.\"
32*2fe8fb19SBen Gras.Dd July 2, 2007
33*2fe8fb19SBen Gras.Dt GETOPT_LONG 3
34*2fe8fb19SBen Gras.Os
35*2fe8fb19SBen Gras.Sh NAME
36*2fe8fb19SBen Gras.Nm getopt_long
37*2fe8fb19SBen Gras.Nd get long options from command line argument list
38*2fe8fb19SBen Gras.Sh LIBRARY
39*2fe8fb19SBen Gras.Lb libc
40*2fe8fb19SBen Gras.Sh SYNOPSIS
41*2fe8fb19SBen Gras.In getopt.h
42*2fe8fb19SBen Gras.Ft int
43*2fe8fb19SBen Gras.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long_options" "int *index"
44*2fe8fb19SBen Gras.Sh DESCRIPTION
45*2fe8fb19SBen GrasThe
46*2fe8fb19SBen Gras.Fn getopt_long
47*2fe8fb19SBen Grasfunction is similar to
48*2fe8fb19SBen Gras.Xr getopt 3
49*2fe8fb19SBen Grasbut it accepts options in two forms: words and characters.
50*2fe8fb19SBen GrasThe
51*2fe8fb19SBen Gras.Fn getopt_long
52*2fe8fb19SBen Grasfunction provides a superset of the functionality of
53*2fe8fb19SBen Gras.Xr getopt 3 .
54*2fe8fb19SBen Gras.Fn getopt_long
55*2fe8fb19SBen Grascan be used in two ways.
56*2fe8fb19SBen GrasIn the first way, every long option understood by the program has a
57*2fe8fb19SBen Grascorresponding short option, and the option structure is only used to
58*2fe8fb19SBen Grastranslate from long options to short options.
59*2fe8fb19SBen GrasWhen used in this fashion,
60*2fe8fb19SBen Gras.Fn getopt_long
61*2fe8fb19SBen Grasbehaves identically to
62*2fe8fb19SBen Gras.Xr getopt 3 .
63*2fe8fb19SBen GrasThis is a good way to add long option processing to an existing program
64*2fe8fb19SBen Graswith the minimum of rewriting.
65*2fe8fb19SBen Gras.Pp
66*2fe8fb19SBen GrasIn the second mechanism, a long option sets a flag in the
67*2fe8fb19SBen Gras.Fa option
68*2fe8fb19SBen Grasstructure passed, or will store a pointer to the command line argument
69*2fe8fb19SBen Grasin the
70*2fe8fb19SBen Gras.Fa option
71*2fe8fb19SBen Grasstructure passed to it for options that take arguments.
72*2fe8fb19SBen GrasAdditionally, the long option's argument may be specified as a single
73*2fe8fb19SBen Grasargument with an equal sign, e.g.
74*2fe8fb19SBen Gras.Bd -literal
75*2fe8fb19SBen Grasmyprogram --myoption=somevalue
76*2fe8fb19SBen Gras.Ed
77*2fe8fb19SBen Gras.Pp
78*2fe8fb19SBen GrasWhen a long option is processed the call to
79*2fe8fb19SBen Gras.Fn getopt_long
80*2fe8fb19SBen Graswill return 0.
81*2fe8fb19SBen GrasFor this reason, long option processing without
82*2fe8fb19SBen Grasshortcuts is not backwards compatible with
83*2fe8fb19SBen Gras.Xr getopt 3 .
84*2fe8fb19SBen Gras.Pp
85*2fe8fb19SBen GrasIt is possible to combine these methods, providing for long options
86*2fe8fb19SBen Grasprocessing with short option equivalents for some options.
87*2fe8fb19SBen GrasLess frequently used options would be processed as long options only.
88*2fe8fb19SBen Gras.Pp
89*2fe8fb19SBen GrasAbbreviated long option names are accepted when
90*2fe8fb19SBen Gras.Fn getopt_long
91*2fe8fb19SBen Grasprocesses long options if the abbreviation is unique.
92*2fe8fb19SBen GrasAn exact match is always preferred for a defined long option.
93*2fe8fb19SBen Gras.Pp
94*2fe8fb19SBen GrasThe
95*2fe8fb19SBen Gras.Fn getopt_long
96*2fe8fb19SBen Grascall requires a structure to be initialized describing the long options.
97*2fe8fb19SBen GrasThe structure is:
98*2fe8fb19SBen Gras.Bd -literal
99*2fe8fb19SBen Grasstruct option {
100*2fe8fb19SBen Gras	char *name;
101*2fe8fb19SBen Gras	int has_arg;
102*2fe8fb19SBen Gras	int *flag;
103*2fe8fb19SBen Gras	int val;
104*2fe8fb19SBen Gras};
105*2fe8fb19SBen Gras.Ed
106*2fe8fb19SBen Gras.Pp
107*2fe8fb19SBen GrasThe
108*2fe8fb19SBen Gras.Fa name
109*2fe8fb19SBen Grasfield should contain the option name without the leading double dash.
110*2fe8fb19SBen Gras.Pp
111*2fe8fb19SBen GrasThe
112*2fe8fb19SBen Gras.Fa has_arg
113*2fe8fb19SBen Grasfield should be one of:
114*2fe8fb19SBen Gras.Bl -tag -width "optional_argument"
115*2fe8fb19SBen Gras.It Li no_argument
116*2fe8fb19SBen Grasno argument to the option is expect.
117*2fe8fb19SBen Gras.It Li required_argument
118*2fe8fb19SBen Grasan argument to the option is required.
119*2fe8fb19SBen Gras.It Li optional_argument
120*2fe8fb19SBen Grasan argument to the option may be presented.
121*2fe8fb19SBen Gras.El
122*2fe8fb19SBen Gras.Pp
123*2fe8fb19SBen GrasIf
124*2fe8fb19SBen Gras.Fa flag
125*2fe8fb19SBen Grasis not
126*2fe8fb19SBen Gras.Dv NULL ,
127*2fe8fb19SBen Grasthen the integer pointed to by it will be set to the value in the
128*2fe8fb19SBen Gras.Fa val
129*2fe8fb19SBen Grasfield.
130*2fe8fb19SBen GrasIf the
131*2fe8fb19SBen Gras.Fa flag
132*2fe8fb19SBen Grasfield is
133*2fe8fb19SBen Gras.Dv NULL ,
134*2fe8fb19SBen Grasthen the
135*2fe8fb19SBen Gras.Fa val
136*2fe8fb19SBen Grasfield will be returned.
137*2fe8fb19SBen GrasSetting
138*2fe8fb19SBen Gras.Fa flag
139*2fe8fb19SBen Grasto
140*2fe8fb19SBen Gras.Dv NULL
141*2fe8fb19SBen Grasand setting
142*2fe8fb19SBen Gras.Fa val
143*2fe8fb19SBen Grasto the corresponding short option will make this function act just
144*2fe8fb19SBen Graslike
145*2fe8fb19SBen Gras.Xr getopt 3 .
146*2fe8fb19SBen Gras.Pp
147*2fe8fb19SBen GrasIf the
148*2fe8fb19SBen Gras.Fa index
149*2fe8fb19SBen Grasfield is not
150*2fe8fb19SBen Gras.Dv NULL ,
151*2fe8fb19SBen Grasthe integer it points to will be set to the index of the long option
152*2fe8fb19SBen Grasin the
153*2fe8fb19SBen Gras.Fa long_options
154*2fe8fb19SBen Grasarray.
155*2fe8fb19SBen Gras.Pp
156*2fe8fb19SBen GrasThe last element of the
157*2fe8fb19SBen Gras.Fa long_options
158*2fe8fb19SBen Grasarray has to be filled with zeroes (see
159*2fe8fb19SBen Gras.Sx EXAMPLES
160*2fe8fb19SBen Grassection).
161*2fe8fb19SBen Gras.Sh EXAMPLES
162*2fe8fb19SBen Gras.Bd -literal -compact
163*2fe8fb19SBen Grasextern char *optarg;
164*2fe8fb19SBen Grasextern int optind;
165*2fe8fb19SBen Grasint bflag, ch, fd;
166*2fe8fb19SBen Grasint daggerset;
167*2fe8fb19SBen Gras
168*2fe8fb19SBen Gras/* options descriptor */
169*2fe8fb19SBen Grasstatic struct option longopts[] = {
170*2fe8fb19SBen Gras	{ "buffy",	no_argument,		0, 		'b' },
171*2fe8fb19SBen Gras	{ "fluoride",	required_argument,	0, 	       	'f' },
172*2fe8fb19SBen Gras	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
173*2fe8fb19SBen Gras	{ NULL,		0,			NULL, 		0 }
174*2fe8fb19SBen Gras};
175*2fe8fb19SBen Gras
176*2fe8fb19SBen Grasbflag = 0;
177*2fe8fb19SBen Graswhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
178*2fe8fb19SBen Gras	switch (ch) {
179*2fe8fb19SBen Gras	case 'b':
180*2fe8fb19SBen Gras		bflag = 1;
181*2fe8fb19SBen Gras		break;
182*2fe8fb19SBen Gras	case 'f':
183*2fe8fb19SBen Gras		if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
184*2fe8fb19SBen Gras			(void)fprintf(stderr,
185*2fe8fb19SBen Gras			    "myname: %s: %s\en", optarg, strerror(errno));
186*2fe8fb19SBen Gras			exit(1);
187*2fe8fb19SBen Gras		}
188*2fe8fb19SBen Gras		break;
189*2fe8fb19SBen Gras	case 0:
190*2fe8fb19SBen Gras		if(daggerset) {
191*2fe8fb19SBen Gras			fprintf(stderr,"Buffy will use her dagger to "
192*2fe8fb19SBen Gras				       "apply fluoride to dracula's teeth\en");
193*2fe8fb19SBen Gras		}
194*2fe8fb19SBen Gras		break;
195*2fe8fb19SBen Gras	case '?':
196*2fe8fb19SBen Gras	default:
197*2fe8fb19SBen Gras		usage();
198*2fe8fb19SBen Gras}
199*2fe8fb19SBen Grasargc -= optind;
200*2fe8fb19SBen Grasargv += optind;
201*2fe8fb19SBen Gras.Ed
202*2fe8fb19SBen Gras.Sh IMPLEMENTATION DIFFERENCES
203*2fe8fb19SBen GrasThis section describes differences to the GNU implementation
204*2fe8fb19SBen Grasfound in glibc-2.1.3:
205*2fe8fb19SBen Gras.Bl -tag -width "xxx"
206*2fe8fb19SBen Gras.It Li o
207*2fe8fb19SBen Grashandling of - as first char of option string in presence of
208*2fe8fb19SBen Grasenvironment variable POSIXLY_CORRECT:
209*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
210*2fe8fb19SBen Gras.It Li GNU
211*2fe8fb19SBen Grasignores POSIXLY_CORRECT and returns non-options as
212*2fe8fb19SBen Grasarguments to option '\e1'.
213*2fe8fb19SBen Gras.It Li NetBSD
214*2fe8fb19SBen Grashonors POSIXLY_CORRECT and stops at the first non-option.
215*2fe8fb19SBen Gras.El
216*2fe8fb19SBen Gras.It Li o
217*2fe8fb19SBen Grashandling of :: in options string in presence of POSIXLY_CORRECT:
218*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
219*2fe8fb19SBen Gras.It Li Both
220*2fe8fb19SBen GrasGNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
221*2fe8fb19SBen Grasmean the preceding option takes an optional argument.
222*2fe8fb19SBen Gras.El
223*2fe8fb19SBen Gras.It Li o
224*2fe8fb19SBen Grasreturn value in case of missing argument if first character
225*2fe8fb19SBen Gras(after + or -) in option string is not ':':
226*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
227*2fe8fb19SBen Gras.It Li GNU
228*2fe8fb19SBen Grasreturns '?'
229*2fe8fb19SBen Gras.It NetBSD
230*2fe8fb19SBen Grasreturns ':' (since NetBSD's getopt does).
231*2fe8fb19SBen Gras.El
232*2fe8fb19SBen Gras.It Li o
233*2fe8fb19SBen Grashandling of --a in getopt:
234*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
235*2fe8fb19SBen Gras.It Li GNU
236*2fe8fb19SBen Grasparses this as option '-', option 'a'.
237*2fe8fb19SBen Gras.It Li NetBSD
238*2fe8fb19SBen Grasparses this as '--', and returns \-1 (ignoring the a).
239*2fe8fb19SBen Gras(Because the original getopt does.)
240*2fe8fb19SBen Gras.El
241*2fe8fb19SBen Gras.It Li o
242*2fe8fb19SBen Grassetting of optopt for long options with flag !=
243*2fe8fb19SBen Gras.Dv NULL :
244*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
245*2fe8fb19SBen Gras.It Li GNU
246*2fe8fb19SBen Grassets optopt to val.
247*2fe8fb19SBen Gras.It Li NetBSD
248*2fe8fb19SBen Grassets optopt to 0 (since val would never be returned).
249*2fe8fb19SBen Gras.El
250*2fe8fb19SBen Gras.It Li o
251*2fe8fb19SBen Grashandling of -W with W; in option string in getopt (not getopt_long):
252*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
253*2fe8fb19SBen Gras.It Li GNU
254*2fe8fb19SBen Grascauses a segfault.
255*2fe8fb19SBen Gras.It Li NetBSD
256*2fe8fb19SBen Grasreturns \-1, with optind pointing past the argument of -W
257*2fe8fb19SBen Gras(as if `-W arg' were `--arg', and thus '--' had been found).
258*2fe8fb19SBen Gras.\" How should we treat W; in the option string when called via
259*2fe8fb19SBen Gras.\" getopt?  Ignore the ';' or treat it as a ':'? Issue a warning?
260*2fe8fb19SBen Gras.El
261*2fe8fb19SBen Gras.It Li o
262*2fe8fb19SBen Grassetting of optarg for long options without an argument that are
263*2fe8fb19SBen Grasinvoked via -W (W; in option string):
264*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
265*2fe8fb19SBen Gras.It Li GNU
266*2fe8fb19SBen Grassets optarg to the option name (the argument of -W).
267*2fe8fb19SBen Gras.It Li NetBSD
268*2fe8fb19SBen Grassets optarg to
269*2fe8fb19SBen Gras.Dv NULL
270*2fe8fb19SBen Gras(the argument of the long option).
271*2fe8fb19SBen Gras.El
272*2fe8fb19SBen Gras.It Li o
273*2fe8fb19SBen Grashandling of -W with an argument that is not (a prefix to) a known
274*2fe8fb19SBen Graslong option (W; in option string):
275*2fe8fb19SBen Gras.Bl -tag -width "NetBSD"
276*2fe8fb19SBen Gras.It Li GNU
277*2fe8fb19SBen Grasreturns -W with optarg set to the unknown option.
278*2fe8fb19SBen Gras.It Li NetBSD
279*2fe8fb19SBen Grastreats this as an error (unknown option) and returns '?' with
280*2fe8fb19SBen Grasoptopt set to 0 and optarg set to
281*2fe8fb19SBen Gras.Dv NULL
282*2fe8fb19SBen Gras(as GNU's man page documents).
283*2fe8fb19SBen Gras.El
284*2fe8fb19SBen Gras.It Li o
285*2fe8fb19SBen GrasThe error messages are different.
286*2fe8fb19SBen Gras.It Li o
287*2fe8fb19SBen GrasNetBSD does not permute the argument vector at the same points in
288*2fe8fb19SBen Grasthe calling sequence as GNU does.
289*2fe8fb19SBen GrasThe aspects normally used by the caller
290*2fe8fb19SBen Gras(ordering after \-1 is returned, value of optind relative
291*2fe8fb19SBen Grasto current positions) are the same, though.
292*2fe8fb19SBen Gras(We do fewer variable swaps.)
293*2fe8fb19SBen Gras.El
294*2fe8fb19SBen Gras.Sh SEE ALSO
295*2fe8fb19SBen Gras.Xr getopt 3
296*2fe8fb19SBen Gras.Sh HISTORY
297*2fe8fb19SBen GrasThe
298*2fe8fb19SBen Gras.Fn getopt_long
299*2fe8fb19SBen Grasfunction first appeared in GNU libiberty.
300*2fe8fb19SBen GrasThe first
301*2fe8fb19SBen Gras.Nx
302*2fe8fb19SBen Grasimplementation appeared in 1.5.
303*2fe8fb19SBen Gras.Sh BUGS
304*2fe8fb19SBen GrasThe implementation can completely replace
305*2fe8fb19SBen Gras.Xr getopt 3 ,
306*2fe8fb19SBen Grasbut right now we are using separate code.
307*2fe8fb19SBen Gras.Pp
308*2fe8fb19SBen GrasThe
309*2fe8fb19SBen Gras.Fa argv
310*2fe8fb19SBen Grasargument is not really const.
311