xref: /csrg-svn/lib/libc/stdlib/getopt.3 (revision 54707)
148349Scael.\" Copyright (c) 1988, 1991 Regents of the University of California.
233992Sbostic.\" All rights reserved.
320617Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520617Smckusick.\"
6*54707Smarc.\"     @(#)getopt.3	6.17 (Berkeley) 07/06/92
733992Sbostic.\"
848349Scael.Dd
948349Scael.Dt GETOPT 3
1048349Scael.Os BSD 4.3
1148349Scael.Sh NAME
1248349Scael.Nm getopt
1348349Scael.Nd get option letter from argv
1448349Scael.Sh SYNOPSIS
1548349Scael.Fd #include <stdlib.h>
1648349Scael.Vt extern char *optarg
1748349Scael.Vt extern int   optind
1848349Scael.Vt extern int   opterr
19*54707Smarc.Vt extern int   optreset
2048349Scael.Ft int
2148349Scael.Fn getopt "int argc" "char * const *argv" "const char *optstring"
2248349Scael.Sh DESCRIPTION
2348349ScaelThe
2448349Scael.Fn getopt
2548349Scaelfunction gets
2648349Scaelthe next
2748349Scael.Em known
2848349Scaeloption character from
2948349Scael.Fa argv .
3048349ScaelAn option character is
3148349Scael.Em known
3248349Scaelif it has been specified in the string of accepted option characters,
3348349Scael.Fa optstring .
3448349Scael.Pp
3548349ScaelThe option string
3648349Scael.Fa optstring
3748349Scaelmay contain the following characters; letters and
3848349Scaelletters followed by a colon to indicate an option argument
39*54707Smarcis to follow.
40*54707SmarcIt does not matter to the function
4148349Scael.Fn getopt
4248349Scaelif a following argument has leading white space.
4348349Scael.Pp
4439478SbosticOn return from
4548349Scael.Fn getopt ,
4648349Scael.Va optarg
4748349Scaelpoints to an option argument, if it is anticipated,
4848349Scaeland the variable
4948349Scael.Va optind
5048349Scaelcontains the index to the next
5148349Scael.Fa argv
5248349Scaelargument for a subsequent call
5348349Scaelto
5448349Scael.Fn getopt .
5548349Scael.Pp
5648349ScaelThe variable
5748349Scael.Va opterr
5839478Sbosticand
5948349Scael.Va optind
6039478Sbosticare both initialized to 1.
61*54707SmarcThe
62*54707Smarc.Va optind
63*54707Smarcvariable may be set to another value before a set of calls to
64*54707Smarc.Fn getopt
65*54707Smarcin order to skip over more or less argv entries.
66*54707Smarc.Pp
6742262SbosticIn order to use
6848349Scael.Fn getopt
6942262Sbosticto evaluate multiple sets of arguments, or to evaluate a single set of
7042262Sbosticarguments multiple times,
71*54707Smarcthe variable
72*54707Smarc.Va optreset
73*54707Smarcmust be set to 1 before the second and each additional set of calls to
74*54707Smarc.Fn getopt ,
75*54707Smarcand the variable
7648349Scael.Va optind
77*54707Smarcmust be reinitialized.
7848349Scael.Pp
7948349ScaelThe
8048349Scael.Fn getopt
8148349Scaelfunction
8248349Scaelreturns an
8348349Scael.Dv EOF
8448349Scaelwhen the argument list is exhausted, or a non-recognized
8548349Scaeloption is encountered.
8648349ScaelThe interpretation of options in the argument list may be cancelled
8748349Scaelby the option
8848349Scael.Ql --
8948349Scael(double dash) which causes
9048349Scael.Fn getopt
9148349Scaelto signal the end of argument processing and return an
9248349Scael.Dv EOF .
9339478SbosticWhen all options have been processed (i.e., up to the first non-option
9439478Sbosticargument),
9548349Scael.Fn getopt
9648349Scaelreturns
9748349Scael.Dv EOF .
9848349Scael.Sh DIAGNOSTICS
9948349ScaelIf the
10048349Scael.Fn getopt
10148349Scaelfunction encounters a character not found in the string
10248349Scael.Va optarg
10348349Scaelor detects
104*54707Smarca missing option argument it writes an error message and returns
10548349Scael.Ql ?
10648349Scaelto the
10748349Scael.Em stderr .
10839478SbosticSetting
10948349Scael.Va opterr
11039478Sbosticto a zero will disable these error messages.
111*54707SmarcIf
112*54707Smarc.Va optstring
113*54707Smarchas a leading
114*54707Smarc.Ql \&:
115*54707Smarcthen then a missing option argumet causes a
116*54707Smarc.Ql \&:
117*54707Smarcto be returned in addition to supressing any error messages.
118*54707Smarcoption argument
119*54707Smarc.Pp
120*54707SmarcOption arguments are allowed to begin with
121*54707Smarc.Dq Li \- ;
122*54707Smarcthis is reasonable but
123*54707Smarcreduces the amount of error checking possible.
124*54707Smarc.Sh EXTENSIONS
125*54707SmarcThe
126*54707Smarc.Va optreset
127*54707Smarcvariable was added to make it possible to call the
128*54707Smarc.Fn getopt
129*54707Smarcfunction multiple times.
130*54707SmarcThis is an extension to the
131*54707Smarc.St -p1003.2
132*54707Smarcspecification.
13348349Scael.Sh EXAMPLE
13448349Scael.Bd -literal -compact
13539478Sbosticextern char *optarg;
13639478Sbosticextern int optind;
13739478Sbosticint bflag, ch, fd;
13839478Sbostic
13939478Sbosticbflag = 0;
14039478Sbosticwhile ((ch = getopt(argc, argv, "bf:")) != EOF)
14139478Sbostic	switch(ch) {
14239478Sbostic	case 'b':
14339478Sbostic		bflag = 1;
14439478Sbostic		break;
14539478Sbostic	case 'f':
14639478Sbostic		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
14739478Sbostic			(void)fprintf(stderr,
148*54707Smarc			    "myname: %s: %s\en", optarg, strerror(errno));
149*54707Smarc			exit(1);
15020617Smckusick		}
15139478Sbostic		break;
15239478Sbostic	case '?':
15339478Sbostic	default:
15439478Sbostic		usage();
15548349Scael}
15639478Sbosticargc -= optind;
15739478Sbosticargv += optind;
15848349Scael.Ed
15948349Scael.Sh HISTORY
16048349ScaelThe
16148349Scael.Fn getopt
16248349Scaelfunction appeared
16348349Scael.Bx 4.3 .
16448349Scael.Sh BUGS
16548349ScaelA single dash
16648349Scael.Dq Li -
16748349Scaelmay be specified as an character in
16848349Scael.Fa optstring ,
16939478Sbostichowever it should
17048349Scael.Em never
17139478Sbostichave an argument associated with it.
17239478SbosticThis allows
17348349Scael.Fn getopt
17448349Scaelto be used with programs that expect
17548349Scael.Dq Li -
17648349Scaelas an option flag.
17739478SbosticThis practice is wrong, and should not be used in any current development.
17839478SbosticIt is provided for backward compatibility
17948349Scael.Em only .
18039712SbosticBy default, a single dash causes
18148349Scael.Fn getopt
18248349Scaelto return
18348349Scael.Dv EOF .
18439712SbosticThis is, we believe, compatible with System V.
18548349Scael.Pp
18639478SbosticIt is also possible to handle digits as option letters.
18739478SbosticThis allows
18848349Scael.Fn getopt
18948349Scaelto be used with programs that expect a number
19048349Scael.Pq Dq Li \&-\&3
19148349Scaelas an option.
19239478SbosticThis practice is wrong, and should not be used in any current development.
19339478SbosticIt is provided for backward compatibility
19448349Scael.Em only .
195*54707SmarcThe following code fragment works in most cases.
19648349Scael.Bd -literal -offset indent
19739478Sbosticint length;
19839478Sbosticchar *p;
19935982Sbostic
20039478Sbosticwhile ((c = getopt(argc, argv, "0123456789")) != EOF)
20139478Sbostic	switch (c) {
20239478Sbostic	case '0': case '1': case '2': case '3': case '4':
20339478Sbostic	case '5': case '6': case '7': case '8': case '9':
20439478Sbostic		p = argv[optind - 1];
20539478Sbostic		if (p[0] == '-' && p[1] == ch && !p[2])
20639478Sbostic			length = atoi(++p);
20739478Sbostic		else
20839478Sbostic			length = atoi(argv[optind] + 1);
20939478Sbostic		break;
21035982Sbostic	}
21139478Sbostic}
21248349Scael.Ed
213