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