xref: /freebsd/lib/libc/stdlib/getopt.3 (revision 7b4e5796b921fa69629efd1c167c072d1343b69e)
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.\"
3258f0484fSRodney W. Grimes.\"     @(#)getopt.3	8.4 (Berkeley) 4/19/94
3358f0484fSRodney W. Grimes.\"
3458f0484fSRodney W. Grimes.Dd April 19, 1994
3558f0484fSRodney W. Grimes.Dt GETOPT 3
3658f0484fSRodney W. Grimes.Os BSD 4.3
3758f0484fSRodney W. Grimes.Sh NAME
3858f0484fSRodney W. Grimes.Nm getopt
3958f0484fSRodney W. Grimes.Nd get option character from command line argument list
4058f0484fSRodney W. Grimes.Sh SYNOPSIS
4158f0484fSRodney W. Grimes.Fd #include <stdlib.h>
4258f0484fSRodney W. Grimes.Vt extern char *optarg;
4358f0484fSRodney W. Grimes.Vt extern int   optind;
4458f0484fSRodney W. Grimes.Vt extern int   optopt;
4558f0484fSRodney W. Grimes.Vt extern int   opterr;
4658f0484fSRodney W. Grimes.Vt extern int   optreset;
4758f0484fSRodney W. Grimes.Ft int
4858f0484fSRodney W. Grimes.Fn getopt "int argc" "char * const *argv" "const char *optstring"
4958f0484fSRodney W. Grimes.Sh DESCRIPTION
5058f0484fSRodney W. GrimesThe
5158f0484fSRodney W. Grimes.Fn getopt
5258f0484fSRodney W. Grimesfunction incrementally parses a command line argument list
5358f0484fSRodney W. Grimes.Fa argv
5458f0484fSRodney W. Grimesand returns the next
5558f0484fSRodney W. Grimes.Em known
5658f0484fSRodney W. Grimesoption character.
5758f0484fSRodney W. GrimesAn option character is
5858f0484fSRodney W. Grimes.Em known
5958f0484fSRodney W. Grimesif it has been specified in the string of accepted option characters,
6058f0484fSRodney W. Grimes.Fa optstring .
6158f0484fSRodney W. Grimes.Pp
6258f0484fSRodney W. GrimesThe option string
6358f0484fSRodney W. Grimes.Fa optstring
6458f0484fSRodney W. Grimesmay contain the following elements: individual characters, and
6558f0484fSRodney W. Grimescharacters followed by a colon to indicate an option argument
6658f0484fSRodney W. Grimesis to follow.
6758f0484fSRodney W. GrimesFor example, an option string
6858f0484fSRodney W. Grimes.Li "\&""x""
6958f0484fSRodney W. Grimesrecognizes an option
7058f0484fSRodney W. Grimes.Dq Fl x ,
7158f0484fSRodney W. Grimesand an option string
7258f0484fSRodney W. Grimes.Li "\&""x:""
7358f0484fSRodney W. Grimesrecognizes an option and argument
7458f0484fSRodney W. Grimes.Dq Fl x Ar argument .
7558f0484fSRodney W. GrimesIt does not matter to
7658f0484fSRodney W. Grimes.Fn getopt
7758f0484fSRodney W. Grimesif a following argument has leading white space.
7858f0484fSRodney W. Grimes.Pp
7958f0484fSRodney W. GrimesOn return from
8058f0484fSRodney W. Grimes.Fn getopt ,
8158f0484fSRodney W. Grimes.Va optarg
8258f0484fSRodney W. Grimespoints to an option argument, if it is anticipated,
8358f0484fSRodney W. Grimesand the variable
8458f0484fSRodney W. Grimes.Va optind
8558f0484fSRodney W. Grimescontains the index to the next
8658f0484fSRodney W. Grimes.Fa argv
8758f0484fSRodney W. Grimesargument for a subsequent call
8858f0484fSRodney W. Grimesto
8958f0484fSRodney W. Grimes.Fn getopt .
9058f0484fSRodney W. GrimesThe variable
9158f0484fSRodney W. Grimes.Va optopt
9258f0484fSRodney W. Grimessaves the last
9358f0484fSRodney W. Grimes.Em known
9458f0484fSRodney W. Grimesoption character returned by
9558f0484fSRodney W. Grimes.Fn getopt .
9658f0484fSRodney W. Grimes.Pp
9758f0484fSRodney W. GrimesThe variable
9858f0484fSRodney W. Grimes.Va opterr
9958f0484fSRodney W. Grimesand
10058f0484fSRodney W. Grimes.Va optind
10158f0484fSRodney W. Grimesare both initialized to 1.
10258f0484fSRodney W. GrimesThe
10358f0484fSRodney W. Grimes.Va optind
10458f0484fSRodney W. Grimesvariable may be set to another value before a set of calls to
10558f0484fSRodney W. Grimes.Fn getopt
10658f0484fSRodney W. Grimesin order to skip over more or less argv entries.
10758f0484fSRodney W. Grimes.Pp
10858f0484fSRodney W. GrimesIn order to use
10958f0484fSRodney W. Grimes.Fn getopt
11058f0484fSRodney W. Grimesto evaluate multiple sets of arguments, or to evaluate a single set of
11158f0484fSRodney W. Grimesarguments multiple times,
11258f0484fSRodney W. Grimesthe variable
11358f0484fSRodney W. Grimes.Va optreset
11458f0484fSRodney W. Grimesmust be set to 1 before the second and each additional set of calls to
11558f0484fSRodney W. Grimes.Fn getopt ,
11658f0484fSRodney W. Grimesand the variable
11758f0484fSRodney W. Grimes.Va optind
11858f0484fSRodney W. Grimesmust be reinitialized.
11958f0484fSRodney W. Grimes.Pp
12058f0484fSRodney W. GrimesThe
12158f0484fSRodney W. Grimes.Fn getopt
12258f0484fSRodney W. Grimesfunction
12358f0484fSRodney W. Grimesreturns an
12458f0484fSRodney W. Grimes.Dv EOF
1257b4e5796SMike Pritchardwhen the argument list is exhausted, or
1267b4e5796SMike Pritchard.Ql ?
1277b4e5796SMike Pritchardif a non-recognized
12858f0484fSRodney W. Grimesoption is encountered.
129a5ed710cSMike PritchardThe interpretation of options in the argument list may be canceled
13058f0484fSRodney W. Grimesby the option
13158f0484fSRodney W. Grimes.Ql --
13258f0484fSRodney W. Grimes(double dash) which causes
13358f0484fSRodney W. Grimes.Fn getopt
13458f0484fSRodney W. Grimesto signal the end of argument processing and return an
13558f0484fSRodney W. Grimes.Dv EOF .
13658f0484fSRodney W. GrimesWhen all options have been processed (i.e., up to the first non-option
13758f0484fSRodney W. Grimesargument),
13858f0484fSRodney W. Grimes.Fn getopt
13958f0484fSRodney W. Grimesreturns
14058f0484fSRodney W. Grimes.Dv EOF .
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
14558f0484fSRodney W. Grimes.Va optarg
14658f0484fSRodney W. Grimesor detects
1477b4e5796SMike Pritcharda missing option argument it writes an error message to the
1487b4e5796SMike Pritchard.Em stderr
1497b4e5796SMike Pritchardand returns
1507b4e5796SMike Pritchard.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.
17558f0484fSRodney W. Grimes.Sh EXAMPLE
17658f0484fSRodney W. Grimes.Bd -literal -compact
17758f0484fSRodney W. Grimesextern char *optarg;
17858f0484fSRodney W. Grimesextern int optind;
17958f0484fSRodney W. Grimesint bflag, ch, fd;
18058f0484fSRodney W. Grimes
18158f0484fSRodney W. Grimesbflag = 0;
18258f0484fSRodney W. Grimeswhile ((ch = getopt(argc, argv, "bf:")) != EOF)
18358f0484fSRodney W. Grimes	switch(ch) {
18458f0484fSRodney W. Grimes	case 'b':
18558f0484fSRodney W. Grimes		bflag = 1;
18658f0484fSRodney W. Grimes		break;
18758f0484fSRodney W. Grimes	case 'f':
18858f0484fSRodney W. Grimes		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
18958f0484fSRodney W. Grimes			(void)fprintf(stderr,
19058f0484fSRodney W. Grimes			    "myname: %s: %s\en", optarg, strerror(errno));
19158f0484fSRodney W. Grimes			exit(1);
19258f0484fSRodney W. Grimes		}
19358f0484fSRodney W. Grimes		break;
19458f0484fSRodney W. Grimes	case '?':
19558f0484fSRodney W. Grimes	default:
19658f0484fSRodney W. Grimes		usage();
19758f0484fSRodney W. Grimes}
19858f0484fSRodney W. Grimesargc -= optind;
19958f0484fSRodney W. Grimesargv += optind;
20058f0484fSRodney W. Grimes.Ed
20158f0484fSRodney W. Grimes.Sh HISTORY
20258f0484fSRodney W. GrimesThe
20358f0484fSRodney W. Grimes.Fn getopt
20458f0484fSRodney W. Grimesfunction appeared
20558f0484fSRodney W. Grimes.Bx 4.3 .
20658f0484fSRodney W. Grimes.Sh BUGS
20758f0484fSRodney W. GrimesA single dash
20858f0484fSRodney W. Grimes.Dq Li -
20958f0484fSRodney W. Grimesmay be specified as an character in
21058f0484fSRodney W. Grimes.Fa optstring ,
21158f0484fSRodney W. Grimeshowever it should
21258f0484fSRodney W. Grimes.Em never
21358f0484fSRodney W. Grimeshave an argument associated with it.
21458f0484fSRodney W. GrimesThis allows
21558f0484fSRodney W. Grimes.Fn getopt
21658f0484fSRodney W. Grimesto be used with programs that expect
21758f0484fSRodney W. Grimes.Dq Li -
21858f0484fSRodney W. Grimesas an option flag.
21958f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development.
22058f0484fSRodney W. GrimesIt is provided for backward compatibility
22158f0484fSRodney W. Grimes.Em only .
22258f0484fSRodney W. GrimesBy default, a single dash causes
22358f0484fSRodney W. Grimes.Fn getopt
22458f0484fSRodney W. Grimesto return
22558f0484fSRodney W. Grimes.Dv EOF .
22658f0484fSRodney W. GrimesThis is, we believe, compatible with System V.
22758f0484fSRodney W. Grimes.Pp
22858f0484fSRodney W. GrimesIt is also possible to handle digits as option letters.
22958f0484fSRodney W. GrimesThis allows
23058f0484fSRodney W. Grimes.Fn getopt
23158f0484fSRodney W. Grimesto be used with programs that expect a number
23258f0484fSRodney W. Grimes.Pq Dq Li \&-\&3
23358f0484fSRodney W. Grimesas an option.
23458f0484fSRodney W. GrimesThis practice is wrong, and should not be used in any current development.
23558f0484fSRodney W. GrimesIt is provided for backward compatibility
23658f0484fSRodney W. Grimes.Em only .
23758f0484fSRodney W. GrimesThe following code fragment works in most cases.
23858f0484fSRodney W. Grimes.Bd -literal -offset indent
23958f0484fSRodney W. Grimesint length;
24058f0484fSRodney W. Grimeschar *p;
24158f0484fSRodney W. Grimes
24258f0484fSRodney W. Grimeswhile ((c = getopt(argc, argv, "0123456789")) != EOF)
24358f0484fSRodney W. Grimes	switch (c) {
24458f0484fSRodney W. Grimes	case '0': case '1': case '2': case '3': case '4':
24558f0484fSRodney W. Grimes	case '5': case '6': case '7': case '8': case '9':
24658f0484fSRodney W. Grimes		p = argv[optind - 1];
24758f0484fSRodney W. Grimes		if (p[0] == '-' && p[1] == ch && !p[2])
24858f0484fSRodney W. Grimes			length = atoi(++p);
24958f0484fSRodney W. Grimes		else
25058f0484fSRodney W. Grimes			length = atoi(argv[optind] + 1);
25158f0484fSRodney W. Grimes		break;
25258f0484fSRodney W. Grimes	}
25358f0484fSRodney W. Grimes}
25458f0484fSRodney W. Grimes.Ed
255