xref: /netbsd/lib/libc/stdlib/getopt_long.3 (revision bf9ec67e)
1.\"	$NetBSD: getopt_long.3,v 1.7 2002/02/07 09:24:06 ross 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 \*[Lt]getopt.h\*[Gt]
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,		\*[Am]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)) \*[Lt] 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
177This section describes differences to the GNU implementation
178found in glibc-2.1.3:
179.Bl -tag -width "xxx"
180.It Li o
181handling of - as first char of option string in presence of
182environment variable POSIXLY_CORRECT:
183.Bl -tag -width "NetBSD"
184.It Li GNU
185ignores POSIXLY_CORRECT and returns non-options as
186arguments to option '\e1'.
187.It Li NetBSD
188honors POSIXLY_CORRECT and stops at the first non-option.
189.El
190.It Li o
191handling of :: in options string in presence of POSIXLY_CORRECT:
192.Bl -tag -width "NetBSD"
193.It Li Both
194GNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
195mean the preceding option takes an optional argument.
196.El
197.It Li o
198return value in case of missing argument if first character
199(after + or -) in option string is not ':':
200.Bl -tag -width "NetBSD"
201.It Li GNU
202returns '?'
203.It NetBSD
204returns ':' (since NetBSD's getopt does).
205.El
206.It Li o
207handling of --a in getopt:
208.Bl -tag -width "NetBSD"
209.It Li GNU
210parses this as option '-', option 'a'.
211.It Li NetBSD
212parses this as '--', and returns -1 (ignoring the a).  (Because
213the original getopt does.)
214.El
215.It Li o
216setting of optopt for long options with flag != NULL:
217.Bl -tag -width "NetBSD"
218.It Li GNU
219sets optopt to val.
220.It Li NetBSD
221sets optopt to 0 (since val would never be returned).
222.El
223.It Li o
224handling of -W with W; in option string in getopt (not getopt_long):
225.Bl -tag -width "NetBSD"
226.It Li GNU
227causes a segfault.
228.It Li NetBSD
229returns -1, with optind pointing past the argument of -W
230(as if `-W arg' were `--arg', and thus '--' had been found).
231.\" How should we treat W; in the option string when called via
232.\" getopt?  Ignore the ';' or treat it as a ':'? Issue a warning?
233.El
234.It Li o
235setting of optarg for long options without an argument that are
236invoked via -W (W; in option string):
237.Bl -tag -width "NetBSD"
238.It Li GNU
239sets optarg to the option name (the argument of -W).
240.It Li NetBSD
241sets optarg to NULL (the argument of the long option).
242.El
243.It Li o
244handling of -W with an argument that is not (a prefix to) a known
245long option (W; in option string):
246.Bl -tag -width "NetBSD"
247.It Li GNU
248returns -W with optarg set to the unknown option.
249.It Li NetBSD
250treats this as an error (unknown option) and returns '?' with
251optopt set to 0 and optarg set to NULL (as GNU's man page
252documents).
253.El
254.It Li o
255The error messages are different.
256.It Li o
257NetBSD does not permute the argument vector at the same points in
258the calling sequence as GNU does.  The aspects normally used by
259the caller (ordering after -1 is returned, value of optind relative
260to current positions) are the same, though.  (We do fewer variable
261swaps.)
262.El
263.Sh SEE ALSO
264.Xr getopt 3
265.Sh HISTORY
266The
267.Fn getopt_long
268function first appeared in GNU libiberty. The first
269.Nx
270implementation appeared in 1.5.
271.Sh BUGS
272The implementation, can completelely replace
273.Xr getopt 3 ,
274but right now we are using separate code.
275