xref: /freebsd/lib/libc/stdlib/getopt_long.3 (revision d646513e57c9d59a7c0cc27a4d41518110db4359)
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.\"
34*d646513eSKyle Evans.Dd December 24, 2022
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
1330603b75aSEitan Adlerno argument to the option is expected
134187f61dfSRuslan Ermilov.It Dv required_argument
135187f61dfSRuslan Ermilovan argument to the option is required
136629a7369SRuslan Ermilov.It Dv optional_argument
1370603b75aSEitan Adleran 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 .
2103b890189SKyle Evans.Pp
211829a229dSAndrey A. ChernovThese functions return
212829a229dSAndrey A. Chernov.Ql \&:
2133b890189SKyle Evansif there was a missing option argument and error messages are suppressed,
214829a229dSAndrey A. Chernov.Ql \&?
215829a229dSAndrey A. Chernovif the user specified an unknown or ambiguous option, and
216829a229dSAndrey A. Chernov\-1 when the argument list has been exhausted.
2173b890189SKyle EvansThe default behavior when a missing option argument is encountered is to write
2183b890189SKyle Evansan error and return
2193b890189SKyle Evans.Ql \&? .
2203b890189SKyle EvansSpecifying
2213b890189SKyle Evans.Ql \&:
2223b890189SKyle Evansin
2233b890189SKyle Evans.Fa optstr
2243b890189SKyle Evanswill cause the error message to be suppressed and
2253b890189SKyle Evans.Ql \&:
2263b890189SKyle Evansto be returned instead.
2273b890189SKyle Evans.Pp
2283b890189SKyle EvansIn addition to
2293b890189SKyle Evans.Ql \&: ,
2303b890189SKyle Evansa leading
2313b890189SKyle Evans.Ql \&+
2323b890189SKyle Evansor
2333b890189SKyle Evans.Ql \&-
2343b890189SKyle Evansin
2353b890189SKyle Evans.Fa optstr
2363b890189SKyle Evansalso has special meaning.
2373b890189SKyle EvansIf either of these are specified, they must appear before
2383b890189SKyle Evans.Ql \&: .
2393b890189SKyle Evans.Pp
2403b890189SKyle EvansA leading
2413b890189SKyle Evans.Ql \&+
2423b890189SKyle Evansindicates that processing should be halted at the first non-option argument,
2433b890189SKyle Evansmatching the default behavior of
2443b890189SKyle Evans.Xr getopt 3 .
2453b890189SKyle EvansThe default behavior without
2463b890189SKyle Evans.Ql \&+
247d50ff7deSBenedict Reuschlingis to permute non-option arguments to the end of
2483b890189SKyle Evans.Fa argv .
2493b890189SKyle Evans.Pp
2503b890189SKyle EvansA leading
2513b890189SKyle Evans.Ql \&-
2523b890189SKyle Evansindicates that all non-option arguments should be treated as if they are
2533b890189SKyle Evansarguments to a literal
2543b890189SKyle Evans.Ql \&1
2553b890189SKyle Evansflag (i.e., the function call will return the value 1, rather than the char
2563b890189SKyle Evansliteral '1').
25724a0682cSRuslan Ermilov.Sh ENVIRONMENT
25824a0682cSRuslan Ermilov.Bl -tag -width ".Ev POSIXLY_CORRECT"
25924a0682cSRuslan Ermilov.It Ev POSIXLY_CORRECT
26024a0682cSRuslan ErmilovIf set, option processing stops when the first non-option is found and
26124a0682cSRuslan Ermilova leading
26224a0682cSRuslan Ermilov.Ql -
26324a0682cSRuslan Ermilovor
26424a0682cSRuslan Ermilov.Ql +
26524a0682cSRuslan Ermilovin the
26624a0682cSRuslan Ermilov.Fa optstring
26724a0682cSRuslan Ermilovis ignored.
26824a0682cSRuslan Ermilov.El
269a35a7e76SEric Melville.Sh EXAMPLES
270a35a7e76SEric Melville.Bd -literal -compact
271a35a7e76SEric Melvilleint bflag, ch, fd;
272a35a7e76SEric Melvilleint daggerset;
273a35a7e76SEric Melville
274a35a7e76SEric Melville/* options descriptor */
275a35a7e76SEric Melvillestatic struct option longopts[] = {
276829a229dSAndrey A. Chernov	{ "buffy",	no_argument,		NULL, 		'b' },
277829a229dSAndrey A. Chernov	{ "fluoride",	required_argument,	NULL, 	       	'f' },
278a35a7e76SEric Melville	{ "daggerset",	no_argument,		\*[Am]daggerset,	1 },
279829a229dSAndrey A. Chernov	{ NULL,		0,			NULL, 		0 }
280a35a7e76SEric Melville};
281a35a7e76SEric Melville
282a35a7e76SEric Melvillebflag = 0;
283bd45f567SGlen Barberwhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
284a35a7e76SEric Melville	switch (ch) {
285a35a7e76SEric Melville	case 'b':
286a35a7e76SEric Melville		bflag = 1;
287a35a7e76SEric Melville		break;
288a35a7e76SEric Melville	case 'f':
289829a229dSAndrey A. Chernov		if ((fd = open(optarg, O_RDONLY, 0)) == -1)
290829a229dSAndrey A. Chernov			err(1, "unable to open %s", optarg);
291a35a7e76SEric Melville		break;
292a35a7e76SEric Melville	case 0:
293a35a7e76SEric Melville		if (daggerset) {
294a35a7e76SEric Melville			fprintf(stderr,"Buffy will use her dagger to "
295829a229dSAndrey A. Chernov			    "apply fluoride to dracula's teeth\en");
296a35a7e76SEric Melville		}
297a35a7e76SEric Melville		break;
298a35a7e76SEric Melville	default:
299a35a7e76SEric Melville		usage();
300a35a7e76SEric Melville	}
301bd45f567SGlen Barber}
302a35a7e76SEric Melvilleargc -= optind;
303a35a7e76SEric Melvilleargv += optind;
304a35a7e76SEric Melville.Ed
305a35a7e76SEric Melville.Sh IMPLEMENTATION DIFFERENCES
306187f61dfSRuslan ErmilovThis section describes differences to the
307187f61dfSRuslan Ermilov.Tn GNU
308187f61dfSRuslan Ermilovimplementation
309a35a7e76SEric Melvillefound in glibc-2.1.3:
310187f61dfSRuslan Ermilov.Bl -bullet
3118a4dd785SAndrey A. Chernov.\" .It
3128a4dd785SAndrey A. Chernov.\" Handling of
3138a4dd785SAndrey A. Chernov.\" .Ql -
3148a4dd785SAndrey A. Chernov.\" as first char of option string in presence of
3158a4dd785SAndrey A. Chernov.\" environment variable
3168a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
3178a4dd785SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3188a4dd785SAndrey A. Chernov.\" .It Tn GNU
3198a4dd785SAndrey A. Chernov.\" ignores
3208a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
3218a4dd785SAndrey A. Chernov.\" and returns non-options as
3228a4dd785SAndrey A. Chernov.\" arguments to option '\e1'.
3238a4dd785SAndrey A. Chernov.\" .It Bx
3248a4dd785SAndrey A. Chernov.\" honors
3258a4dd785SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
3268a4dd785SAndrey A. Chernov.\" and stops at the first non-option.
3278a4dd785SAndrey A. Chernov.\" .El
3283700175bSAndrey A. Chernov.\" .It
3293700175bSAndrey A. Chernov.\" Handling of
3303700175bSAndrey A. Chernov.\" .Ql -
3313700175bSAndrey A. Chernov.\" within the option string (not the first character):
3323700175bSAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3333700175bSAndrey A. Chernov.\" .It Tn GNU
3343700175bSAndrey A. Chernov.\" treats a
3353700175bSAndrey A. Chernov.\" .Ql -
3363700175bSAndrey A. Chernov.\" on the command line as a non-argument.
3373700175bSAndrey A. Chernov.\" .It Bx
3383700175bSAndrey A. Chernov.\" a
3393700175bSAndrey A. Chernov.\" .Ql -
3403700175bSAndrey A. Chernov.\" within the option string matches a
3413700175bSAndrey A. Chernov.\" .Ql -
3423700175bSAndrey A. Chernov.\" (single dash) on the command line.
3433700175bSAndrey A. Chernov.\" This functionality is provided for backward compatibility with
3443700175bSAndrey A. Chernov.\" programs, such as
3453700175bSAndrey A. Chernov.\" .Xr su 1 ,
3463700175bSAndrey A. Chernov.\" that use
3473700175bSAndrey A. Chernov.\" .Ql -
3483700175bSAndrey A. Chernov.\" as an option flag.
3493700175bSAndrey A. Chernov.\" This practice is wrong, and should not be used in any current development.
3503700175bSAndrey A. Chernov.\" .El
351f2fd86b7SAndrey A. Chernov.\" .It
352f2fd86b7SAndrey A. Chernov.\" Handling of
353f2fd86b7SAndrey A. Chernov.\" .Ql ::
354f2fd86b7SAndrey A. Chernov.\" in options string in presence of
355f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT :
356f2fd86b7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
357f2fd86b7SAndrey A. Chernov.\" .It Both
358f2fd86b7SAndrey A. Chernov.\" .Tn GNU
359f2fd86b7SAndrey A. Chernov.\" and
360f2fd86b7SAndrey A. Chernov.\" .Bx
361f2fd86b7SAndrey A. Chernov.\" ignore
362f2fd86b7SAndrey A. Chernov.\" .Ev POSIXLY_CORRECT
363f2fd86b7SAndrey A. Chernov.\" here and take
364f2fd86b7SAndrey A. Chernov.\" .Ql ::
365f2fd86b7SAndrey A. Chernov.\" to
366f2fd86b7SAndrey A. Chernov.\" mean the preceding option takes an optional argument.
367f2fd86b7SAndrey A. Chernov.\" .El
36888485399SAndrey A. Chernov.\" .It
36988485399SAndrey A. Chernov.\" Return value in case of missing argument if first character
37088485399SAndrey A. Chernov.\" (after
37188485399SAndrey A. Chernov.\" .Ql +
37288485399SAndrey A. Chernov.\" or
37388485399SAndrey A. Chernov.\" .Ql - )
37488485399SAndrey A. Chernov.\" in option string is not
37588485399SAndrey A. Chernov.\" .Ql \&: :
37688485399SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
37788485399SAndrey A. Chernov.\" .It Tn GNU
37888485399SAndrey A. Chernov.\" returns
37988485399SAndrey A. Chernov.\" .Ql \&?
38088485399SAndrey A. Chernov.\" .It Bx
38188485399SAndrey A. Chernov.\" returns
38288485399SAndrey A. Chernov.\" .Ql \&:
38388485399SAndrey A. Chernov.\" (since
38488485399SAndrey A. Chernov.\" .Bx Ns 's
38588485399SAndrey A. Chernov.\" .Fn getopt
38688485399SAndrey A. Chernov.\" does).
38788485399SAndrey A. Chernov.\" .El
3882298a6e7SAndrey A. Chernov.\" .It
3892298a6e7SAndrey A. Chernov.\" Handling of
3902298a6e7SAndrey A. Chernov.\" .Ql --a
3912298a6e7SAndrey A. Chernov.\" in getopt:
3922298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
3932298a6e7SAndrey A. Chernov.\" .It Tn GNU
3942298a6e7SAndrey A. Chernov.\" parses this as option
3952298a6e7SAndrey A. Chernov.\" .Ql - ,
3962298a6e7SAndrey A. Chernov.\" option
3972298a6e7SAndrey A. Chernov.\" .Ql a .
3982298a6e7SAndrey A. Chernov.\" .It Bx
3992298a6e7SAndrey A. Chernov.\" parses this as
4002298a6e7SAndrey A. Chernov.\" .Ql -- ,
4012298a6e7SAndrey A. Chernov.\" and returns \-1 (ignoring the
4022298a6e7SAndrey A. Chernov.\" .Ql a ) .
4032298a6e7SAndrey A. Chernov.\" (Because the original
4042298a6e7SAndrey A. Chernov.\" .Fn getopt
4052298a6e7SAndrey A. Chernov.\" does.)
4062298a6e7SAndrey A. Chernov.\" .El
407187f61dfSRuslan Ermilov.It
408187f61dfSRuslan ErmilovSetting of
409187f61dfSRuslan Ermilov.Va optopt
410187f61dfSRuslan Ermilovfor long options with
411187f61dfSRuslan Ermilov.Va flag
412187f61dfSRuslan Ermilov!=
413187f61dfSRuslan Ermilov.Dv NULL :
414829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
415187f61dfSRuslan Ermilov.It Tn GNU
416187f61dfSRuslan Ermilovsets
417187f61dfSRuslan Ermilov.Va optopt
418187f61dfSRuslan Ermilovto
419187f61dfSRuslan Ermilov.Va val .
420829a229dSAndrey A. Chernov.It Bx
421187f61dfSRuslan Ermilovsets
422187f61dfSRuslan Ermilov.Va optopt
423187f61dfSRuslan Ermilovto 0 (since
424187f61dfSRuslan Ermilov.Va val
425187f61dfSRuslan Ermilovwould never be returned).
426a35a7e76SEric Melville.El
4272298a6e7SAndrey A. Chernov.\" .It
4282298a6e7SAndrey A. Chernov.\" Handling of
4292298a6e7SAndrey A. Chernov.\" .Ql -W
4302298a6e7SAndrey A. Chernov.\" with
4312298a6e7SAndrey A. Chernov.\" .Ql W;
4322298a6e7SAndrey A. Chernov.\" in option string in
4332298a6e7SAndrey A. Chernov.\" .Fn getopt
4342298a6e7SAndrey A. Chernov.\" (not
4352298a6e7SAndrey A. Chernov.\" .Fn getopt_long ) :
4362298a6e7SAndrey A. Chernov.\" .Bl -tag -width ".Bx"
4372298a6e7SAndrey A. Chernov.\" .It Tn GNU
4382298a6e7SAndrey A. Chernov.\" causes a segfault.
4392298a6e7SAndrey A. Chernov.\" .It Bx
4402298a6e7SAndrey A. Chernov.\" no special handling is done;
4412298a6e7SAndrey A. Chernov.\" .Ql W;
4422298a6e7SAndrey A. Chernov.\" is interpreted as two separate options, neither of which take an argument.
4432298a6e7SAndrey A. Chernov.\" .El
444187f61dfSRuslan Ermilov.It
445187f61dfSRuslan ErmilovSetting of
446187f61dfSRuslan Ermilov.Va optarg
447187f61dfSRuslan Ermilovfor long options without an argument that are
448187f61dfSRuslan Ermilovinvoked via
449187f61dfSRuslan Ermilov.Ql -W
450187f61dfSRuslan Ermilov.Ql ( W;
451187f61dfSRuslan Ermilovin option string):
452829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
453187f61dfSRuslan Ermilov.It Tn GNU
454187f61dfSRuslan Ermilovsets
455187f61dfSRuslan Ermilov.Va optarg
456187f61dfSRuslan Ermilovto the option name (the argument of
457187f61dfSRuslan Ermilov.Ql -W ) .
458829a229dSAndrey A. Chernov.It Bx
459187f61dfSRuslan Ermilovsets
460187f61dfSRuslan Ermilov.Va optarg
461187f61dfSRuslan Ermilovto
462187f61dfSRuslan Ermilov.Dv NULL
463187f61dfSRuslan Ermilov(the argument of the long option).
464a35a7e76SEric Melville.El
465187f61dfSRuslan Ermilov.It
466187f61dfSRuslan ErmilovHandling of
467187f61dfSRuslan Ermilov.Ql -W
468187f61dfSRuslan Ermilovwith an argument that is not (a prefix to) a known
469187f61dfSRuslan Ermilovlong option
470187f61dfSRuslan Ermilov.Ql ( W;
471187f61dfSRuslan Ermilovin option string):
472829a229dSAndrey A. Chernov.Bl -tag -width ".Bx"
473187f61dfSRuslan Ermilov.It Tn GNU
474187f61dfSRuslan Ermilovreturns
475187f61dfSRuslan Ermilov.Ql -W
476187f61dfSRuslan Ermilovwith
477187f61dfSRuslan Ermilov.Va optarg
478187f61dfSRuslan Ermilovset to the unknown option.
479829a229dSAndrey A. Chernov.It Bx
480187f61dfSRuslan Ermilovtreats this as an error (unknown option) and returns
481187f61dfSRuslan Ermilov.Ql \&?
482187f61dfSRuslan Ermilovwith
483187f61dfSRuslan Ermilov.Va optopt
484187f61dfSRuslan Ermilovset to 0 and
485187f61dfSRuslan Ermilov.Va optarg
486187f61dfSRuslan Ermilovset to
487187f61dfSRuslan Ermilov.Dv NULL
488187f61dfSRuslan Ermilov(as
489187f61dfSRuslan Ermilov.Tn GNU Ns 's
490187f61dfSRuslan Ermilovman page documents).
491a35a7e76SEric Melville.El
492f2fd86b7SAndrey A. Chernov.\" .It
493f2fd86b7SAndrey A. Chernov.\" The error messages are different.
494187f61dfSRuslan Ermilov.It
495829a229dSAndrey A. Chernov.Bx
496187f61dfSRuslan Ermilovdoes not permute the argument vector at the same points in
497187f61dfSRuslan Ermilovthe calling sequence as
498187f61dfSRuslan Ermilov.Tn GNU
499187f61dfSRuslan Ermilovdoes.
500187f61dfSRuslan ErmilovThe aspects normally used by
501187f61dfSRuslan Ermilovthe caller (ordering after \-1 is returned, value of
502187f61dfSRuslan Ermilov.Va optind
503187f61dfSRuslan Ermilovrelative
504187f61dfSRuslan Ermilovto current positions) are the same, though.
505187f61dfSRuslan Ermilov(We do fewer variable swaps.)
506a35a7e76SEric Melville.El
507a35a7e76SEric Melville.Sh SEE ALSO
508a35a7e76SEric Melville.Xr getopt 3
509a35a7e76SEric Melville.Sh HISTORY
510a35a7e76SEric MelvilleThe
511a35a7e76SEric Melville.Fn getopt_long
512829a229dSAndrey A. Chernovand
513829a229dSAndrey A. Chernov.Fn getopt_long_only
514c2025a76SJoel Dahlfunctions first appeared in the
515187f61dfSRuslan Ermilov.Tn GNU
516c2025a76SJoel Dahllibiberty library.
517187f61dfSRuslan ErmilovThe first
518829a229dSAndrey A. Chernov.Bx
519829a229dSAndrey A. Chernovimplementation of
520829a229dSAndrey A. Chernov.Fn getopt_long
521829a229dSAndrey A. Chernovappeared in
522829a229dSAndrey A. Chernov.Nx 1.5 ,
523829a229dSAndrey A. Chernovthe first
524829a229dSAndrey A. Chernov.Bx
525829a229dSAndrey A. Chernovimplementation of
526829a229dSAndrey A. Chernov.Fn getopt_long_only
527829a229dSAndrey A. Chernovin
528829a229dSAndrey A. Chernov.Ox 3.3 .
529829a229dSAndrey A. Chernov.Fx
530829a229dSAndrey A. Chernovfirst included
531829a229dSAndrey A. Chernov.Fn getopt_long
532829a229dSAndrey A. Chernovin
533829a229dSAndrey A. Chernov.Fx 5.0 ,
534829a229dSAndrey A. Chernov.Fn getopt_long_only
535829a229dSAndrey A. Chernovin
536829a229dSAndrey A. Chernov.Fx 5.2 .
537a35a7e76SEric Melville.Sh BUGS
538829a229dSAndrey A. ChernovThe
539629a7369SRuslan Ermilov.Fa argv
540829a229dSAndrey A. Chernovargument is not really
541629a7369SRuslan Ermilov.Vt const
542829a229dSAndrey A. Chernovas its elements may be permuted (unless
543829a229dSAndrey A. Chernov.Ev POSIXLY_CORRECT
544829a229dSAndrey A. Chernovis set).
545829a229dSAndrey A. Chernov.Pp
546187f61dfSRuslan ErmilovThe implementation can completely replace
547a35a7e76SEric Melville.Xr getopt 3 ,
548a35a7e76SEric Melvillebut right now we are using separate code.
549*d646513eSKyle Evans.Pp
550*d646513eSKyle Evans.Nm
551*d646513eSKyle Evansmakes the assumption that the first argument should always be skipped because
552*d646513eSKyle Evansit's typically the program name.
553*d646513eSKyle EvansAs a result, setting
554*d646513eSKyle Evans.Va optind
555*d646513eSKyle Evansto 0 will indicate that
556*d646513eSKyle Evans.Nm
557*d646513eSKyle Evansshould reset, and
558*d646513eSKyle Evans.Va optind
559*d646513eSKyle Evanswill be set to 1 in the process.
560*d646513eSKyle EvansThis behavior differs from
561*d646513eSKyle Evans.Xr getopt 3 ,
562*d646513eSKyle Evanswhich will handle an
563*d646513eSKyle Evans.Va optind
564*d646513eSKyle Evansvalue of 0 as expected and process the first element.
565