xref: /freebsd/lib/libc/stdlib/getopt_long.3 (revision c2025a76606b44c4d5367b7509fbc0285ae1e7f8)
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
136629a7369SRuslan Ermilov.It Dv 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
193629a7369SRuslan Ermilov.Vt "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.
21624a0682cSRuslan Ermilov.Sh ENVIRONMENT
21724a0682cSRuslan Ermilov.Bl -tag -width ".Ev POSIXLY_CORRECT"
21824a0682cSRuslan Ermilov.It Ev POSIXLY_CORRECT
21924a0682cSRuslan ErmilovIf set, option processing stops when the first non-option is found and
22024a0682cSRuslan Ermilova leading
22124a0682cSRuslan Ermilov.Ql -
22224a0682cSRuslan Ermilovor
22324a0682cSRuslan Ermilov.Ql +
22424a0682cSRuslan Ermilovin the
22524a0682cSRuslan Ermilov.Fa optstring
22624a0682cSRuslan Ermilovis ignored.
22724a0682cSRuslan Ermilov.El
228a35a7e76SEric Melville.Sh EXAMPLES
229a35a7e76SEric Melville.Bd -literal -compact
230a35a7e76SEric Melvilleint bflag, ch, fd;
231a35a7e76SEric Melvilleint daggerset;
232a35a7e76SEric Melville
233a35a7e76SEric Melville/* options descriptor */
234a35a7e76SEric Melvillestatic struct option longopts[] = {
235829a229dSAndrey A. Chernov	{ "buffy",	no_argument,		NULL, 		'b' },
236829a229dSAndrey A. Chernov	{ "fluoride",	required_argument,	NULL, 	       	'f' },
237a35a7e76SEric Melville	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
238829a229dSAndrey A. Chernov	{ NULL,		0,			NULL, 		0 }
239a35a7e76SEric Melville};
240a35a7e76SEric Melville
241a35a7e76SEric Melvillebflag = 0;
242a35a7e76SEric Melvillewhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
243a35a7e76SEric Melville	switch (ch) {
244a35a7e76SEric Melville	case 'b':
245a35a7e76SEric Melville		bflag = 1;
246a35a7e76SEric Melville		break;
247a35a7e76SEric Melville	case 'f':
248829a229dSAndrey A. Chernov		if ((fd = open(optarg, O_RDONLY, 0)) == -1)
249829a229dSAndrey A. Chernov			err(1, "unable to open %s", optarg);
250a35a7e76SEric Melville		break;
251a35a7e76SEric Melville	case 0:
252a35a7e76SEric Melville		if (daggerset) {
253a35a7e76SEric Melville			fprintf(stderr,"Buffy will use her dagger to "
254829a229dSAndrey A. Chernov			    "apply fluoride to dracula's teeth\en");
255a35a7e76SEric Melville		}
256a35a7e76SEric Melville		break;
257a35a7e76SEric Melville	default:
258a35a7e76SEric Melville		usage();
259a35a7e76SEric Melville}
260a35a7e76SEric Melvilleargc -= optind;
261a35a7e76SEric Melvilleargv += optind;
262a35a7e76SEric Melville.Ed
263a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES
264187f61dfSRuslan ErmilovThis section describes differences to the
265187f61dfSRuslan Ermilov.Tn GNU
266187f61dfSRuslan Ermilovimplementation
267a35a7e76SEric Melvillefound in glibc-2.1.3:
268187f61dfSRuslan Ermilov.Bl -bullet
2698a4dd785SAndrey A. Chernov.\" .It
2708a4dd785SAndrey A. Chernov.\" Handling of
2718a4dd785SAndrey A. Chernov.\" .Ql -
2728a4dd785SAndrey A. Chernov.\" as first char of option string in presence of
2738a4dd785SAndrey A. Chernov.\" environment variable
2748a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
2758a4dd785SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
2768a4dd785SAndrey A. Chernov.\" .It Tn GNU
2778a4dd785SAndrey A. Chernov.\" ignores
2788a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
2798a4dd785SAndrey A. Chernov.\" and returns non-options as
2808a4dd785SAndrey A. Chernov.\" arguments to option '\e1'.
2818a4dd785SAndrey A. Chernov.\" .It Bx
2828a4dd785SAndrey A. Chernov.\" honors
2838a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
2848a4dd785SAndrey A. Chernov.\" and stops at the first non-option.
2858a4dd785SAndrey A. Chernov.\" .El
2863700175bSAndrey A. Chernov.\" .It
2873700175bSAndrey A. Chernov.\" Handling of
2883700175bSAndrey A. Chernov.\" .Ql -
2893700175bSAndrey A. Chernov.\" within the option string (not the first character):
2903700175bSAndrey A. Chernov.\" .Bl -tag -width ".Bx"
2913700175bSAndrey A. Chernov.\" .It Tn GNU
2923700175bSAndrey A. Chernov.\" treats a
2933700175bSAndrey A. Chernov.\" .Ql -
2943700175bSAndrey A. Chernov.\" on the command line as a non-argument.
2953700175bSAndrey A. Chernov.\" .It Bx
2963700175bSAndrey A. Chernov.\" a
2973700175bSAndrey A. Chernov.\" .Ql -
2983700175bSAndrey A. Chernov.\" within the option string matches a
2993700175bSAndrey A. Chernov.\" .Ql -
3003700175bSAndrey A. Chernov.\" (single dash) on the command line.
3013700175bSAndrey A. Chernov.\" This functionality is provided for backward compatibility with
3023700175bSAndrey A. Chernov.\" programs, such as
3033700175bSAndrey A. Chernov.\" .Xr su 1 ,
3043700175bSAndrey A. Chernov.\" that use
3053700175bSAndrey A. Chernov.\" .Ql -
3063700175bSAndrey A. Chernov.\" as an option flag.
3073700175bSAndrey A. Chernov.\" This practice is wrong, and should not be used in any current development.
3083700175bSAndrey A. Chernov.\" .El
309f2fd86b7SAndrey A. Chernov.\" .It
310f2fd86b7SAndrey A. Chernov.\" Handling of
311f2fd86b7SAndrey A. Chernov.\" .Ql ::
312f2fd86b7SAndrey A. Chernov.\" in options string in presence of
313f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
314f2fd86b7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
315f2fd86b7SAndrey A. Chernov.\" .It Both
316f2fd86b7SAndrey A. Chernov.\" .Tn GNU
317f2fd86b7SAndrey A. Chernov.\" and
318f2fd86b7SAndrey A. Chernov.\" .Bx
319f2fd86b7SAndrey A. Chernov.\" ignore
320f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
321f2fd86b7SAndrey A. Chernov.\" here and take
322f2fd86b7SAndrey A. Chernov.\" .Ql ::
323f2fd86b7SAndrey A. Chernov.\" to
324f2fd86b7SAndrey A. Chernov.\" mean the preceding option takes an optional argument.
325f2fd86b7SAndrey A. Chernov.\" .El
32688485399SAndrey A. Chernov.\" .It
32788485399SAndrey A. Chernov.\" Return value in case of missing argument if first character
32888485399SAndrey A. Chernov.\" (after
32988485399SAndrey A. Chernov.\" .Ql +
33088485399SAndrey A. Chernov.\" or
33188485399SAndrey A. Chernov.\" .Ql - )
33288485399SAndrey A. Chernov.\" in option string is not
33388485399SAndrey A. Chernov.\" .Ql \&: :
33488485399SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
33588485399SAndrey A. Chernov.\" .It Tn GNU
33688485399SAndrey A. Chernov.\" returns
33788485399SAndrey A. Chernov.\" .Ql \&?
33888485399SAndrey A. Chernov.\" .It Bx
33988485399SAndrey A. Chernov.\" returns
34088485399SAndrey A. Chernov.\" .Ql \&:
34188485399SAndrey A. Chernov.\" (since
34288485399SAndrey A. Chernov.\" .Bx Ns 's
34388485399SAndrey A. Chernov.\" .Fn getopt
34488485399SAndrey A. Chernov.\" does).
34588485399SAndrey A. Chernov.\" .El
3462298a6e7SAndrey A. Chernov.\" .It
3472298a6e7SAndrey A. Chernov.\" Handling of
3482298a6e7SAndrey A. Chernov.\" .Ql --a
3492298a6e7SAndrey A. Chernov.\" in getopt:
3502298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3512298a6e7SAndrey A. Chernov.\" .It Tn GNU
3522298a6e7SAndrey A. Chernov.\" parses this as option
3532298a6e7SAndrey A. Chernov.\" .Ql - ,
3542298a6e7SAndrey A. Chernov.\" option
3552298a6e7SAndrey A. Chernov.\" .Ql a .
3562298a6e7SAndrey A. Chernov.\" .It Bx
3572298a6e7SAndrey A. Chernov.\" parses this as
3582298a6e7SAndrey A. Chernov.\" .Ql -- ,
3592298a6e7SAndrey A. Chernov.\" and returns \-1 (ignoring the
3602298a6e7SAndrey A. Chernov.\" .Ql a ) .
3612298a6e7SAndrey A. Chernov.\" (Because the original
3622298a6e7SAndrey A. Chernov.\" .Fn getopt
3632298a6e7SAndrey A. Chernov.\" does.)
3642298a6e7SAndrey A. Chernov.\" .El
365187f61dfSRuslan Ermilov.It
366187f61dfSRuslan ErmilovSetting of
367187f61dfSRuslan Ermilov.Va optopt
368187f61dfSRuslan Ermilovfor long options with
369187f61dfSRuslan Ermilov.Va flag
370187f61dfSRuslan Ermilov!=
371187f61dfSRuslan Ermilov.Dv NULL :
372829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
373187f61dfSRuslan Ermilov.It Tn GNU
374187f61dfSRuslan Ermilovsets
375187f61dfSRuslan Ermilov.Va optopt
376187f61dfSRuslan Ermilovto
377187f61dfSRuslan Ermilov.Va val .
378829a229dSAndrey A. Chernov.It Bx
379187f61dfSRuslan Ermilovsets
380187f61dfSRuslan Ermilov.Va optopt
381187f61dfSRuslan Ermilovto 0 (since
382187f61dfSRuslan Ermilov.Va val
383187f61dfSRuslan Ermilovwould never be returned).
384a35a7e76SEric Melville.El
3852298a6e7SAndrey A. Chernov.\" .It
3862298a6e7SAndrey A. Chernov.\" Handling of
3872298a6e7SAndrey A. Chernov.\" .Ql -W
3882298a6e7SAndrey A. Chernov.\" with
3892298a6e7SAndrey A. Chernov.\" .Ql W;
3902298a6e7SAndrey A. Chernov.\" in option string in
3912298a6e7SAndrey A. Chernov.\" .Fn getopt
3922298a6e7SAndrey A. Chernov.\" (not
3932298a6e7SAndrey A. Chernov.\" .Fn getopt_long ) :
3942298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3952298a6e7SAndrey A. Chernov.\" .It Tn GNU
3962298a6e7SAndrey A. Chernov.\" causes a segfault.
3972298a6e7SAndrey A. Chernov.\" .It Bx
3982298a6e7SAndrey A. Chernov.\" no special handling is done;
3992298a6e7SAndrey A. Chernov.\" .Ql W;
4002298a6e7SAndrey A. Chernov.\" is interpreted as two separate options, neither of which take an argument.
4012298a6e7SAndrey A. Chernov.\" .El
402187f61dfSRuslan Ermilov.It
403187f61dfSRuslan ErmilovSetting of
404187f61dfSRuslan Ermilov.Va optarg
405187f61dfSRuslan Ermilovfor long options without an argument that are
406187f61dfSRuslan Ermilovinvoked via
407187f61dfSRuslan Ermilov.Ql -W
408187f61dfSRuslan Ermilov.Ql ( W;
409187f61dfSRuslan Ermilovin option string):
410829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
411187f61dfSRuslan Ermilov.It Tn GNU
412187f61dfSRuslan Ermilovsets
413187f61dfSRuslan Ermilov.Va optarg
414187f61dfSRuslan Ermilovto the option name (the argument of
415187f61dfSRuslan Ermilov.Ql -W ) .
416829a229dSAndrey A. Chernov.It Bx
417187f61dfSRuslan Ermilovsets
418187f61dfSRuslan Ermilov.Va optarg
419187f61dfSRuslan Ermilovto
420187f61dfSRuslan Ermilov.Dv NULL
421187f61dfSRuslan Ermilov(the argument of the long option).
422a35a7e76SEric Melville.El
423187f61dfSRuslan Ermilov.It
424187f61dfSRuslan ErmilovHandling of
425187f61dfSRuslan Ermilov.Ql -W
426187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known
427187f61dfSRuslan Ermilovlong option
428187f61dfSRuslan Ermilov.Ql ( W;
429187f61dfSRuslan Ermilovin option string):
430829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
431187f61dfSRuslan Ermilov.It Tn GNU
432187f61dfSRuslan Ermilovreturns
433187f61dfSRuslan Ermilov.Ql -W
434187f61dfSRuslan Ermilovwith
435187f61dfSRuslan Ermilov.Va optarg
436187f61dfSRuslan Ermilovset to the unknown option.
437829a229dSAndrey A. Chernov.It Bx
438187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns
439187f61dfSRuslan Ermilov.Ql \&?
440187f61dfSRuslan Ermilovwith
441187f61dfSRuslan Ermilov.Va optopt
442187f61dfSRuslan Ermilovset to 0 and
443187f61dfSRuslan Ermilov.Va optarg
444187f61dfSRuslan Ermilovset to
445187f61dfSRuslan Ermilov.Dv NULL
446187f61dfSRuslan Ermilov(as
447187f61dfSRuslan Ermilov.Tn GNU Ns 's
448187f61dfSRuslan Ermilovman page documents).
449a35a7e76SEric Melville.El
450f2fd86b7SAndrey A. Chernov.\" .It
451f2fd86b7SAndrey A. Chernov.\" The error messages are different.
452187f61dfSRuslan Ermilov.It
453829a229dSAndrey A. Chernov.Bx
454187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in
455187f61dfSRuslan Ermilovthe calling sequence as
456187f61dfSRuslan Ermilov.Tn GNU
457187f61dfSRuslan Ermilovdoes.
458187f61dfSRuslan ErmilovThe aspects normally used by
459187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of
460187f61dfSRuslan Ermilov.Va optind
461187f61dfSRuslan Ermilovrelative
462187f61dfSRuslan Ermilovto current positions) are the same, though.
463187f61dfSRuslan Ermilov(We do fewer variable swaps.)
464a35a7e76SEric Melville.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
472*c2025a76SJoel Dahlfunctions first appeared in the
473187f61dfSRuslan Ermilov.Tn GNU
474*c2025a76SJoel Dahllibiberty library.
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
497629a7369SRuslan Ermilov.Fa argv
498829a229dSAndrey A. Chernovargument is not really
499629a7369SRuslan Ermilov.Vt 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