xref: /freebsd-14.2/lib/libc/stdlib/getopt_long.3 (revision b2c76c41)
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
32a35a7e76SEric Melville.\"
33*d646513eSKyle Evans.Dd December 24, 2022
34a35a7e76SEric Melville.Dt GETOPT_LONG 3
35a35a7e76SEric Melville.Os
36a35a7e76SEric Melville.Sh NAME
37829a229dSAndrey A. Chernov.Nm getopt_long ,
38829a229dSAndrey A. Chernov.Nm getopt_long_only
39a35a7e76SEric Melville.Nd get long options from command line argument list
40a35a7e76SEric Melville.Sh LIBRARY
41a35a7e76SEric Melville.Lb libc
42a35a7e76SEric Melville.Sh SYNOPSIS
43187f61dfSRuslan Ermilov.In getopt.h
4475207c7aSAndrey A. Chernov.Vt extern char *optarg ;
4575207c7aSAndrey A. Chernov.Vt extern int optind ;
4675207c7aSAndrey A. Chernov.Vt extern int optopt ;
4775207c7aSAndrey A. Chernov.Vt extern int opterr ;
4875207c7aSAndrey A. Chernov.Vt extern int optreset ;
49a35a7e76SEric Melville.Ft int
50187f61dfSRuslan Ermilov.Fo getopt_long
51187f61dfSRuslan Ermilov.Fa "int argc" "char * const *argv" "const char *optstring"
52829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex"
53829a229dSAndrey A. Chernov.Fc
54829a229dSAndrey A. Chernov.Ft int
55829a229dSAndrey A. Chernov.Fo getopt_long_only
56829a229dSAndrey A. Chernov.Fa "int argc" "char * const *argv" "const char *optstring"
57829a229dSAndrey A. Chernov.Fa "const struct option *longopts" "int *longindex"
58187f61dfSRuslan Ermilov.Fc
59a35a7e76SEric Melville.Sh DESCRIPTION
60a35a7e76SEric MelvilleThe
61a35a7e76SEric Melville.Fn getopt_long
62a35a7e76SEric Melvillefunction is similar to
63a35a7e76SEric Melville.Xr getopt 3
64187f61dfSRuslan Ermilovbut it accepts options in two forms: words and characters.
65187f61dfSRuslan ErmilovThe
66a35a7e76SEric Melville.Fn getopt_long
67187f61dfSRuslan Ermilovfunction provides a superset of the functionality of
68a35a7e76SEric Melville.Xr getopt 3 .
691fae73b1SRuslan ErmilovThe
70a35a7e76SEric Melville.Fn getopt_long
711fae73b1SRuslan Ermilovfunction
72187f61dfSRuslan Ermilovcan be used in two ways.
73187f61dfSRuslan ErmilovIn the first way, every long option understood
74a35a7e76SEric Melvilleby the program has a corresponding short option, and the option
75a35a7e76SEric Melvillestructure is only used to translate from long options to short
76187f61dfSRuslan Ermilovoptions.
77187f61dfSRuslan ErmilovWhen used in this fashion,
78a35a7e76SEric Melville.Fn getopt_long
79a35a7e76SEric Melvillebehaves identically to
80a35a7e76SEric Melville.Xr getopt 3 .
81a35a7e76SEric MelvilleThis is a good way to add long option processing to an existing program
82a35a7e76SEric Melvillewith the minimum of rewriting.
83a35a7e76SEric Melville.Pp
84a35a7e76SEric MelvilleIn the second mechanism, a long option sets a flag in the
85187f61dfSRuslan Ermilov.Vt option
86a35a7e76SEric Melvillestructure passed, or will store a pointer to the command line argument
87a35a7e76SEric Melvillein the
88187f61dfSRuslan Ermilov.Vt option
89187f61dfSRuslan Ermilovstructure passed to it for options that take arguments.
90187f61dfSRuslan ErmilovAdditionally,
91a35a7e76SEric Melvillethe long option's argument may be specified as a single argument with
92187f61dfSRuslan Ermilovan equal sign, e.g.,
93a35a7e76SEric Melville.Pp
94187f61dfSRuslan Ermilov.Dl "myprogram --myoption=somevalue"
95187f61dfSRuslan Ermilov.Pp
96187f61dfSRuslan ErmilovWhen a long option is processed, the call to
97a35a7e76SEric Melville.Fn getopt_long
98187f61dfSRuslan Ermilovwill return 0.
99187f61dfSRuslan ErmilovFor this reason, long option processing without
100a35a7e76SEric Melvilleshortcuts is not backwards compatible with
101a35a7e76SEric Melville.Xr getopt 3 .
102a35a7e76SEric Melville.Pp
103a35a7e76SEric MelvilleIt is possible to combine these methods, providing for long options
104187f61dfSRuslan Ermilovprocessing with short option equivalents for some options.
105187f61dfSRuslan ErmilovLess
106a35a7e76SEric Melvillefrequently used options would be processed as long options only.
107a35a7e76SEric Melville.Pp
108a35a7e76SEric MelvilleThe
109a35a7e76SEric Melville.Fn getopt_long
110a35a7e76SEric Melvillecall requires a structure to be initialized describing the long
111187f61dfSRuslan Ermilovoptions.
112187f61dfSRuslan ErmilovThe structure is:
113187f61dfSRuslan Ermilov.Bd -literal -offset indent
114a35a7e76SEric Melvillestruct option {
115a35a7e76SEric Melville	char *name;
116a35a7e76SEric Melville	int has_arg;
117a35a7e76SEric Melville	int *flag;
118a35a7e76SEric Melville	int val;
119a35a7e76SEric Melville};
120a35a7e76SEric Melville.Ed
121a35a7e76SEric Melville.Pp
122a35a7e76SEric MelvilleThe
123187f61dfSRuslan Ermilov.Va name
124a35a7e76SEric Melvillefield should contain the option name without the leading double dash.
125a35a7e76SEric Melville.Pp
126a35a7e76SEric MelvilleThe
127187f61dfSRuslan Ermilov.Va has_arg
128a35a7e76SEric Melvillefield should be one of:
129187f61dfSRuslan Ermilov.Pp
130187f61dfSRuslan Ermilov.Bl -tag -width ".Dv optional_argument" -offset indent -compact
131187f61dfSRuslan Ermilov.It Dv no_argument
1320603b75aSEitan Adlerno argument to the option is expected
133187f61dfSRuslan Ermilov.It Dv required_argument
134187f61dfSRuslan Ermilovan argument to the option is required
135629a7369SRuslan Ermilov.It Dv optional_argument
1360603b75aSEitan Adleran argument to the option may be presented
137a35a7e76SEric Melville.El
138a35a7e76SEric Melville.Pp
139a35a7e76SEric MelvilleIf
140187f61dfSRuslan Ermilov.Va flag
141187f61dfSRuslan Ermilovis not
142187f61dfSRuslan Ermilov.Dv NULL ,
143187f61dfSRuslan Ermilovthen the integer pointed to by it will be set to the
144a35a7e76SEric Melvillevalue in the
145187f61dfSRuslan Ermilov.Va val
146187f61dfSRuslan Ermilovfield.
147187f61dfSRuslan ErmilovIf the
148187f61dfSRuslan Ermilov.Va flag
149187f61dfSRuslan Ermilovfield is
150187f61dfSRuslan Ermilov.Dv NULL ,
151187f61dfSRuslan Ermilovthen the
152187f61dfSRuslan Ermilov.Va val
153187f61dfSRuslan Ermilovfield will be returned.
154187f61dfSRuslan ErmilovSetting
155187f61dfSRuslan Ermilov.Va flag
156187f61dfSRuslan Ermilovto
157187f61dfSRuslan Ermilov.Dv NULL
158187f61dfSRuslan Ermilovand setting
159187f61dfSRuslan Ermilov.Va val
160a35a7e76SEric Melvilleto the corresponding short option will make this function act just
161a35a7e76SEric Melvillelike
162a35a7e76SEric Melville.Xr getopt 3 .
163829a229dSAndrey A. Chernov.Pp
164829a229dSAndrey A. ChernovIf the
165829a229dSAndrey A. Chernov.Fa longindex
166829a229dSAndrey A. Chernovfield is not
167829a229dSAndrey A. Chernov.Dv NULL ,
168829a229dSAndrey A. Chernovthen the integer pointed to by it will be set to the index of the long
169829a229dSAndrey A. Chernovoption relative to
170829a229dSAndrey A. Chernov.Fa longopts .
171829a229dSAndrey A. Chernov.Pp
172829a229dSAndrey A. ChernovThe last element of the
173829a229dSAndrey A. Chernov.Fa longopts
174829a229dSAndrey A. Chernovarray has to be filled with zeroes.
175829a229dSAndrey A. Chernov.Pp
176829a229dSAndrey A. ChernovThe
177829a229dSAndrey A. Chernov.Fn getopt_long_only
178829a229dSAndrey A. Chernovfunction behaves identically to
179829a229dSAndrey A. Chernov.Fn getopt_long
180829a229dSAndrey A. Chernovwith the exception that long options may start with
181829a229dSAndrey A. Chernov.Ql -
182829a229dSAndrey A. Chernovin addition to
183829a229dSAndrey A. Chernov.Ql -- .
184829a229dSAndrey A. ChernovIf an option starting with
185829a229dSAndrey A. Chernov.Ql -
186829a229dSAndrey A. Chernovdoes not match a long option but does match a single-character option,
187829a229dSAndrey A. Chernovthe single-character option is returned.
188829a229dSAndrey A. Chernov.Sh RETURN VALUES
189829a229dSAndrey A. ChernovIf the
190829a229dSAndrey A. Chernov.Fa flag
191829a229dSAndrey A. Chernovfield in
192629a7369SRuslan Ermilov.Vt "struct option"
193829a229dSAndrey A. Chernovis
194829a229dSAndrey A. Chernov.Dv NULL ,
195829a229dSAndrey A. Chernov.Fn getopt_long
196829a229dSAndrey A. Chernovand
197829a229dSAndrey A. Chernov.Fn getopt_long_only
198829a229dSAndrey A. Chernovreturn the value specified in the
199829a229dSAndrey A. Chernov.Fa val
200829a229dSAndrey A. Chernovfield, which is usually just the corresponding short option.
201829a229dSAndrey A. ChernovIf
202829a229dSAndrey A. Chernov.Fa flag
203829a229dSAndrey A. Chernovis not
204829a229dSAndrey A. Chernov.Dv NULL ,
205829a229dSAndrey A. Chernovthese functions return 0 and store
206829a229dSAndrey A. Chernov.Fa val
207829a229dSAndrey A. Chernovin the location pointed to by
208829a229dSAndrey A. Chernov.Fa flag .
2093b890189SKyle Evans.Pp
210829a229dSAndrey A. ChernovThese functions return
211829a229dSAndrey A. Chernov.Ql \&:
2123b890189SKyle Evansif there was a missing option argument and error messages are suppressed,
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.
2163b890189SKyle EvansThe default behavior when a missing option argument is encountered is to write
2173b890189SKyle Evansan error and return
2183b890189SKyle Evans.Ql \&? .
2193b890189SKyle EvansSpecifying
2203b890189SKyle Evans.Ql \&:
2213b890189SKyle Evansin
2223b890189SKyle Evans.Fa optstr
2233b890189SKyle Evanswill cause the error message to be suppressed and
2243b890189SKyle Evans.Ql \&:
2253b890189SKyle Evansto be returned instead.
2263b890189SKyle Evans.Pp
2273b890189SKyle EvansIn addition to
2283b890189SKyle Evans.Ql \&: ,
2293b890189SKyle Evansa leading
2303b890189SKyle Evans.Ql \&+
2313b890189SKyle Evansor
2323b890189SKyle Evans.Ql \&-
2333b890189SKyle Evansin
2343b890189SKyle Evans.Fa optstr
2353b890189SKyle Evansalso has special meaning.
2363b890189SKyle EvansIf either of these are specified, they must appear before
2373b890189SKyle Evans.Ql \&: .
2383b890189SKyle Evans.Pp
2393b890189SKyle EvansA leading
2403b890189SKyle Evans.Ql \&+
2413b890189SKyle Evansindicates that processing should be halted at the first non-option argument,
2423b890189SKyle Evansmatching the default behavior of
2433b890189SKyle Evans.Xr getopt 3 .
2443b890189SKyle EvansThe default behavior without
2453b890189SKyle Evans.Ql \&+
246d50ff7deSBenedict Reuschlingis to permute non-option arguments to the end of
2473b890189SKyle Evans.Fa argv .
2483b890189SKyle Evans.Pp
2493b890189SKyle EvansA leading
2503b890189SKyle Evans.Ql \&-
2513b890189SKyle Evansindicates that all non-option arguments should be treated as if they are
2523b890189SKyle Evansarguments to a literal
2533b890189SKyle Evans.Ql \&1
2543b890189SKyle Evansflag (i.e., the function call will return the value 1, rather than the char
2553b890189SKyle Evansliteral '1').
25624a0682cSRuslan Ermilov.Sh ENVIRONMENT
25724a0682cSRuslan Ermilov.Bl -tag -width ".Ev POSIXLY_CORRECT"
25824a0682cSRuslan Ermilov.It Ev POSIXLY_CORRECT
25924a0682cSRuslan ErmilovIf set, option processing stops when the first non-option is found and
26024a0682cSRuslan Ermilova leading
26124a0682cSRuslan Ermilov.Ql -
26224a0682cSRuslan Ermilovor
26324a0682cSRuslan Ermilov.Ql +
26424a0682cSRuslan Ermilovin the
26524a0682cSRuslan Ermilov.Fa optstring
26624a0682cSRuslan Ermilovis ignored.
26724a0682cSRuslan Ermilov.El
268a35a7e76SEric Melville.Sh EXAMPLES
269a35a7e76SEric Melville.Bd -literal -compact
270a35a7e76SEric Melvilleint bflag, ch, fd;
271a35a7e76SEric Melvilleint daggerset;
272a35a7e76SEric Melville
273a35a7e76SEric Melville/* options descriptor */
274a35a7e76SEric Melvillestatic struct option longopts[] = {
275829a229dSAndrey A. Chernov	{ "buffy",	no_argument,		NULL, 		'b' },
276829a229dSAndrey A. Chernov	{ "fluoride",	required_argument,	NULL, 	       	'f' },
277a35a7e76SEric Melville	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
278829a229dSAndrey A. Chernov	{ NULL,		0,			NULL, 		0 }
279a35a7e76SEric Melville};
280a35a7e76SEric Melville
281a35a7e76SEric Melvillebflag = 0;
282bd45f567SGlen Barberwhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
283a35a7e76SEric Melville	switch (ch) {
284a35a7e76SEric Melville	case 'b':
285a35a7e76SEric Melville		bflag = 1;
286a35a7e76SEric Melville		break;
287a35a7e76SEric Melville	case 'f':
288829a229dSAndrey A. Chernov		if ((fd = open(optarg, O_RDONLY, 0)) == -1)
289829a229dSAndrey A. Chernov			err(1, "unable to open %s", optarg);
290a35a7e76SEric Melville		break;
291a35a7e76SEric Melville	case 0:
292a35a7e76SEric Melville		if (daggerset) {
293a35a7e76SEric Melville			fprintf(stderr,"Buffy will use her dagger to "
294829a229dSAndrey A. Chernov			    "apply fluoride to dracula's teeth\en");
295a35a7e76SEric Melville		}
296a35a7e76SEric Melville		break;
297a35a7e76SEric Melville	default:
298a35a7e76SEric Melville		usage();
299a35a7e76SEric Melville	}
300bd45f567SGlen Barber}
301a35a7e76SEric Melvilleargc -= optind;
302a35a7e76SEric Melvilleargv += optind;
303a35a7e76SEric Melville.Ed
304a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES
305187f61dfSRuslan ErmilovThis section describes differences to the
306187f61dfSRuslan Ermilov.Tn GNU
307187f61dfSRuslan Ermilovimplementation
308a35a7e76SEric Melvillefound in glibc-2.1.3:
309187f61dfSRuslan Ermilov.Bl -bullet
3108a4dd785SAndrey A. Chernov.\" .It
3118a4dd785SAndrey A. Chernov.\" Handling of
3128a4dd785SAndrey A. Chernov.\" .Ql -
3138a4dd785SAndrey A. Chernov.\" as first char of option string in presence of
3148a4dd785SAndrey A. Chernov.\" environment variable
3158a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
3168a4dd785SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3178a4dd785SAndrey A. Chernov.\" .It Tn GNU
3188a4dd785SAndrey A. Chernov.\" ignores
3198a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
3208a4dd785SAndrey A. Chernov.\" and returns non-options as
3218a4dd785SAndrey A. Chernov.\" arguments to option '\e1'.
3228a4dd785SAndrey A. Chernov.\" .It Bx
3238a4dd785SAndrey A. Chernov.\" honors
3248a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
3258a4dd785SAndrey A. Chernov.\" and stops at the first non-option.
3268a4dd785SAndrey A. Chernov.\" .El
3273700175bSAndrey A. Chernov.\" .It
3283700175bSAndrey A. Chernov.\" Handling of
3293700175bSAndrey A. Chernov.\" .Ql -
3303700175bSAndrey A. Chernov.\" within the option string (not the first character):
3313700175bSAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3323700175bSAndrey A. Chernov.\" .It Tn GNU
3333700175bSAndrey A. Chernov.\" treats a
3343700175bSAndrey A. Chernov.\" .Ql -
3353700175bSAndrey A. Chernov.\" on the command line as a non-argument.
3363700175bSAndrey A. Chernov.\" .It Bx
3373700175bSAndrey A. Chernov.\" a
3383700175bSAndrey A. Chernov.\" .Ql -
3393700175bSAndrey A. Chernov.\" within the option string matches a
3403700175bSAndrey A. Chernov.\" .Ql -
3413700175bSAndrey A. Chernov.\" (single dash) on the command line.
3423700175bSAndrey A. Chernov.\" This functionality is provided for backward compatibility with
3433700175bSAndrey A. Chernov.\" programs, such as
3443700175bSAndrey A. Chernov.\" .Xr su 1 ,
3453700175bSAndrey A. Chernov.\" that use
3463700175bSAndrey A. Chernov.\" .Ql -
3473700175bSAndrey A. Chernov.\" as an option flag.
3483700175bSAndrey A. Chernov.\" This practice is wrong, and should not be used in any current development.
3493700175bSAndrey A. Chernov.\" .El
350f2fd86b7SAndrey A. Chernov.\" .It
351f2fd86b7SAndrey A. Chernov.\" Handling of
352f2fd86b7SAndrey A. Chernov.\" .Ql ::
353f2fd86b7SAndrey A. Chernov.\" in options string in presence of
354f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
355f2fd86b7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
356f2fd86b7SAndrey A. Chernov.\" .It Both
357f2fd86b7SAndrey A. Chernov.\" .Tn GNU
358f2fd86b7SAndrey A. Chernov.\" and
359f2fd86b7SAndrey A. Chernov.\" .Bx
360f2fd86b7SAndrey A. Chernov.\" ignore
361f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
362f2fd86b7SAndrey A. Chernov.\" here and take
363f2fd86b7SAndrey A. Chernov.\" .Ql ::
364f2fd86b7SAndrey A. Chernov.\" to
365f2fd86b7SAndrey A. Chernov.\" mean the preceding option takes an optional argument.
366f2fd86b7SAndrey A. Chernov.\" .El
36788485399SAndrey A. Chernov.\" .It
36888485399SAndrey A. Chernov.\" Return value in case of missing argument if first character
36988485399SAndrey A. Chernov.\" (after
37088485399SAndrey A. Chernov.\" .Ql +
37188485399SAndrey A. Chernov.\" or
37288485399SAndrey A. Chernov.\" .Ql - )
37388485399SAndrey A. Chernov.\" in option string is not
37488485399SAndrey A. Chernov.\" .Ql \&: :
37588485399SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
37688485399SAndrey A. Chernov.\" .It Tn GNU
37788485399SAndrey A. Chernov.\" returns
37888485399SAndrey A. Chernov.\" .Ql \&?
37988485399SAndrey A. Chernov.\" .It Bx
38088485399SAndrey A. Chernov.\" returns
38188485399SAndrey A. Chernov.\" .Ql \&:
38288485399SAndrey A. Chernov.\" (since
38388485399SAndrey A. Chernov.\" .Bx Ns 's
38488485399SAndrey A. Chernov.\" .Fn getopt
38588485399SAndrey A. Chernov.\" does).
38688485399SAndrey A. Chernov.\" .El
3872298a6e7SAndrey A. Chernov.\" .It
3882298a6e7SAndrey A. Chernov.\" Handling of
3892298a6e7SAndrey A. Chernov.\" .Ql --a
3902298a6e7SAndrey A. Chernov.\" in getopt:
3912298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3922298a6e7SAndrey A. Chernov.\" .It Tn GNU
3932298a6e7SAndrey A. Chernov.\" parses this as option
3942298a6e7SAndrey A. Chernov.\" .Ql - ,
3952298a6e7SAndrey A. Chernov.\" option
3962298a6e7SAndrey A. Chernov.\" .Ql a .
3972298a6e7SAndrey A. Chernov.\" .It Bx
3982298a6e7SAndrey A. Chernov.\" parses this as
3992298a6e7SAndrey A. Chernov.\" .Ql -- ,
4002298a6e7SAndrey A. Chernov.\" and returns \-1 (ignoring the
4012298a6e7SAndrey A. Chernov.\" .Ql a ) .
4022298a6e7SAndrey A. Chernov.\" (Because the original
4032298a6e7SAndrey A. Chernov.\" .Fn getopt
4042298a6e7SAndrey A. Chernov.\" does.)
4052298a6e7SAndrey A. Chernov.\" .El
406187f61dfSRuslan Ermilov.It
407187f61dfSRuslan ErmilovSetting of
408187f61dfSRuslan Ermilov.Va optopt
409187f61dfSRuslan Ermilovfor long options with
410187f61dfSRuslan Ermilov.Va flag
411187f61dfSRuslan Ermilov!=
412187f61dfSRuslan Ermilov.Dv NULL :
413829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
414187f61dfSRuslan Ermilov.It Tn GNU
415187f61dfSRuslan Ermilovsets
416187f61dfSRuslan Ermilov.Va optopt
417187f61dfSRuslan Ermilovto
418187f61dfSRuslan Ermilov.Va val .
419829a229dSAndrey A. Chernov.It Bx
420187f61dfSRuslan Ermilovsets
421187f61dfSRuslan Ermilov.Va optopt
422187f61dfSRuslan Ermilovto 0 (since
423187f61dfSRuslan Ermilov.Va val
424187f61dfSRuslan Ermilovwould never be returned).
425a35a7e76SEric Melville.El
4262298a6e7SAndrey A. Chernov.\" .It
4272298a6e7SAndrey A. Chernov.\" Handling of
4282298a6e7SAndrey A. Chernov.\" .Ql -W
4292298a6e7SAndrey A. Chernov.\" with
4302298a6e7SAndrey A. Chernov.\" .Ql W;
4312298a6e7SAndrey A. Chernov.\" in option string in
4322298a6e7SAndrey A. Chernov.\" .Fn getopt
4332298a6e7SAndrey A. Chernov.\" (not
4342298a6e7SAndrey A. Chernov.\" .Fn getopt_long ) :
4352298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
4362298a6e7SAndrey A. Chernov.\" .It Tn GNU
4372298a6e7SAndrey A. Chernov.\" causes a segfault.
4382298a6e7SAndrey A. Chernov.\" .It Bx
4392298a6e7SAndrey A. Chernov.\" no special handling is done;
4402298a6e7SAndrey A. Chernov.\" .Ql W;
4412298a6e7SAndrey A. Chernov.\" is interpreted as two separate options, neither of which take an argument.
4422298a6e7SAndrey A. Chernov.\" .El
443187f61dfSRuslan Ermilov.It
444187f61dfSRuslan ErmilovSetting of
445187f61dfSRuslan Ermilov.Va optarg
446187f61dfSRuslan Ermilovfor long options without an argument that are
447187f61dfSRuslan Ermilovinvoked via
448187f61dfSRuslan Ermilov.Ql -W
449187f61dfSRuslan Ermilov.Ql ( W;
450187f61dfSRuslan Ermilovin option string):
451829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
452187f61dfSRuslan Ermilov.It Tn GNU
453187f61dfSRuslan Ermilovsets
454187f61dfSRuslan Ermilov.Va optarg
455187f61dfSRuslan Ermilovto the option name (the argument of
456187f61dfSRuslan Ermilov.Ql -W ) .
457829a229dSAndrey A. Chernov.It Bx
458187f61dfSRuslan Ermilovsets
459187f61dfSRuslan Ermilov.Va optarg
460187f61dfSRuslan Ermilovto
461187f61dfSRuslan Ermilov.Dv NULL
462187f61dfSRuslan Ermilov(the argument of the long option).
463a35a7e76SEric Melville.El
464187f61dfSRuslan Ermilov.It
465187f61dfSRuslan ErmilovHandling of
466187f61dfSRuslan Ermilov.Ql -W
467187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known
468187f61dfSRuslan Ermilovlong option
469187f61dfSRuslan Ermilov.Ql ( W;
470187f61dfSRuslan Ermilovin option string):
471829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
472187f61dfSRuslan Ermilov.It Tn GNU
473187f61dfSRuslan Ermilovreturns
474187f61dfSRuslan Ermilov.Ql -W
475187f61dfSRuslan Ermilovwith
476187f61dfSRuslan Ermilov.Va optarg
477187f61dfSRuslan Ermilovset to the unknown option.
478829a229dSAndrey A. Chernov.It Bx
479187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns
480187f61dfSRuslan Ermilov.Ql \&?
481187f61dfSRuslan Ermilovwith
482187f61dfSRuslan Ermilov.Va optopt
483187f61dfSRuslan Ermilovset to 0 and
484187f61dfSRuslan Ermilov.Va optarg
485187f61dfSRuslan Ermilovset to
486187f61dfSRuslan Ermilov.Dv NULL
487187f61dfSRuslan Ermilov(as
488187f61dfSRuslan Ermilov.Tn GNU Ns 's
489187f61dfSRuslan Ermilovman page documents).
490a35a7e76SEric Melville.El
491f2fd86b7SAndrey A. Chernov.\" .It
492f2fd86b7SAndrey A. Chernov.\" The error messages are different.
493187f61dfSRuslan Ermilov.It
494829a229dSAndrey A. Chernov.Bx
495187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in
496187f61dfSRuslan Ermilovthe calling sequence as
497187f61dfSRuslan Ermilov.Tn GNU
498187f61dfSRuslan Ermilovdoes.
499187f61dfSRuslan ErmilovThe aspects normally used by
500187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of
501187f61dfSRuslan Ermilov.Va optind
502187f61dfSRuslan Ermilovrelative
503187f61dfSRuslan Ermilovto current positions) are the same, though.
504187f61dfSRuslan Ermilov(We do fewer variable swaps.)
505a35a7e76SEric Melville.El
506a35a7e76SEric Melville.Sh SEE ALSO
507a35a7e76SEric Melville.Xr getopt 3
508a35a7e76SEric Melville.Sh HISTORY
509a35a7e76SEric MelvilleThe
510a35a7e76SEric Melville.Fn getopt_long
511829a229dSAndrey A. Chernovand
512829a229dSAndrey A. Chernov.Fn getopt_long_only
513c2025a76SJoel Dahlfunctions first appeared in the
514187f61dfSRuslan Ermilov.Tn GNU
515c2025a76SJoel Dahllibiberty library.
516187f61dfSRuslan ErmilovThe first
517829a229dSAndrey A. Chernov.Bx
518829a229dSAndrey A. Chernovimplementation of
519829a229dSAndrey A. Chernov.Fn getopt_long
520829a229dSAndrey A. Chernovappeared in
521829a229dSAndrey A. Chernov.Nx 1.5 ,
522829a229dSAndrey A. Chernovthe first
523829a229dSAndrey A. Chernov.Bx
524829a229dSAndrey A. Chernovimplementation of
525829a229dSAndrey A. Chernov.Fn getopt_long_only
526829a229dSAndrey A. Chernovin
527829a229dSAndrey A. Chernov.Ox 3.3 .
528829a229dSAndrey A. Chernov.Fx
529829a229dSAndrey A. Chernovfirst included
530829a229dSAndrey A. Chernov.Fn getopt_long
531829a229dSAndrey A. Chernovin
532829a229dSAndrey A. Chernov.Fx 5.0 ,
533829a229dSAndrey A. Chernov.Fn getopt_long_only
534829a229dSAndrey A. Chernovin
535829a229dSAndrey A. Chernov.Fx 5.2 .
536a35a7e76SEric Melville.Sh BUGS
537829a229dSAndrey A. ChernovThe
538629a7369SRuslan Ermilov.Fa argv
539829a229dSAndrey A. Chernovargument is not really
540629a7369SRuslan Ermilov.Vt const
541829a229dSAndrey A. Chernovas its elements may be permuted (unless
542829a229dSAndrey A. Chernov.Ev POSIXLY_CORRECT
543829a229dSAndrey A. Chernovis set).
544829a229dSAndrey A. Chernov.Pp
545187f61dfSRuslan ErmilovThe implementation can completely replace
546a35a7e76SEric Melville.Xr getopt 3 ,
547a35a7e76SEric Melvillebut right now we are using separate code.
548*d646513eSKyle Evans.Pp
549*d646513eSKyle Evans.Nm
550*d646513eSKyle Evansmakes the assumption that the first argument should always be skipped because
551*d646513eSKyle Evansit's typically the program name.
552*d646513eSKyle EvansAs a result, setting
553*d646513eSKyle Evans.Va optind
554*d646513eSKyle Evansto 0 will indicate that
555*d646513eSKyle Evans.Nm
556*d646513eSKyle Evansshould reset, and
557*d646513eSKyle Evans.Va optind
558*d646513eSKyle Evanswill be set to 1 in the process.
559*d646513eSKyle EvansThis behavior differs from
560*d646513eSKyle Evans.Xr getopt 3 ,
561*d646513eSKyle Evanswhich will handle an
562*d646513eSKyle Evans.Va optind
563*d646513eSKyle Evansvalue of 0 as expected and process the first element.
564