xref: /csrg-svn/lib/libc/stdlib/getopt.3 (revision 68918)
161180Sbostic.\" Copyright (c) 1988, 1991, 1993
261180Sbostic.\"	The Regents of the University of California.  All rights reserved.
320617Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520617Smckusick.\"
6*68918Sbostic.\"     @(#)getopt.3	8.5 (Berkeley) 04/27/95
733992Sbostic.\"
848349Scael.Dd
948349Scael.Dt GETOPT 3
1048349Scael.Os BSD 4.3
1148349Scael.Sh NAME
1248349Scael.Nm getopt
1358389Sbostic.Nd get option character from command line argument list
1448349Scael.Sh SYNOPSIS
15*68918Sbostic.Fd #include <unistd.h>
1666455Sbostic.Vt extern char *optarg;
1766455Sbostic.Vt extern int   optind;
1866455Sbostic.Vt extern int   optopt;
1966455Sbostic.Vt extern int   opterr;
2066455Sbostic.Vt extern int   optreset;
2148349Scael.Ft int
2248349Scael.Fn getopt "int argc" "char * const *argv" "const char *optstring"
2348349Scael.Sh DESCRIPTION
2448349ScaelThe
2548349Scael.Fn getopt
2658389Sbosticfunction incrementally parses a command line argument list
2758389Sbostic.Fa argv
2858389Sbosticand returns the next
2948349Scael.Em known
3058389Sbosticoption character.
3148349ScaelAn option character is
3248349Scael.Em known
3348349Scaelif it has been specified in the string of accepted option characters,
3448349Scael.Fa optstring .
3548349Scael.Pp
3648349ScaelThe option string
3748349Scael.Fa optstring
3858389Sbosticmay contain the following elements: individual characters, and
3958389Sbosticcharacters followed by a colon to indicate an option argument
4054707Smarcis to follow.
4158389SbosticFor example, an option string
4258389Sbostic.Li "\&""x""
4358389Sbosticrecognizes an option
4458389Sbostic.Dq Fl x ,
4558389Sbosticand an option string
4658389Sbostic.Li "\&""x:""
4758389Sbosticrecognizes an option and argument
4858389Sbostic.Dq Fl x Ar argument .
4958389SbosticIt does not matter to
5048349Scael.Fn getopt
5148349Scaelif a following argument has leading white space.
5248349Scael.Pp
5339478SbosticOn return from
5448349Scael.Fn getopt ,
5548349Scael.Va optarg
5648349Scaelpoints to an option argument, if it is anticipated,
5748349Scaeland the variable
5848349Scael.Va optind
5948349Scaelcontains the index to the next
6048349Scael.Fa argv
6148349Scaelargument for a subsequent call
6248349Scaelto
6348349Scael.Fn getopt .
6458389SbosticThe variable
6558389Sbostic.Va optopt
6658389Sbosticsaves the last
6758389Sbostic.Em known
6858389Sbosticoption character returned by
6958389Sbostic.Fn getopt .
7048349Scael.Pp
7148349ScaelThe variable
7248349Scael.Va opterr
7339478Sbosticand
7448349Scael.Va optind
7539478Sbosticare both initialized to 1.
7654707SmarcThe
7754707Smarc.Va optind
7854707Smarcvariable may be set to another value before a set of calls to
7954707Smarc.Fn getopt
8054707Smarcin order to skip over more or less argv entries.
8154707Smarc.Pp
8242262SbosticIn order to use
8348349Scael.Fn getopt
8442262Sbosticto evaluate multiple sets of arguments, or to evaluate a single set of
8542262Sbosticarguments multiple times,
8654707Smarcthe variable
8754707Smarc.Va optreset
8854707Smarcmust be set to 1 before the second and each additional set of calls to
8954707Smarc.Fn getopt ,
9054707Smarcand the variable
9148349Scael.Va optind
9254707Smarcmust be reinitialized.
9348349Scael.Pp
9448349ScaelThe
9548349Scael.Fn getopt
9648349Scaelfunction
97*68918Sbosticreturns \-1
9848349Scaelwhen the argument list is exhausted, or a non-recognized
9948349Scaeloption is encountered.
10048349ScaelThe interpretation of options in the argument list may be cancelled
10148349Scaelby the option
10248349Scael.Ql --
10348349Scael(double dash) which causes
10448349Scael.Fn getopt
105*68918Sbosticto signal the end of argument processing and returns \-1.
10639478SbosticWhen all options have been processed (i.e., up to the first non-option
10739478Sbosticargument),
10848349Scael.Fn getopt
109*68918Sbosticreturns \-1.
11048349Scael.Sh DIAGNOSTICS
11148349ScaelIf the
11248349Scael.Fn getopt
11348349Scaelfunction encounters a character not found in the string
11448349Scael.Va optarg
11548349Scaelor detects
11654707Smarca missing option argument it writes an error message and returns
11748349Scael.Ql ?
11848349Scaelto the
11948349Scael.Em stderr .
12039478SbosticSetting
12148349Scael.Va opterr
12239478Sbosticto a zero will disable these error messages.
12354707SmarcIf
12454707Smarc.Va optstring
12554707Smarchas a leading
12654707Smarc.Ql \&:
12766946Sbosticthen a missing option argument causes a
12854707Smarc.Ql \&:
12966946Sbosticto be returned in addition to suppressing any error messages.
13054707Smarc.Pp
13154707SmarcOption arguments are allowed to begin with
13254707Smarc.Dq Li \- ;
13354707Smarcthis is reasonable but
13454707Smarcreduces the amount of error checking possible.
13554707Smarc.Sh EXTENSIONS
13654707SmarcThe
13754707Smarc.Va optreset
13854707Smarcvariable was added to make it possible to call the
13954707Smarc.Fn getopt
14054707Smarcfunction multiple times.
14154707SmarcThis is an extension to the
14254707Smarc.St -p1003.2
14354707Smarcspecification.
14448349Scael.Sh EXAMPLE
14548349Scael.Bd -literal -compact
14639478Sbosticextern char *optarg;
14739478Sbosticextern int optind;
14839478Sbosticint bflag, ch, fd;
14939478Sbostic
15039478Sbosticbflag = 0;
151*68918Sbosticwhile ((ch = getopt(argc, argv, "bf:")) != -1)
15239478Sbostic	switch(ch) {
15339478Sbostic	case 'b':
15439478Sbostic		bflag = 1;
15539478Sbostic		break;
15639478Sbostic	case 'f':
15739478Sbostic		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
15839478Sbostic			(void)fprintf(stderr,
15954707Smarc			    "myname: %s: %s\en", optarg, strerror(errno));
16054707Smarc			exit(1);
16120617Smckusick		}
16239478Sbostic		break;
16339478Sbostic	case '?':
16439478Sbostic	default:
16539478Sbostic		usage();
16648349Scael}
16739478Sbosticargc -= optind;
16839478Sbosticargv += optind;
16948349Scael.Ed
17048349Scael.Sh HISTORY
17148349ScaelThe
17248349Scael.Fn getopt
17348349Scaelfunction appeared
17448349Scael.Bx 4.3 .
17548349Scael.Sh BUGS
176*68918SbosticThe
177*68918Sbostic.Fn getopt
178*68918Sbosticfunction was once specified to return
179*68918Sbostic.Dv EOF
180*68918Sbosticinstead of \-1.
181*68918SbosticThis was changed by
182*68918Sbostic.St -p1003.2-92
183*68918Sbosticto decouple
184*68918Sbostic.Fn getopt
185*68918Sbosticfrom
186*68918Sbostic.Pa <stdio.h> .
187*68918Sbostic.Pp
18848349ScaelA single dash
18948349Scael.Dq Li -
19048349Scaelmay be specified as an character in
19148349Scael.Fa optstring ,
19239478Sbostichowever it should
19348349Scael.Em never
19439478Sbostichave an argument associated with it.
19539478SbosticThis allows
19648349Scael.Fn getopt
19748349Scaelto be used with programs that expect
19848349Scael.Dq Li -
19948349Scaelas an option flag.
20039478SbosticThis practice is wrong, and should not be used in any current development.
20139478SbosticIt is provided for backward compatibility
20248349Scael.Em only .
20339712SbosticBy default, a single dash causes
20448349Scael.Fn getopt
205*68918Sbosticto return \-1.
20639712SbosticThis is, we believe, compatible with System V.
20748349Scael.Pp
20839478SbosticIt is also possible to handle digits as option letters.
20939478SbosticThis allows
21048349Scael.Fn getopt
21148349Scaelto be used with programs that expect a number
21248349Scael.Pq Dq Li \&-\&3
21348349Scaelas an option.
21439478SbosticThis practice is wrong, and should not be used in any current development.
21539478SbosticIt is provided for backward compatibility
21648349Scael.Em only .
21754707SmarcThe following code fragment works in most cases.
21848349Scael.Bd -literal -offset indent
21939478Sbosticint length;
22039478Sbosticchar *p;
22135982Sbostic
222*68918Sbosticwhile ((c = getopt(argc, argv, "0123456789")) != -1)
22339478Sbostic	switch (c) {
22439478Sbostic	case '0': case '1': case '2': case '3': case '4':
22539478Sbostic	case '5': case '6': case '7': case '8': case '9':
22639478Sbostic		p = argv[optind - 1];
22739478Sbostic		if (p[0] == '-' && p[1] == ch && !p[2])
22839478Sbostic			length = atoi(++p);
22939478Sbostic		else
23039478Sbostic			length = atoi(argv[optind] + 1);
23139478Sbostic		break;
23235982Sbostic	}
23339478Sbostic}
23448349Scael.Ed
235