xref: /freebsd/lib/libc/stdlib/getopt.3 (revision ae828962685b8ae9e5104ce8e7fe8b05effcb9ec)
158f0484fSRodney W. Grimes.\" Copyright (c) 1988, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
324381233dSPeter Wemm.\"     @(#)getopt.3	8.5 (Berkeley) 4/27/95
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
354381233dSPeter Wemm.Dd April 27, 1995
3658f0484fSRodney W. Grimes.Dt GETOPT 3
37a307d598SRuslan Ermilov.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm getopt
4058f0484fSRodney W. Grimes.Nd get option character from command line argument list
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
4358f0484fSRodney W. Grimes.Sh SYNOPSIS
448aefde06SJeroen Ruigrok van der Werven.In unistd.h
4558f0484fSRodney W. Grimes.Vt extern char *optarg ;
4658f0484fSRodney W. Grimes.Vt extern int   optind ;
4758f0484fSRodney W. Grimes.Vt extern int   optopt ;
4858f0484fSRodney W. Grimes.Vt extern int   opterr ;
4958f0484fSRodney W. Grimes.Vt extern int   optreset ;
5058f0484fSRodney W. Grimes.Ft int
5158f0484fSRodney W. Grimes.Fn getopt "int argc" "char * const *argv" "const char *optstring"
5258f0484fSRodney W. Grimes.Sh DESCRIPTION
5358f0484fSRodney W. GrimesThe
5458f0484fSRodney W. Grimes.Fn getopt
5558f0484fSRodney W. Grimesfunction incrementally parses a command line argument list
5658f0484fSRodney W. Grimes.Fa argv
5758f0484fSRodney W. Grimesand returns the next
5858f0484fSRodney W. Grimes.Em known
5958f0484fSRodney W. Grimesoption character.
6058f0484fSRodney W. GrimesAn option character is
6158f0484fSRodney W. Grimes.Em known
6258f0484fSRodney W. Grimesif it has been specified in the string of accepted option characters,
6358f0484fSRodney W. Grimes.Fa optstring .
6458f0484fSRodney W. Grimes.Pp
6558f0484fSRodney W. GrimesThe option string
6658f0484fSRodney W. Grimes.Fa optstring
6758f0484fSRodney W. Grimesmay contain the following elements: individual characters, and
6858f0484fSRodney W. Grimescharacters followed by a colon to indicate an option argument
6958f0484fSRodney W. Grimesis to follow.
7058f0484fSRodney W. GrimesFor example, an option string
7158f0484fSRodney W. Grimes.Li "\&""x""
7258f0484fSRodney W. Grimesrecognizes an option
7358f0484fSRodney W. Grimes.Dq Fl x ,
7458f0484fSRodney W. Grimesand an option string
7558f0484fSRodney W. Grimes.Li "\&""x:""
7658f0484fSRodney W. Grimesrecognizes an option and argument
7758f0484fSRodney W. Grimes.Dq Fl x Ar argument .
7858f0484fSRodney W. GrimesIt does not matter to
7958f0484fSRodney W. Grimes.Fn getopt
8058f0484fSRodney W. Grimesif a following argument has leading white space.
8158f0484fSRodney W. Grimes.Pp
8258f0484fSRodney W. GrimesOn return from
8358f0484fSRodney W. Grimes.Fn getopt ,
8458f0484fSRodney W. Grimes.Va optarg
8558f0484fSRodney W. Grimespoints to an option argument, if it is anticipated,
8658f0484fSRodney W. Grimesand the variable
8758f0484fSRodney W. Grimes.Va optind
8858f0484fSRodney W. Grimescontains the index to the next
8958f0484fSRodney W. Grimes.Fa argv
9058f0484fSRodney W. Grimesargument for a subsequent call
9158f0484fSRodney W. Grimesto
9258f0484fSRodney W. Grimes.Fn getopt .
9358f0484fSRodney W. GrimesThe variable
9458f0484fSRodney W. Grimes.Va optopt
9558f0484fSRodney W. Grimessaves the last
9658f0484fSRodney W. Grimes.Em known
9758f0484fSRodney W. Grimesoption character returned by
9858f0484fSRodney W. Grimes.Fn getopt .
9958f0484fSRodney W. Grimes.Pp
10058f0484fSRodney W. GrimesThe variable
10158f0484fSRodney W. Grimes.Va opterr
10258f0484fSRodney W. Grimesand
10358f0484fSRodney W. Grimes.Va optind
10458f0484fSRodney W. Grimesare both initialized to 1.
10558f0484fSRodney W. GrimesThe
10658f0484fSRodney W. Grimes.Va optind
10758f0484fSRodney W. Grimesvariable may be set to another value before a set of calls to
10858f0484fSRodney W. Grimes.Fn getopt
10958f0484fSRodney W. Grimesin order to skip over more or less argv entries.
11058f0484fSRodney W. Grimes.Pp
11158f0484fSRodney W. GrimesIn order to use
11258f0484fSRodney W. Grimes.Fn getopt
11358f0484fSRodney W. Grimesto evaluate multiple sets of arguments, or to evaluate a single set of
11458f0484fSRodney W. Grimesarguments multiple times,
11558f0484fSRodney W. Grimesthe variable
11658f0484fSRodney W. Grimes.Va optreset
11758f0484fSRodney W. Grimesmust be set to 1 before the second and each additional set of calls to
11858f0484fSRodney W. Grimes.Fn getopt ,
11958f0484fSRodney W. Grimesand the variable
12058f0484fSRodney W. Grimes.Va optind
12158f0484fSRodney W. Grimesmust be reinitialized.
12258f0484fSRodney W. Grimes.Pp
12358f0484fSRodney W. GrimesThe
12458f0484fSRodney W. Grimes.Fn getopt
12558f0484fSRodney W. Grimesfunction
1264381233dSPeter Wemmreturns \-1
1277b4e5796SMike Pritchardwhen the argument list is exhausted, or
12842635956SRuslan Ermilov.Ql ?\&
1297b4e5796SMike Pritchardif a non-recognized
13058f0484fSRodney W. Grimesoption is encountered.
131f25c63afSPhilippe CharnierThe interpretation of options in the argument list may be cancelled
13258f0484fSRodney W. Grimesby the option
13358f0484fSRodney W. Grimes.Ql --
13458f0484fSRodney W. Grimes(double dash) which causes
13558f0484fSRodney W. Grimes.Fn getopt
136a55fccb4SRobert Nordierto signal the end of argument processing and return \-1.
13758f0484fSRodney W. GrimesWhen all options have been processed (i.e., up to the first non-option
13858f0484fSRodney W. Grimesargument),
13958f0484fSRodney W. Grimes.Fn getopt
1404381233dSPeter Wemmreturns \-1.
14158f0484fSRodney W. Grimes.Sh DIAGNOSTICS
14258f0484fSRodney W. GrimesIf the
14358f0484fSRodney W. Grimes.Fn getopt
14458f0484fSRodney W. Grimesfunction encounters a character not found in the string
145fecae349SMatthew Hunt.Va optstring
14658f0484fSRodney W. Grimesor detects
1477b4e5796SMike Pritcharda missing option argument it writes an error message to the
148ae828962SRuslan Ermilov.Dv stderr
1497b4e5796SMike Pritchardand returns
15042635956SRuslan Ermilov.Ql ?\& .
15158f0484fSRodney W. GrimesSetting
15258f0484fSRodney W. Grimes.Va opterr
15358f0484fSRodney W. Grimesto a zero will disable these error messages.
15458f0484fSRodney W. GrimesIf
15558f0484fSRodney W. Grimes.Va optstring
15658f0484fSRodney W. Grimeshas a leading
15758f0484fSRodney W. Grimes.Ql \&:
15858f0484fSRodney W. Grimesthen a missing option argument causes a
15958f0484fSRodney W. Grimes.Ql \&:
16058f0484fSRodney W. Grimesto be returned in addition to suppressing any error messages.
16158f0484fSRodney W. Grimes.Pp
16258f0484fSRodney W. GrimesOption arguments are allowed to begin with
16358f0484fSRodney W. Grimes.Dq Li \- ;
16458f0484fSRodney W. Grimesthis is reasonable but
16558f0484fSRodney W. Grimesreduces the amount of error checking possible.
16658f0484fSRodney W. Grimes.Sh EXTENSIONS
16758f0484fSRodney W. GrimesThe
16858f0484fSRodney W. Grimes.Va optreset
16958f0484fSRodney W. Grimesvariable was added to make it possible to call the
17058f0484fSRodney W. Grimes.Fn getopt
17158f0484fSRodney W. Grimesfunction multiple times.
17258f0484fSRodney W. GrimesThis is an extension to the
17358f0484fSRodney W. Grimes.St -p1003.2
17458f0484fSRodney W. Grimesspecification.
175251c176fSRuslan Ermilov.Sh EXAMPLES
17658f0484fSRodney W. Grimes.Bd -literal -compact
17758f0484fSRodney W. Grimesint bflag, ch, fd;
17858f0484fSRodney W. Grimes
17958f0484fSRodney W. Grimesbflag = 0;
1804381233dSPeter Wemmwhile ((ch = getopt(argc, argv, "bf:")) != -1)
18158f0484fSRodney W. Grimes	switch (ch) {
18258f0484fSRodney W. Grimes	case 'b':
18358f0484fSRodney W. Grimes		bflag = 1;
18458f0484fSRodney W. Grimes		break;
18558f0484fSRodney W. Grimes	case 'f':
186f25c63afSPhilippe Charnier		if ((fd = open(optarg, O_RDONLY, 0)) < 0)
187f25c63afSPhilippe Charnier			err(1, "%s", optarg);
18858f0484fSRodney W. Grimes		break;
18958f0484fSRodney W. Grimes	case '?':
19058f0484fSRodney W. Grimes	default:
19158f0484fSRodney W. Grimes		usage();
19258f0484fSRodney W. Grimes	}
19358f0484fSRodney W. Grimesargc -= optind;
19458f0484fSRodney W. Grimesargv += optind;
19558f0484fSRodney W. Grimes.Ed
19658f0484fSRodney W. Grimes.Sh HISTORY
19758f0484fSRodney W. GrimesThe
19858f0484fSRodney W. Grimes.Fn getopt
199a8b4fa4aSKris Kennawayfunction appeared in
20058f0484fSRodney W. Grimes.Bx 4.3 .
20158f0484fSRodney W. Grimes.Sh BUGS
2024381233dSPeter WemmThe
2034381233dSPeter Wemm.Fn getopt
2044381233dSPeter Wemmfunction was once specified to return
2054381233dSPeter Wemm.Dv EOF
2064381233dSPeter Wemminstead of \-1.
2074381233dSPeter WemmThis was changed by
2084381233dSPeter Wemm.St -p1003.2-92
2094381233dSPeter Wemmto decouple
2104381233dSPeter Wemm.Fn getopt
2114381233dSPeter Wemmfrom
2124381233dSPeter Wemm.Pa <stdio.h> .
2134381233dSPeter Wemm.Pp
21458f0484fSRodney W. GrimesA single dash
21558f0484fSRodney W. Grimes.Dq Li -
2164c204da0SMasafumi Max NAKANEmay be specified as a character in
21758f0484fSRodney W. Grimes.Fa optstring ,
21858f0484fSRodney W. Grimeshowever it should
21958f0484fSRodney W. Grimes.Em never
22058f0484fSRodney W. Grimeshave an argument associated with it.
22158f0484fSRodney W. GrimesThis allows
22258f0484fSRodney W. Grimes.Fn getopt
22358f0484fSRodney W. Grimesto be used with programs that expect
22458f0484fSRodney W. Grimes.Dq Li -
22558f0484fSRodney W. Grimesas an option flag.
22658f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development.
22758f0484fSRodney W. GrimesIt is provided for backward compatibility
22858f0484fSRodney W. Grimes.Em only .
22958f0484fSRodney W. GrimesBy default, a single dash causes
23058f0484fSRodney W. Grimes.Fn getopt
2314381233dSPeter Wemmto return \-1.
23258f0484fSRodney W. GrimesThis is, we believe, compatible with System V.
23358f0484fSRodney W. Grimes.Pp
23458f0484fSRodney W. GrimesIt is also possible to handle digits as option letters.
23558f0484fSRodney W. GrimesThis allows
23658f0484fSRodney W. Grimes.Fn getopt
23758f0484fSRodney W. Grimesto be used with programs that expect a number
23858f0484fSRodney W. Grimes.Pq Dq Li \&-\&3
23958f0484fSRodney W. Grimesas an option.
24058f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development.
24158f0484fSRodney W. GrimesIt is provided for backward compatibility
24258f0484fSRodney W. Grimes.Em only .
243838fb327STim VanderhoekThe following code fragment works in most (but not all) cases.
24458f0484fSRodney W. Grimes.Bd -literal -offset indent
24558f0484fSRodney W. Grimesint length;
246838fb327STim Vanderhoekchar *p, *ep;
24758f0484fSRodney W. Grimes
248a55fccb4SRobert Nordierwhile ((ch = getopt(argc, argv, "0123456789")) != -1)
249a55fccb4SRobert Nordier	switch (ch) {
25058f0484fSRodney W. Grimes	case '0': case '1': case '2': case '3': case '4':
25158f0484fSRodney W. Grimes	case '5': case '6': case '7': case '8': case '9':
25258f0484fSRodney W. Grimes		p = argv[optind - 1];
25358f0484fSRodney W. Grimes		if (p[0] == '-' && p[1] == ch && !p[2])
254838fb327STim Vanderhoek			length = strtol(++p, &ep, 10);
255838fb327STim Vanderhoek		else if (argv[optind] && argv[optind][1] == ch) {
256838fb327STim Vanderhoek			length = strtol((p = argv[optind] + 1),
257838fb327STim Vanderhoek			    &ep, 10);
258838fb327STim Vanderhoek			optind++;
259838fb327STim Vanderhoek			optreset = 1;
260838fb327STim Vanderhoek		} else
261838fb327STim Vanderhoek			usage();
262838fb327STim Vanderhoek		if (*ep != '\0')
263838fb327STim Vanderhoek			errx(EX_USAGE, "illegal number -- %s", p);
26458f0484fSRodney W. Grimes		break;
26558f0484fSRodney W. Grimes	}
26658f0484fSRodney W. Grimes.Ed
267