xref: /netbsd-src/lib/libc/stdlib/getopt_long.3 (revision b4852c3567bdc1c13cfb849248c5a43f3ed56363)
1*b4852c35Suwe.\"	$NetBSD: getopt_long.3,v 1.22 2022/01/04 19:36:16 uwe Exp $
262da59d7Smcr.\"
362da59d7Smcr.\" Copyright (c) 1988, 1991, 1993
462da59d7Smcr.\"	The Regents of the University of California.  All rights reserved.
562da59d7Smcr.\"
662da59d7Smcr.\" Redistribution and use in source and binary forms, with or without
762da59d7Smcr.\" modification, are permitted provided that the following conditions
862da59d7Smcr.\" are met:
962da59d7Smcr.\" 1. Redistributions of source code must retain the above copyright
1062da59d7Smcr.\"    notice, this list of conditions and the following disclaimer.
1162da59d7Smcr.\" 2. Redistributions in binary form must reproduce the above copyright
1262da59d7Smcr.\"    notice, this list of conditions and the following disclaimer in the
1362da59d7Smcr.\"    documentation and/or other materials provided with the distribution.
14eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1562da59d7Smcr.\"    may be used to endorse or promote products derived from this software
1662da59d7Smcr.\"    without specific prior written permission.
1762da59d7Smcr.\"
1862da59d7Smcr.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1962da59d7Smcr.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2062da59d7Smcr.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2162da59d7Smcr.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2262da59d7Smcr.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2362da59d7Smcr.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2462da59d7Smcr.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2562da59d7Smcr.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2662da59d7Smcr.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2762da59d7Smcr.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2862da59d7Smcr.\" SUCH DAMAGE.
2962da59d7Smcr.\"
3062da59d7Smcr.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
3162da59d7Smcr.\"
32dda6d8caSchristos.Dd December 9, 2018
3362da59d7Smcr.Dt GETOPT_LONG 3
34bfb2439eSchristos.Os
3562da59d7Smcr.Sh NAME
3662da59d7Smcr.Nm getopt_long
3762da59d7Smcr.Nd get long options from command line argument list
3862da59d7Smcr.Sh LIBRARY
3962da59d7Smcr.Lb libc
4062da59d7Smcr.Sh SYNOPSIS
41472351e1Swiz.In getopt.h
4262da59d7Smcr.Ft int
437224809dSwiz.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "struct option *long_options" "int *index"
4462da59d7Smcr.Sh DESCRIPTION
4562da59d7SmcrThe
4662da59d7Smcr.Fn getopt_long
4762da59d7Smcrfunction is similar to
48bfb2439eSchristos.Xr getopt 3
496569c5c5Swizbut it accepts options in two forms: words and characters.
506569c5c5SwizThe
5162da59d7Smcr.Fn getopt_long
528e21934bSwizfunction provides a superset of the functionality of
53bfb2439eSchristos.Xr getopt 3 .
5462da59d7Smcr.Fn getopt_long
556569c5c5Swizcan be used in two ways.
566569c5c5SwizIn the first way, every long option understood by the program has a
576569c5c5Swizcorresponding short option, and the option structure is only used to
586569c5c5Swiztranslate from long options to short options.
596569c5c5SwizWhen used in this fashion,
6062da59d7Smcr.Fn getopt_long
6162da59d7Smcrbehaves identically to
62bfb2439eSchristos.Xr getopt 3 .
633492cfddSwizThis is a good way to add long option processing to an existing program
6462da59d7Smcrwith the minimum of rewriting.
6562da59d7Smcr.Pp
663492cfddSwizIn the second mechanism, a long option sets a flag in the
6762da59d7Smcr.Fa option
6862da59d7Smcrstructure passed, or will store a pointer to the command line argument
6962da59d7Smcrin the
7062da59d7Smcr.Fa option
716569c5c5Swizstructure passed to it for options that take arguments.
726569c5c5SwizAdditionally, the long option's argument may be specified as a single
736569c5c5Swizargument with an equal sign, e.g.
7462da59d7Smcr.Bd -literal
7562da59d7Smcrmyprogram --myoption=somevalue
7662da59d7Smcr.Ed
7762da59d7Smcr.Pp
7862da59d7SmcrWhen a long option is processed the call to
7962da59d7Smcr.Fn getopt_long
806569c5c5Swizwill return 0.
816569c5c5SwizFor this reason, long option processing without
823492cfddSwizshortcuts is not backwards compatible with
83bfb2439eSchristos.Xr getopt 3 .
8462da59d7Smcr.Pp
8562da59d7SmcrIt is possible to combine these methods, providing for long options
866569c5c5Swizprocessing with short option equivalents for some options.
876569c5c5SwizLess frequently used options would be processed as long options only.
8862da59d7Smcr.Pp
89a3ae2738SginsbachAbbreviated long option names are accepted when
90a3ae2738Sginsbach.Fn getopt_long
91a3ae2738Sginsbachprocesses long options if the abbreviation is unique.
92a3ae2738SginsbachAn exact match is always preferred for a defined long option.
93a3ae2738Sginsbach.Pp
9462da59d7SmcrThe
9562da59d7Smcr.Fn getopt_long
966569c5c5Swizcall requires a structure to be initialized describing the long options.
976569c5c5SwizThe structure is:
9862da59d7Smcr.Bd -literal
9962da59d7Smcrstruct option {
10062da59d7Smcr	char *name;
10162da59d7Smcr	int has_arg;
10262da59d7Smcr	int *flag;
10362da59d7Smcr	int val;
10462da59d7Smcr};
10562da59d7Smcr.Ed
10662da59d7Smcr.Pp
10762da59d7SmcrThe
10862da59d7Smcr.Fa name
10962da59d7Smcrfield should contain the option name without the leading double dash.
11062da59d7Smcr.Pp
11162da59d7SmcrThe
11262da59d7Smcr.Fa has_arg
113bfb2439eSchristosfield should be one of:
114bfb2439eSchristos.Bl -tag -width "optional_argument"
115bfb2439eSchristos.It Li no_argument
116dda6d8caSchristosno argument to the option is expected.
117bfb2439eSchristos.It Li required_argument
11862da59d7Smcran argument to the option is required.
119bfb2439eSchristos.It Li optional_argument
12062da59d7Smcran argument to the option may be presented.
12162da59d7Smcr.El
12262da59d7Smcr.Pp
12362da59d7SmcrIf
12462da59d7Smcr.Fa flag
1258e21934bSwizis not
1268e21934bSwiz.Dv NULL ,
1278e21934bSwizthen the integer pointed to by it will be set to the value in the
12862da59d7Smcr.Fa val
1296569c5c5Swizfield.
1306569c5c5SwizIf the
13162da59d7Smcr.Fa flag
1328e21934bSwizfield is
1338e21934bSwiz.Dv NULL ,
1348e21934bSwizthen the
13562da59d7Smcr.Fa val
1366569c5c5Swizfield will be returned.
1376569c5c5SwizSetting
13862da59d7Smcr.Fa flag
1398e21934bSwizto
1408e21934bSwiz.Dv NULL
1418e21934bSwizand setting
14262da59d7Smcr.Fa val
14362da59d7Smcrto the corresponding short option will make this function act just
14462da59d7Smcrlike
145bfb2439eSchristos.Xr getopt 3 .
1467224809dSwiz.Pp
1477224809dSwizIf the
1487224809dSwiz.Fa index
149*b4852c35Suweargument is not
1507224809dSwiz.Dv NULL ,
1517224809dSwizthe integer it points to will be set to the index of the long option
1527224809dSwizin the
1537224809dSwiz.Fa long_options
1547224809dSwizarray.
1557224809dSwiz.Pp
1567224809dSwizThe last element of the
1577224809dSwiz.Fa long_options
1587224809dSwizarray has to be filled with zeroes (see
1597224809dSwiz.Sx EXAMPLES
1607224809dSwizsection).
1617af3cfc2Swiz.Sh EXAMPLES
16262da59d7Smcr.Bd -literal -compact
16362da59d7Smcrextern char *optarg;
16462da59d7Smcrextern int optind;
16562da59d7Smcrint bflag, ch, fd;
16662da59d7Smcrint daggerset;
16762da59d7Smcr
16862da59d7Smcr/* options descriptor */
169bfb2439eSchristosstatic struct option longopts[] = {
170868269f7Suwe	{ "buffy",	no_argument,		NULL, 		'b' },
171868269f7Suwe	{ "fluoride",	required_argument,	NULL, 	       	'f' },
17201869ca4Swiz	{ "daggerset",	no_argument,		&daggerset,	1 },
1734f95ecd7Skleink	{ NULL,		0,			NULL, 		0 }
17462da59d7Smcr};
17562da59d7Smcr
17662da59d7Smcrbflag = 0;
1773492cfddSwizwhile ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
17862da59d7Smcr	switch (ch) {
17962da59d7Smcr	case 'b':
18062da59d7Smcr		bflag = 1;
18162da59d7Smcr		break;
18262da59d7Smcr	case 'f':
18301869ca4Swiz		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
18462da59d7Smcr			(void)fprintf(stderr,
18562da59d7Smcr			    "myname: %s: %s\en", optarg, strerror(errno));
18662da59d7Smcr			exit(1);
18762da59d7Smcr		}
18862da59d7Smcr		break;
18962da59d7Smcr	case 0:
19062da59d7Smcr		if(daggerset) {
1917af3cfc2Swiz			fprintf(stderr,"Buffy will use her dagger to "
192d072c175Smjl				       "apply fluoride to dracula's teeth\en");
19362da59d7Smcr		}
19462da59d7Smcr		break;
19562da59d7Smcr	case '?':
19662da59d7Smcr	default:
19762da59d7Smcr		usage();
19862da59d7Smcr}
19962da59d7Smcrargc -= optind;
20062da59d7Smcrargv += optind;
20162da59d7Smcr.Ed
202bfb2439eSchristos.Sh IMPLEMENTATION DIFFERENCES
2033492cfddSwizThis section describes differences to the GNU implementation
204bfb2439eSchristosfound in glibc-2.1.3:
205bfb2439eSchristos.Bl -tag -width "xxx"
206bfb2439eSchristos.It Li o
207bfb2439eSchristoshandling of - as first char of option string in presence of
208bfb2439eSchristosenvironment variable POSIXLY_CORRECT:
209bfb2439eSchristos.Bl -tag -width "NetBSD"
210bfb2439eSchristos.It Li GNU
211bfb2439eSchristosignores POSIXLY_CORRECT and returns non-options as
2123492cfddSwizarguments to option '\e1'.
213bfb2439eSchristos.It Li NetBSD
2143492cfddSwizhonors POSIXLY_CORRECT and stops at the first non-option.
215bfb2439eSchristos.El
216bfb2439eSchristos.It Li o
217bfb2439eSchristoshandling of :: in options string in presence of POSIXLY_CORRECT:
218bfb2439eSchristos.Bl -tag -width "NetBSD"
219bfb2439eSchristos.It Li Both
220bfb2439eSchristosGNU and NetBSD ignore POSIXLY_CORRECT here and take :: to
221bfb2439eSchristosmean the preceding option takes an optional argument.
222bfb2439eSchristos.El
223bfb2439eSchristos.It Li o
224bfb2439eSchristosreturn value in case of missing argument if first character
225bfb2439eSchristos(after + or -) in option string is not ':':
226bfb2439eSchristos.Bl -tag -width "NetBSD"
227bfb2439eSchristos.It Li GNU
228bfb2439eSchristosreturns '?'
229bfb2439eSchristos.It NetBSD
230bfb2439eSchristosreturns ':' (since NetBSD's getopt does).
231bfb2439eSchristos.El
232bfb2439eSchristos.It Li o
233bfb2439eSchristoshandling of --a in getopt:
234bfb2439eSchristos.Bl -tag -width "NetBSD"
235bfb2439eSchristos.It Li GNU
236bfb2439eSchristosparses this as option '-', option 'a'.
237bfb2439eSchristos.It Li NetBSD
2388e21934bSwizparses this as '--', and returns \-1 (ignoring the a).
2396569c5c5Swiz(Because the original getopt does.)
240bfb2439eSchristos.El
241bfb2439eSchristos.It Li o
2428e21934bSwizsetting of optopt for long options with flag !=
2438e21934bSwiz.Dv NULL :
244bfb2439eSchristos.Bl -tag -width "NetBSD"
245bfb2439eSchristos.It Li GNU
246bfb2439eSchristossets optopt to val.
247bfb2439eSchristos.It Li NetBSD
248bfb2439eSchristossets optopt to 0 (since val would never be returned).
249bfb2439eSchristos.El
250bfb2439eSchristos.It Li o
251bfb2439eSchristoshandling of -W with W; in option string in getopt (not getopt_long):
252bfb2439eSchristos.Bl -tag -width "NetBSD"
253bfb2439eSchristos.It Li GNU
254bfb2439eSchristoscauses a segfault.
255bfb2439eSchristos.It Li NetBSD
2568e21934bSwizreturns \-1, with optind pointing past the argument of -W
257bfb2439eSchristos(as if `-W arg' were `--arg', and thus '--' had been found).
258bfb2439eSchristos.\" How should we treat W; in the option string when called via
259bfb2439eSchristos.\" getopt?  Ignore the ';' or treat it as a ':'? Issue a warning?
260bfb2439eSchristos.El
261bfb2439eSchristos.It Li o
262bfb2439eSchristossetting of optarg for long options without an argument that are
263bfb2439eSchristosinvoked via -W (W; in option string):
264bfb2439eSchristos.Bl -tag -width "NetBSD"
265bfb2439eSchristos.It Li GNU
266bfb2439eSchristossets optarg to the option name (the argument of -W).
267bfb2439eSchristos.It Li NetBSD
2688e21934bSwizsets optarg to
2698e21934bSwiz.Dv NULL
2708e21934bSwiz(the argument of the long option).
271bfb2439eSchristos.El
272bfb2439eSchristos.It Li o
273bfb2439eSchristoshandling of -W with an argument that is not (a prefix to) a known
274bfb2439eSchristoslong option (W; in option string):
275bfb2439eSchristos.Bl -tag -width "NetBSD"
276bfb2439eSchristos.It Li GNU
277bfb2439eSchristosreturns -W with optarg set to the unknown option.
278bfb2439eSchristos.It Li NetBSD
2793492cfddSwiztreats this as an error (unknown option) and returns '?' with
2808e21934bSwizoptopt set to 0 and optarg set to
2818e21934bSwiz.Dv NULL
2828e21934bSwiz(as GNU's man page documents).
283bfb2439eSchristos.El
284bfb2439eSchristos.It Li o
285bfb2439eSchristosThe error messages are different.
286bfb2439eSchristos.It Li o
287bfb2439eSchristosNetBSD does not permute the argument vector at the same points in
2886569c5c5Swizthe calling sequence as GNU does.
2896569c5c5SwizThe aspects normally used by the caller
2908e21934bSwiz(ordering after \-1 is returned, value of optind relative
2916569c5c5Swizto current positions) are the same, though.
2926569c5c5Swiz(We do fewer variable swaps.)
293bfb2439eSchristos.El
2947af3cfc2Swiz.Sh SEE ALSO
2957af3cfc2Swiz.Xr getopt 3
2967af3cfc2Swiz.Sh HISTORY
2977af3cfc2SwizThe
2987af3cfc2Swiz.Fn getopt_long
2996569c5c5Swizfunction first appeared in GNU libiberty.
3006569c5c5SwizThe first
3017af3cfc2Swiz.Nx
3027af3cfc2Swizimplementation appeared in 1.5.
30362da59d7Smcr.Sh BUGS
3048e21934bSwizThe implementation can completely replace
305bfb2439eSchristos.Xr getopt 3 ,
306bfb2439eSchristosbut right now we are using separate code.
307a6a060b0Syamt.Pp
308a6a060b0SyamtThe
309a6a060b0Syamt.Fa argv
310a6a060b0Syamtargument is not really const.
311