xref: /freebsd/lib/libc/stdlib/getopt_long.3 (revision 829a229d88f126ce06c2d47112d81621e1ec35e6)
1829a229dSAndrey A. Chernov.\"	$OpenBSD: getopt_long.3,v 1.10 2004/01/06 23:44:28 fgsch Exp $
2829a229dSAndrey A. Chernov.\"	$NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc Exp $
3a35a7e76SEric Melville.\"
4a35a7e76SEric Melville.\" Copyright (c) 1988, 1991, 1993
5a35a7e76SEric Melville.\"	The Regents of the University of California.  All rights reserved.
6a35a7e76SEric Melville.\"
7a35a7e76SEric Melville.\" Redistribution and use in source and binary forms, with or without
8a35a7e76SEric Melville.\" modification, are permitted provided that the following conditions
9a35a7e76SEric Melville.\" are met:
10a35a7e76SEric Melville.\" 1. Redistributions of source code must retain the above copyright
11a35a7e76SEric Melville.\"    notice, this list of conditions and the following disclaimer.
12a35a7e76SEric Melville.\" 2. Redistributions in binary form must reproduce the above copyright
13a35a7e76SEric Melville.\"    notice, this list of conditions and the following disclaimer in the
14a35a7e76SEric Melville.\"    documentation and/or other materials provided with the distribution.
15829a229dSAndrey A. Chernov.\" 3. Neither the name of the University nor the names of its contributors
16a35a7e76SEric Melville.\"    may be used to endorse or promote products derived from this software
17a35a7e76SEric Melville.\"    without specific prior written permission.
18a35a7e76SEric Melville.\"
19a35a7e76SEric Melville.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a35a7e76SEric Melville.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a35a7e76SEric Melville.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a35a7e76SEric Melville.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a35a7e76SEric Melville.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a35a7e76SEric Melville.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a35a7e76SEric Melville.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a35a7e76SEric Melville.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a35a7e76SEric Melville.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a35a7e76SEric Melville.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a35a7e76SEric Melville.\" SUCH DAMAGE.
30a35a7e76SEric Melville.\"
31a35a7e76SEric Melville.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
32187f61dfSRuslan Ermilov.\" $FreeBSD$
33a35a7e76SEric Melville.\"
34a35a7e76SEric Melville.Dd April 1, 2000
35a35a7e76SEric Melville.Dt GETOPT_LONG 3
36a35a7e76SEric Melville.Os
37a35a7e76SEric Melville.Sh NAME
38829a229dSAndrey A. Chernov.Nm getopt_long ,
39829a229dSAndrey A. Chernov.Nm getopt_long_only
40a35a7e76SEric Melville.Nd get long options from command line argument list
41a35a7e76SEric Melville.Sh LIBRARY
42a35a7e76SEric Melville.Lb libc
43a35a7e76SEric Melville.Sh SYNOPSIS
44187f61dfSRuslan Ermilov.In getopt.h
4575207c7aSAndrey A. Chernov.Vt extern char *optarg ;
4675207c7aSAndrey A. Chernov.Vt extern int optind ;
4775207c7aSAndrey A. Chernov.Vt extern int optopt ;
4875207c7aSAndrey A. Chernov.Vt extern int opterr ;
4975207c7aSAndrey A. Chernov.Vt extern int optreset ;
50a35a7e76SEric Melville.Ft int
51187f61dfSRuslan Ermilov.Fo getopt_long
52187f61dfSRuslan Ermilov.Fa "int argc" "char * const *argv" "const char *optstring"
53829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex"
54829a229dSAndrey A. Chernov.Fc
55829a229dSAndrey A. Chernov.Ft int
56829a229dSAndrey A. Chernov.Fo getopt_long_only
57829a229dSAndrey A. Chernov.Fa "int argc" "char * const *argv" "const char *optstring"
58829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex"
59187f61dfSRuslan Ermilov.Fc
60a35a7e76SEric Melville.Sh DESCRIPTION
61a35a7e76SEric MelvilleThe
62a35a7e76SEric Melville.Fn getopt_long
63a35a7e76SEric Melvillefunction is similar to
64a35a7e76SEric Melville.Xr getopt 3
65187f61dfSRuslan Ermilovbut it accepts options in two forms: words and characters.
66187f61dfSRuslan ErmilovThe
67a35a7e76SEric Melville.Fn getopt_long
68187f61dfSRuslan Ermilovfunction provides a superset of the functionality of
69a35a7e76SEric Melville.Xr getopt 3 .
701fae73b1SRuslan ErmilovThe
71a35a7e76SEric Melville.Fn getopt_long
721fae73b1SRuslan Ermilovfunction
73187f61dfSRuslan Ermilovcan be used in two ways.
74187f61dfSRuslan ErmilovIn the first way, every long option understood
75a35a7e76SEric Melvilleby the program has a corresponding short option, and the option
76a35a7e76SEric Melvillestructure is only used to translate from long options to short
77187f61dfSRuslan Ermilovoptions.
78187f61dfSRuslan ErmilovWhen used in this fashion,
79a35a7e76SEric Melville.Fn getopt_long
80a35a7e76SEric Melvillebehaves identically to
81a35a7e76SEric Melville.Xr getopt 3 .
82a35a7e76SEric MelvilleThis is a good way to add long option processing to an existing program
83a35a7e76SEric Melvillewith the minimum of rewriting.
84a35a7e76SEric Melville.Pp
85a35a7e76SEric MelvilleIn the second mechanism, a long option sets a flag in the
86187f61dfSRuslan Ermilov.Vt option
87a35a7e76SEric Melvillestructure passed, or will store a pointer to the command line argument
88a35a7e76SEric Melvillein the
89187f61dfSRuslan Ermilov.Vt option
90187f61dfSRuslan Ermilovstructure passed to it for options that take arguments.
91187f61dfSRuslan ErmilovAdditionally,
92a35a7e76SEric Melvillethe long option's argument may be specified as a single argument with
93187f61dfSRuslan Ermilovan equal sign, e.g.,
94a35a7e76SEric Melville.Pp
95187f61dfSRuslan Ermilov.Dl "myprogram --myoption=somevalue"
96187f61dfSRuslan Ermilov.Pp
97187f61dfSRuslan ErmilovWhen a long option is processed, the call to
98a35a7e76SEric Melville.Fn getopt_long
99187f61dfSRuslan Ermilovwill return 0.
100187f61dfSRuslan ErmilovFor this reason, long option processing without
101a35a7e76SEric Melvilleshortcuts is not backwards compatible with
102a35a7e76SEric Melville.Xr getopt 3 .
103a35a7e76SEric Melville.Pp
104a35a7e76SEric MelvilleIt is possible to combine these methods, providing for long options
105187f61dfSRuslan Ermilovprocessing with short option equivalents for some options.
106187f61dfSRuslan ErmilovLess
107a35a7e76SEric Melvillefrequently used options would be processed as long options only.
108a35a7e76SEric Melville.Pp
109a35a7e76SEric MelvilleThe
110a35a7e76SEric Melville.Fn getopt_long
111a35a7e76SEric Melvillecall requires a structure to be initialized describing the long
112187f61dfSRuslan Ermilovoptions.
113187f61dfSRuslan ErmilovThe structure is:
114187f61dfSRuslan Ermilov.Bd -literal -offset indent
115a35a7e76SEric Melvillestruct option {
116a35a7e76SEric Melville	char *name;
117a35a7e76SEric Melville	int has_arg;
118a35a7e76SEric Melville	int *flag;
119a35a7e76SEric Melville	int val;
120a35a7e76SEric Melville};
121a35a7e76SEric Melville.Ed
122a35a7e76SEric Melville.Pp
123a35a7e76SEric MelvilleThe
124187f61dfSRuslan Ermilov.Va name
125a35a7e76SEric Melvillefield should contain the option name without the leading double dash.
126a35a7e76SEric Melville.Pp
127a35a7e76SEric MelvilleThe
128187f61dfSRuslan Ermilov.Va has_arg
129a35a7e76SEric Melvillefield should be one of:
130187f61dfSRuslan Ermilov.Pp
131187f61dfSRuslan Ermilov.Bl -tag -width ".Dv optional_argument" -offset indent -compact
132187f61dfSRuslan Ermilov.It Dv no_argument
133187f61dfSRuslan Ermilovno argument to the option is expect
134187f61dfSRuslan Ermilov.It Dv required_argument
135187f61dfSRuslan Ermilovan argument to the option is required
136a35a7e76SEric Melville.It Li optional_argument
137a35a7e76SEric Melvillean argument to the option may be presented.
138a35a7e76SEric Melville.El
139a35a7e76SEric Melville.Pp
140a35a7e76SEric MelvilleIf
141187f61dfSRuslan Ermilov.Va flag
142187f61dfSRuslan Ermilovis not
143187f61dfSRuslan Ermilov.Dv NULL ,
144187f61dfSRuslan Ermilovthen the integer pointed to by it will be set to the
145a35a7e76SEric Melvillevalue in the
146187f61dfSRuslan Ermilov.Va val
147187f61dfSRuslan Ermilovfield.
148187f61dfSRuslan ErmilovIf the
149187f61dfSRuslan Ermilov.Va flag
150187f61dfSRuslan Ermilovfield is
151187f61dfSRuslan Ermilov.Dv NULL ,
152187f61dfSRuslan Ermilovthen the
153187f61dfSRuslan Ermilov.Va val
154187f61dfSRuslan Ermilovfield will be returned.
155187f61dfSRuslan ErmilovSetting
156187f61dfSRuslan Ermilov.Va flag
157187f61dfSRuslan Ermilovto
158187f61dfSRuslan Ermilov.Dv NULL
159187f61dfSRuslan Ermilovand setting
160187f61dfSRuslan Ermilov.Va val
161a35a7e76SEric Melvilleto the corresponding short option will make this function act just
162a35a7e76SEric Melvillelike
163a35a7e76SEric Melville.Xr getopt 3 .
164829a229dSAndrey A. Chernov.Pp
165829a229dSAndrey A. ChernovIf the
166829a229dSAndrey A. Chernov.Fa longindex
167829a229dSAndrey A. Chernovfield is not
168829a229dSAndrey A. Chernov.Dv NULL ,
169829a229dSAndrey A. Chernovthen the integer pointed to by it will be set to the index of the long
170829a229dSAndrey A. Chernovoption relative to
171829a229dSAndrey A. Chernov.Fa longopts .
172829a229dSAndrey A. Chernov.Pp
173829a229dSAndrey A. ChernovThe last element of the
174829a229dSAndrey A. Chernov.Fa longopts
175829a229dSAndrey A. Chernovarray has to be filled with zeroes.
176829a229dSAndrey A. Chernov.Pp
177829a229dSAndrey A. ChernovThe
178829a229dSAndrey A. Chernov.Fn getopt_long_only
179829a229dSAndrey A. Chernovfunction behaves identically to
180829a229dSAndrey A. Chernov.Fn getopt_long
181829a229dSAndrey A. Chernovwith the exception that long options may start with
182829a229dSAndrey A. Chernov.Ql -
183829a229dSAndrey A. Chernovin addition to
184829a229dSAndrey A. Chernov.Ql -- .
185829a229dSAndrey A. ChernovIf an option starting with
186829a229dSAndrey A. Chernov.Ql -
187829a229dSAndrey A. Chernovdoes not match a long option but does match a single-character option,
188829a229dSAndrey A. Chernovthe single-character option is returned.
189829a229dSAndrey A. Chernov.Sh RETURN VALUES
190829a229dSAndrey A. ChernovIf the
191829a229dSAndrey A. Chernov.Fa flag
192829a229dSAndrey A. Chernovfield in
193829a229dSAndrey A. Chernov.Li struct option
194829a229dSAndrey A. Chernovis
195829a229dSAndrey A. Chernov.Dv NULL ,
196829a229dSAndrey A. Chernov.Fn getopt_long
197829a229dSAndrey A. Chernovand
198829a229dSAndrey A. Chernov.Fn getopt_long_only
199829a229dSAndrey A. Chernovreturn the value specified in the
200829a229dSAndrey A. Chernov.Fa val
201829a229dSAndrey A. Chernovfield, which is usually just the corresponding short option.
202829a229dSAndrey A. ChernovIf
203829a229dSAndrey A. Chernov.Fa flag
204829a229dSAndrey A. Chernovis not
205829a229dSAndrey A. Chernov.Dv NULL ,
206829a229dSAndrey A. Chernovthese functions return 0 and store
207829a229dSAndrey A. Chernov.Fa val
208829a229dSAndrey A. Chernovin the location pointed to by
209829a229dSAndrey A. Chernov.Fa flag .
210829a229dSAndrey A. ChernovThese functions return
211829a229dSAndrey A. Chernov.Ql \&:
212829a229dSAndrey A. Chernovif there was a missing option argument,
213829a229dSAndrey A. Chernov.Ql \&?
214829a229dSAndrey A. Chernovif the user specified an unknown or ambiguous option, and
215829a229dSAndrey A. Chernov\-1 when the argument list has been exhausted.
216a35a7e76SEric Melville.Sh EXAMPLES
217a35a7e76SEric Melville.Bd -literal -compact
218a35a7e76SEric Melvilleint bflag, ch, fd;
219a35a7e76SEric Melvilleint daggerset;
220a35a7e76SEric Melville
221a35a7e76SEric Melville/* options descriptor */
222a35a7e76SEric Melvillestatic struct option longopts[] = {
223829a229dSAndrey A. Chernov	{ "buffy",	no_argument,		NULL, 		'b' },
224829a229dSAndrey A. Chernov	{ "fluoride",	required_argument,	NULL, 	       	'f' },
225a35a7e76SEric Melville	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
226829a229dSAndrey A. Chernov	{ NULL,		0,			NULL, 		0 }
227a35a7e76SEric Melville};
228a35a7e76SEric Melville
229a35a7e76SEric Melvillebflag = 0;
230a35a7e76SEric Melvillewhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
231a35a7e76SEric Melville	switch (ch) {
232a35a7e76SEric Melville	case 'b':
233a35a7e76SEric Melville		bflag = 1;
234a35a7e76SEric Melville		break;
235a35a7e76SEric Melville	case 'f':
236829a229dSAndrey A. Chernov		if ((fd = open(optarg, O_RDONLY, 0)) == -1)
237829a229dSAndrey A. Chernov			err(1, "unable to open %s", optarg);
238a35a7e76SEric Melville		break;
239a35a7e76SEric Melville	case 0:
240a35a7e76SEric Melville		if (daggerset) {
241a35a7e76SEric Melville			fprintf(stderr,"Buffy will use her dagger to "
242829a229dSAndrey A. Chernov			    "apply fluoride to dracula's teeth\en");
243a35a7e76SEric Melville		}
244a35a7e76SEric Melville		break;
245a35a7e76SEric Melville	default:
246a35a7e76SEric Melville		usage();
247a35a7e76SEric Melville}
248a35a7e76SEric Melvilleargc -= optind;
249a35a7e76SEric Melvilleargv += optind;
250a35a7e76SEric Melville.Ed
251a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES
252187f61dfSRuslan ErmilovThis section describes differences to the
253187f61dfSRuslan Ermilov.Tn GNU
254187f61dfSRuslan Ermilovimplementation
255a35a7e76SEric Melvillefound in glibc-2.1.3:
256187f61dfSRuslan Ermilov.Bl -bullet
257187f61dfSRuslan Ermilov.It
258187f61dfSRuslan ErmilovHandling of
259187f61dfSRuslan Ermilov.Ql -
260187f61dfSRuslan Ermilovas first char of option string in presence of
261187f61dfSRuslan Ermilovenvironment variable
262187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT :
263829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
264187f61dfSRuslan Ermilov.It Tn GNU
265187f61dfSRuslan Ermilovignores
266187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT
267187f61dfSRuslan Ermilovand returns non-options as
268a35a7e76SEric Melvillearguments to option '\e1'.
269829a229dSAndrey A. Chernov.It Bx
270187f61dfSRuslan Ermilovhonors
271187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT
272187f61dfSRuslan Ermilovand stops at the first non-option.
273a35a7e76SEric Melville.El
274187f61dfSRuslan Ermilov.It
275187f61dfSRuslan ErmilovHandling of
276829a229dSAndrey A. Chernov.Ql -
277829a229dSAndrey A. Chernovwithin the option string (not the first character):
278829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
279829a229dSAndrey A. Chernov.It Tn GNU
280829a229dSAndrey A. Chernovtreats a
281829a229dSAndrey A. Chernov.Ql -
282829a229dSAndrey A. Chernovon the command line as a non-argument.
283829a229dSAndrey A. Chernov.It Bx
284829a229dSAndrey A. Chernova
285829a229dSAndrey A. Chernov.Ql -
286829a229dSAndrey A. Chernovwithin the option string matches a
287829a229dSAndrey A. Chernov.Ql -
288829a229dSAndrey A. Chernov(single dash) on the command line.
289829a229dSAndrey A. ChernovThis functionality is provided for backward compatibility with
290829a229dSAndrey A. Chernovprograms, such as
291829a229dSAndrey A. Chernov.Xr su 1 ,
292829a229dSAndrey A. Chernovthat use
293829a229dSAndrey A. Chernov.Ql -
294829a229dSAndrey A. Chernovas an option flag.
295829a229dSAndrey A. ChernovThis practice is wrong, and should not be used in any current development.
296829a229dSAndrey A. Chernov.El
297829a229dSAndrey A. Chernov.It
298829a229dSAndrey A. ChernovHandling of
299187f61dfSRuslan Ermilov.Ql ::
300187f61dfSRuslan Ermilovin options string in presence of
301187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT :
302829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
303187f61dfSRuslan Ermilov.It Both
304187f61dfSRuslan Ermilov.Tn GNU
305187f61dfSRuslan Ermilovand
306829a229dSAndrey A. Chernov.Bx
307187f61dfSRuslan Ermilovignore
308187f61dfSRuslan Ermilov.Ev POSIXLY_CORRECT
309187f61dfSRuslan Ermilovhere and take
310187f61dfSRuslan Ermilov.Ql ::
311187f61dfSRuslan Ermilovto
312a35a7e76SEric Melvillemean the preceding option takes an optional argument.
313a35a7e76SEric Melville.El
314187f61dfSRuslan Ermilov.It
315187f61dfSRuslan ErmilovReturn value in case of missing argument if first character
316187f61dfSRuslan Ermilov(after
317187f61dfSRuslan Ermilov.Ql +
318187f61dfSRuslan Ermilovor
319187f61dfSRuslan Ermilov.Ql - )
320187f61dfSRuslan Ermilovin option string is not
321187f61dfSRuslan Ermilov.Ql \&: :
322829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
323187f61dfSRuslan Ermilov.It Tn GNU
324187f61dfSRuslan Ermilovreturns
325187f61dfSRuslan Ermilov.Ql \&?
326829a229dSAndrey A. Chernov.It Bx
327187f61dfSRuslan Ermilovreturns
328187f61dfSRuslan Ermilov.Ql \&:
329187f61dfSRuslan Ermilov(since
330829a229dSAndrey A. Chernov.Bx Ns 's
331187f61dfSRuslan Ermilov.Fn getopt
332187f61dfSRuslan Ermilovdoes).
333a35a7e76SEric Melville.El
334187f61dfSRuslan Ermilov.It
335187f61dfSRuslan ErmilovHandling of
336187f61dfSRuslan Ermilov.Ql --a
337187f61dfSRuslan Ermilovin getopt:
338829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
339187f61dfSRuslan Ermilov.It Tn GNU
340187f61dfSRuslan Ermilovparses this as option
341187f61dfSRuslan Ermilov.Ql - ,
342187f61dfSRuslan Ermilovoption
343187f61dfSRuslan Ermilov.Ql a .
344829a229dSAndrey A. Chernov.It Bx
345187f61dfSRuslan Ermilovparses this as
346187f61dfSRuslan Ermilov.Ql -- ,
347187f61dfSRuslan Ermilovand returns \-1 (ignoring the
348187f61dfSRuslan Ermilov.Ql a ) .
349187f61dfSRuslan Ermilov(Because the original
350187f61dfSRuslan Ermilov.Fn getopt
351187f61dfSRuslan Ermilovdoes.)
352a35a7e76SEric Melville.El
353187f61dfSRuslan Ermilov.It
354187f61dfSRuslan ErmilovSetting of
355187f61dfSRuslan Ermilov.Va optopt
356187f61dfSRuslan Ermilovfor long options with
357187f61dfSRuslan Ermilov.Va flag
358187f61dfSRuslan Ermilov!=
359187f61dfSRuslan Ermilov.Dv NULL :
360829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
361187f61dfSRuslan Ermilov.It Tn GNU
362187f61dfSRuslan Ermilovsets
363187f61dfSRuslan Ermilov.Va optopt
364187f61dfSRuslan Ermilovto
365187f61dfSRuslan Ermilov.Va val .
366829a229dSAndrey A. Chernov.It Bx
367187f61dfSRuslan Ermilovsets
368187f61dfSRuslan Ermilov.Va optopt
369187f61dfSRuslan Ermilovto 0 (since
370187f61dfSRuslan Ermilov.Va val
371187f61dfSRuslan Ermilovwould never be returned).
372a35a7e76SEric Melville.El
373187f61dfSRuslan Ermilov.It
374187f61dfSRuslan ErmilovHandling of
375187f61dfSRuslan Ermilov.Ql -W
376187f61dfSRuslan Ermilovwith
377187f61dfSRuslan Ermilov.Ql W;
378187f61dfSRuslan Ermilovin option string in
379187f61dfSRuslan Ermilov.Fn getopt
380187f61dfSRuslan Ermilov(not
381187f61dfSRuslan Ermilov.Fn getopt_long ) :
382829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
383187f61dfSRuslan Ermilov.It Tn GNU
384a35a7e76SEric Melvillecauses a segfault.
385829a229dSAndrey A. Chernov.It Bx
386829a229dSAndrey A. Chernovno special handling is done;
387829a229dSAndrey A. Chernov.Ql W;
388829a229dSAndrey A. Chernovis interpreted as two separate options, neither of which take an argument.
389a35a7e76SEric Melville.El
390187f61dfSRuslan Ermilov.It
391187f61dfSRuslan ErmilovSetting of
392187f61dfSRuslan Ermilov.Va optarg
393187f61dfSRuslan Ermilovfor long options without an argument that are
394187f61dfSRuslan Ermilovinvoked via
395187f61dfSRuslan Ermilov.Ql -W
396187f61dfSRuslan Ermilov.Ql ( W;
397187f61dfSRuslan Ermilovin option string):
398829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
399187f61dfSRuslan Ermilov.It Tn GNU
400187f61dfSRuslan Ermilovsets
401187f61dfSRuslan Ermilov.Va optarg
402187f61dfSRuslan Ermilovto the option name (the argument of
403187f61dfSRuslan Ermilov.Ql -W ) .
404829a229dSAndrey A. Chernov.It Bx
405187f61dfSRuslan Ermilovsets
406187f61dfSRuslan Ermilov.Va optarg
407187f61dfSRuslan Ermilovto
408187f61dfSRuslan Ermilov.Dv NULL
409187f61dfSRuslan Ermilov(the argument of the long option).
410a35a7e76SEric Melville.El
411187f61dfSRuslan Ermilov.It
412187f61dfSRuslan ErmilovHandling of
413187f61dfSRuslan Ermilov.Ql -W
414187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known
415187f61dfSRuslan Ermilovlong option
416187f61dfSRuslan Ermilov.Ql ( W ;
417187f61dfSRuslan Ermilovin option string):
418829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
419187f61dfSRuslan Ermilov.It Tn GNU
420187f61dfSRuslan Ermilovreturns
421187f61dfSRuslan Ermilov.Ql -W
422187f61dfSRuslan Ermilovwith
423187f61dfSRuslan Ermilov.Va optarg
424187f61dfSRuslan Ermilovset to the unknown option.
425829a229dSAndrey A. Chernov.It Bx
426187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns
427187f61dfSRuslan Ermilov.Ql \&?
428187f61dfSRuslan Ermilovwith
429187f61dfSRuslan Ermilov.Va optopt
430187f61dfSRuslan Ermilovset to 0 and
431187f61dfSRuslan Ermilov.Va optarg
432187f61dfSRuslan Ermilovset to
433187f61dfSRuslan Ermilov.Dv NULL
434187f61dfSRuslan Ermilov(as
435187f61dfSRuslan Ermilov.Tn GNU Ns 's
436187f61dfSRuslan Ermilovman page documents).
437a35a7e76SEric Melville.El
438187f61dfSRuslan Ermilov.It
439a35a7e76SEric MelvilleThe error messages are different.
440187f61dfSRuslan Ermilov.It
441829a229dSAndrey A. Chernov.Bx
442187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in
443187f61dfSRuslan Ermilovthe calling sequence as
444187f61dfSRuslan Ermilov.Tn GNU
445187f61dfSRuslan Ermilovdoes.
446187f61dfSRuslan ErmilovThe aspects normally used by
447187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of
448187f61dfSRuslan Ermilov.Va optind
449187f61dfSRuslan Ermilovrelative
450187f61dfSRuslan Ermilovto current positions) are the same, though.
451187f61dfSRuslan Ermilov(We do fewer variable swaps.)
452a35a7e76SEric Melville.El
453829a229dSAndrey A. Chernov.Sh ENVIRONMENT
454829a229dSAndrey A. Chernov.Bl -tag -width POSIXLY_CORRECT
455829a229dSAndrey A. Chernov.It Ev POSIXLY_CORRECT
456829a229dSAndrey A. ChernovIf set, option processing stops when the first non-option is found and
457829a229dSAndrey A. Chernova leading
458829a229dSAndrey A. Chernov.Ql -
459829a229dSAndrey A. Chernovor
460829a229dSAndrey A. Chernov.Ql +
461829a229dSAndrey A. Chernovin the
462829a229dSAndrey A. Chernov.Ar optstring
463829a229dSAndrey A. Chernovis ignored.
464829a229dSAndrey A. Chernov.El
465a35a7e76SEric Melville.Sh SEE ALSO
466a35a7e76SEric Melville.Xr getopt 3
467a35a7e76SEric Melville.Sh HISTORY
468a35a7e76SEric MelvilleThe
469a35a7e76SEric Melville.Fn getopt_long
470829a229dSAndrey A. Chernovand
471829a229dSAndrey A. Chernov.Fn getopt_long_only
472829a229dSAndrey A. Chernovfunctions first appeared in
473187f61dfSRuslan Ermilov.Tn GNU
474187f61dfSRuslan Ermilovlibiberty.
475187f61dfSRuslan ErmilovThe first
476829a229dSAndrey A. Chernov.Bx
477829a229dSAndrey A. Chernovimplementation of
478829a229dSAndrey A. Chernov.Fn getopt_long
479829a229dSAndrey A. Chernovappeared in
480829a229dSAndrey A. Chernov.Nx 1.5 ,
481829a229dSAndrey A. Chernovthe first
482829a229dSAndrey A. Chernov.Bx
483829a229dSAndrey A. Chernovimplementation of
484829a229dSAndrey A. Chernov.Fn getopt_long_only
485829a229dSAndrey A. Chernovin
486829a229dSAndrey A. Chernov.Ox 3.3 .
487829a229dSAndrey A. Chernov.Fx
488829a229dSAndrey A. Chernovfirst included
489829a229dSAndrey A. Chernov.Fn getopt_long
490829a229dSAndrey A. Chernovin
491829a229dSAndrey A. Chernov.Fx 5.0 ,
492829a229dSAndrey A. Chernov.Fn getopt_long_only
493829a229dSAndrey A. Chernovin
494829a229dSAndrey A. Chernov.Fx 5.2 .
495a35a7e76SEric Melville.Sh BUGS
496829a229dSAndrey A. ChernovThe
497829a229dSAndrey A. Chernov.Ar argv
498829a229dSAndrey A. Chernovargument is not really
499829a229dSAndrey A. Chernov.Dv const
500829a229dSAndrey A. Chernovas its elements may be permuted (unless
501829a229dSAndrey A. Chernov.Ev POSIXLY_CORRECT
502829a229dSAndrey A. Chernovis set).
503829a229dSAndrey A. Chernov.Pp
504187f61dfSRuslan ErmilovThe implementation can completely replace
505a35a7e76SEric Melville.Xr getopt 3 ,
506a35a7e76SEric Melvillebut right now we are using separate code.
507