xref: /netbsd-src/lib/libc/stdlib/getopt_long.3 (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1.\"	$NetBSD: getopt_long.3,v 1.4 2000/11/26 22:35:53 wiz Exp $
2.\"
3.\" Copyright (c) 1988, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
35.\"
36.Dd April 1, 2000
37.Dt GETOPT_LONG 3
38.Os
39.Sh NAME
40.Nm getopt_long
41.Nd get long options from command line argument list
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.Fd #include <getopt.h>
46.Ft int
47.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct options *long options" "int *index"
48.Sh DESCRIPTION
49The
50.Fn getopt_long
51function is similar to
52.Xr getopt 3
53but it accepts options in two forms: words and characters. The
54.Fn getopt_long
55function provides a superset of of the functionality of
56.Xr getopt 3 .
57.Fn getopt_long
58can be used in two ways. In the first way, every long option understood
59by the program has a corresponding short option, and the option
60structure is only used to translate from long options to short
61options. When used in this fashion,
62.Fn getopt_long
63behaves identically to
64.Xr getopt 3 .
65This is a good way to add long option processing to an existing program
66with the minimum of rewriting.
67.Pp
68In the second mechanism, a long option sets a flag in the
69.Fa option
70structure passed, or will store a pointer to the command line argument
71in the
72.Fa option
73structure passed to it for options that take arguments. Additionally,
74the long option's argument may be specified as a single argument with
75an equal sign, e.g.
76.Bd -literal
77myprogram --myoption=somevalue
78.Ed
79.Pp
80When a long option is processed the call to
81.Fn getopt_long
82will return 0. For 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. Less
88frequently 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
93options. The 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 non-NULL, then the integer pointed to by it will be set to the
122value in the
123.Fa val
124field. If the
125.Fa flag
126field is NULL, then the
127.Fa val
128field will be returned. Setting
129.Fa flag
130to NULL and setting
131.Fa val
132to the corresponding short option will make this function act just
133like
134.Xr getopt 3 .
135.Sh EXAMPLES
136.Bd -literal -compact
137extern char *optarg;
138extern int optind;
139int bflag, ch, fd;
140int daggerset;
141
142/* options descriptor */
143static struct option longopts[] = {
144	{ "buffy",	no_argument,		0, 		'b' },
145	{ "floride",	required_argument,	0, 	       	'f' },
146	{ "daggerset",	no_argument,		&daggerset,	1 },
147	{ 0, 		0,			0, 		0 }
148};
149
150bflag = 0;
151while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
152	switch(ch) {
153	case 'b':
154		bflag = 1;
155		break;
156	case 'f':
157		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
158			(void)fprintf(stderr,
159			    "myname: %s: %s\en", optarg, strerror(errno));
160			exit(1);
161		}
162		break;
163	case 0:
164		if(daggerset) {
165			fprintf(stderr,"Buffy will use her dagger to "
166				       "apply floride to dracula's teeth\en");
167		}
168		break;
169	case '?':
170	default:
171		usage();
172}
173argc -= optind;
174argv += optind;
175.Ed
176.Sh IMPLEMENTATION DIFFERENCES
177.Pp
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