xref: /freebsd/lib/libc/stdlib/getopt_long.3 (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
1.\"	$NetBSD: getopt_long.3,v 1.8 2002/06/03 12:01:43 wiz Exp $
2.\"	$FreeBSD$
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.Nd get long options from command line argument list
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.Fd #include \*[Lt]getopt.h\*[Gt]
47.Ft int
48.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long options" "int *index"
49.Sh DESCRIPTION
50The
51.Fn getopt_long
52function is similar to
53.Xr getopt 3
54but it accepts options in two forms: words and characters. The
55.Fn getopt_long
56function provides a superset of of the functionality of
57.Xr getopt 3 .
58.Fn getopt_long
59can be used in two ways. In the first way, every long option understood
60by the program has a corresponding short option, and the option
61structure is only used to translate from long options to short
62options. When used in this fashion,
63.Fn getopt_long
64behaves identically to
65.Xr getopt 3 .
66This is a good way to add long option processing to an existing program
67with the minimum of rewriting.
68.Pp
69In the second mechanism, a long option sets a flag in the
70.Fa option
71structure passed, or will store a pointer to the command line argument
72in the
73.Fa option
74structure passed to it for options that take arguments. Additionally,
75the long option's argument may be specified as a single argument with
76an equal sign, e.g.
77.Bd -literal
78myprogram --myoption=somevalue
79.Ed
80.Pp
81When a long option is processed the call to
82.Fn getopt_long
83will return 0. For this reason, long option processing without
84shortcuts is not backwards compatible with
85.Xr getopt 3 .
86.Pp
87It is possible to combine these methods, providing for long options
88processing with short option equivalents for some options. Less
89frequently used options would be processed as long options only.
90.Pp
91The
92.Fn getopt_long
93call requires a structure to be initialized describing the long
94options. The structure is:
95.Bd -literal
96struct option {
97	char *name;
98	int has_arg;
99	int *flag;
100	int val;
101};
102.Ed
103.Pp
104The
105.Fa name
106field should contain the option name without the leading double dash.
107.Pp
108The
109.Fa has_arg
110field should be one of:
111.Bl -tag -width "optional_argument"
112.It Li no_argument
113no argument to the option is expect.
114.It Li required_argument
115an argument to the option is required.
116.It Li optional_argument
117an argument to the option may be presented.
118.El
119.Pp
120If
121.Fa flag
122is non-NULL, then the integer pointed to by it will be set to the
123value in the
124.Fa val
125field. If the
126.Fa flag
127field is NULL, then the
128.Fa val
129field will be returned. Setting
130.Fa flag
131to NULL and setting
132.Fa val
133to the corresponding short option will make this function act just
134like
135.Xr getopt 3 .
136.Sh EXAMPLES
137.Bd -literal -compact
138extern char *optarg;
139extern int optind;
140int bflag, ch, fd;
141int daggerset;
142
143/* options descriptor */
144static struct option longopts[] = {
145	{ "buffy",	no_argument,		0, 		'b' },
146	{ "floride",	required_argument,	0, 	       	'f' },
147	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
148	{ 0, 		0,			0, 		0 }
149};
150
151bflag = 0;
152while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
153	switch(ch) {
154	case 'b':
155		bflag = 1;
156		break;
157	case 'f':
158		if ((fd = open(optarg, O_RDONLY, 0)) \*[Lt] 0) {
159			(void)fprintf(stderr,
160			    "myname: %s: %s\en", optarg, strerror(errno));
161			exit(1);
162		}
163		break;
164	case 0:
165		if(daggerset) {
166			fprintf(stderr,"Buffy will use her dagger to "
167				       "apply floride to dracula's teeth\en");
168		}
169		break;
170	case '?':
171	default:
172		usage();
173}
174argc -= optind;
175argv += optind;
176.Ed
177.Sh IMPLEMENTATION DIFFERENCES
178This section describes differences to the GNU implementation
179found in glibc-2.1.3:
180.Bl -tag -width "xxx"
181.It Li o
182handling of - as first char of option string in presence of
183environment variable POSIXLY_CORRECT:
184.Bl -tag -width "NetBSD"
185.It Li GNU
186ignores POSIXLY_CORRECT and returns non-options as
187arguments to option '\e1'.
188.It Li NetBSD
189honors POSIXLY_CORRECT and stops at the first non-option.
190.El
191.It Li o
192handling of :: in options string in presence of POSIXLY_CORRECT:
193.Bl -tag -width "NetBSD"
194.It Li Both
195GNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
196mean the preceding option takes an optional argument.
197.El
198.It Li o
199return value in case of missing argument if first character
200(after + or -) in option string is not ':':
201.Bl -tag -width "NetBSD"
202.It Li GNU
203returns '?'
204.It NetBSD
205returns ':' (since NetBSD's getopt does).
206.El
207.It Li o
208handling of --a in getopt:
209.Bl -tag -width "NetBSD"
210.It Li GNU
211parses this as option '-', option 'a'.
212.It Li NetBSD
213parses this as '--', and returns -1 (ignoring the a).  (Because
214the original getopt does.)
215.El
216.It Li o
217setting of optopt for long options with flag != NULL:
218.Bl -tag -width "NetBSD"
219.It Li GNU
220sets optopt to val.
221.It Li NetBSD
222sets optopt to 0 (since val would never be returned).
223.El
224.It Li o
225handling of -W with W; in option string in getopt (not getopt_long):
226.Bl -tag -width "NetBSD"
227.It Li GNU
228causes a segfault.
229.It Li NetBSD
230returns -1, with optind pointing past the argument of -W
231(as if `-W arg' were `--arg', and thus '--' had been found).
232.\" How should we treat W; in the option string when called via
233.\" getopt?  Ignore the ';' or treat it as a ':'? Issue a warning?
234.El
235.It Li o
236setting of optarg for long options without an argument that are
237invoked via -W (W; in option string):
238.Bl -tag -width "NetBSD"
239.It Li GNU
240sets optarg to the option name (the argument of -W).
241.It Li NetBSD
242sets optarg to NULL (the argument of the long option).
243.El
244.It Li o
245handling of -W with an argument that is not (a prefix to) a known
246long option (W; in option string):
247.Bl -tag -width "NetBSD"
248.It Li GNU
249returns -W with optarg set to the unknown option.
250.It Li NetBSD
251treats this as an error (unknown option) and returns '?' with
252optopt set to 0 and optarg set to NULL (as GNU's man page
253documents).
254.El
255.It Li o
256The error messages are different.
257.It Li o
258NetBSD does not permute the argument vector at the same points in
259the calling sequence as GNU does.  The aspects normally used by
260the caller (ordering after -1 is returned, value of optind relative
261to current positions) are the same, though.  (We do fewer variable
262swaps.)
263.El
264.Sh SEE ALSO
265.Xr getopt 3
266.Sh HISTORY
267The
268.Fn getopt_long
269function first appeared in GNU libiberty. The first
270.Nx
271implementation appeared in 1.5.
272.Sh BUGS
273The implementation, can completelely replace
274.Xr getopt 3 ,
275but right now we are using separate code.
276