xref: /csrg-svn/lib/libc/stdlib/getopt.3 (revision 48349)
1*48349Scael.\" Copyright (c) 1988, 1991 Regents of the University of California.
233992Sbostic.\" All rights reserved.
320617Smckusick.\"
443571Strent.\" %sccs.include.redist.man%
520617Smckusick.\"
6*48349Scael.\"     @(#)getopt.3	6.16 (Berkeley) 04/19/91
733992Sbostic.\"
8*48349Scael.Dd
9*48349Scael.Dt GETOPT 3
10*48349Scael.Os BSD 4.3
11*48349Scael.Sh NAME
12*48349Scael.Nm getopt
13*48349Scael.Nd get option letter from argv
14*48349Scael.Sh SYNOPSIS
15*48349Scael.Fd #include <stdlib.h>
16*48349Scael.Vt extern char *optarg
17*48349Scael.Vt extern int   optind
18*48349Scael.Vt extern int   opterr
19*48349Scael.Ft int
20*48349Scael.Fn getopt "int argc" "char * const *argv" "const char *optstring"
21*48349Scael.Sh DESCRIPTION
22*48349ScaelThe
23*48349Scael.Fn getopt
24*48349Scaelfunction gets
25*48349Scaelthe next
26*48349Scael.Em known
27*48349Scaeloption character from
28*48349Scael.Fa argv .
29*48349ScaelAn option character is
30*48349Scael.Em known
31*48349Scaelif it has been specified in the string of accepted option characters,
32*48349Scael.Fa optstring .
33*48349Scael.Pp
34*48349ScaelThe option string
35*48349Scael.Fa optstring
36*48349Scaelmay contain the following characters; letters and
37*48349Scaelletters followed by a colon to indicate an option argument
38*48349Scaelis to follow. It does not matter to
39*48349Scael.Fn getopt
40*48349Scaelif a following argument has leading white space.
41*48349Scael.Pp
4239478SbosticOn return from
43*48349Scael.Fn getopt ,
44*48349Scael.Va optarg
45*48349Scaelpoints to an option argument, if it is anticipated,
46*48349Scaeland the variable
47*48349Scael.Va optind
48*48349Scaelcontains the index to the next
49*48349Scael.Fa argv
50*48349Scaelargument for a subsequent call
51*48349Scaelto
52*48349Scael.Fn getopt .
53*48349Scael.Pp
54*48349ScaelThe variable
55*48349Scael.Va opterr
5639478Sbosticand
57*48349Scael.Va optind
5839478Sbosticare both initialized to 1.
5942262SbosticIn order to use
60*48349Scael.Fn getopt
6142262Sbosticto evaluate multiple sets of arguments, or to evaluate a single set of
6242262Sbosticarguments multiple times,
63*48349Scael.Va optind
6442262Sbosticmust be initialized to the number of argv entries to be skipped in each
6542262Sbosticevaluation.
66*48349Scael.Pp
67*48349ScaelThe
68*48349Scael.Fn getopt
69*48349Scaelfunction
70*48349Scaelreturns an
71*48349Scael.Dv EOF
72*48349Scaelwhen the argument list is exhausted, or a non-recognized
73*48349Scaeloption is encountered.
74*48349ScaelThe interpretation of options in the argument list may be cancelled
75*48349Scaelby the option
76*48349Scael.Ql --
77*48349Scael(double dash) which causes
78*48349Scael.Fn getopt
79*48349Scaelto signal the end of argument processing and return an
80*48349Scael.Dv EOF .
8139478SbosticWhen all options have been processed (i.e., up to the first non-option
8239478Sbosticargument),
83*48349Scael.Fn getopt
84*48349Scaelreturns
85*48349Scael.Dv EOF .
86*48349Scael.Sh DIAGNOSTICS
87*48349ScaelIf the
88*48349Scael.Fn getopt
89*48349Scaelfunction encounters a character not found in the string
90*48349Scael.Va optarg
91*48349Scaelor detects
92*48349Scaela missing option argument
93*48349Scaelit writes error message
94*48349Scael.Ql ?
95*48349Scaelto the
96*48349Scael.Em stderr .
9739478SbosticSetting
98*48349Scael.Va opterr
9939478Sbosticto a zero will disable these error messages.
100*48349Scael.Sh EXAMPLE
101*48349Scael.Bd -literal -compact
10239478Sbosticextern char *optarg;
10339478Sbosticextern int optind;
10439478Sbosticint bflag, ch, fd;
10539478Sbostic
10639478Sbosticbflag = 0;
10739478Sbosticwhile ((ch = getopt(argc, argv, "bf:")) != EOF)
10839478Sbostic	switch(ch) {
10939478Sbostic	case 'b':
11039478Sbostic		bflag = 1;
11139478Sbostic		break;
11239478Sbostic	case 'f':
11339478Sbostic		if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
11439478Sbostic			(void)fprintf(stderr,
115*48349Scael				"myname: unable to read file %s.\en", optarg);
116*48349Scael			exit(1) ;
11720617Smckusick		}
11839478Sbostic		break;
11939478Sbostic	case '?':
12039478Sbostic	default:
12139478Sbostic		usage();
122*48349Scael}
12339478Sbosticargc -= optind;
12439478Sbosticargv += optind;
125*48349Scael.Ed
126*48349Scael.Sh HISTORY
127*48349ScaelThe
128*48349Scael.Fn getopt
129*48349Scaelfunction appeared
130*48349Scael.Bx 4.3 .
131*48349Scael.Sh BUGS
132*48349ScaelOption arguments are allowed to begin with
133*48349Scael.Dq Li \- ;
134*48349Scaelthis is reasonable but
13539478Sbosticreduces the amount of error checking possible.
136*48349Scael.Pp
137*48349ScaelA single dash
138*48349Scael.Dq Li -
139*48349Scaelmay be specified as an character in
140*48349Scael.Fa optstring ,
14139478Sbostichowever it should
142*48349Scael.Em never
14339478Sbostichave an argument associated with it.
14439478SbosticThis allows
145*48349Scael.Fn getopt
146*48349Scaelto be used with programs that expect
147*48349Scael.Dq Li -
148*48349Scaelas an option flag.
14939478SbosticThis practice is wrong, and should not be used in any current development.
15039478SbosticIt is provided for backward compatibility
151*48349Scael.Em only .
15239712SbosticBy default, a single dash causes
153*48349Scael.Fn getopt
154*48349Scaelto return
155*48349Scael.Dv EOF .
15639712SbosticThis is, we believe, compatible with System V.
157*48349Scael.Pp
15839478SbosticIt is also possible to handle digits as option letters.
15939478SbosticThis allows
160*48349Scael.Fn getopt
161*48349Scaelto be used with programs that expect a number
162*48349Scael.Pq Dq Li \&-\&3
163*48349Scaelas an option.
16439478SbosticThis practice is wrong, and should not be used in any current development.
16539478SbosticIt is provided for backward compatibility
166*48349Scael.Em only .
16739478SbosticThe following code fragment works fairly well.
168*48349Scael.Bd -literal -offset indent
16939478Sbosticint length;
17039478Sbosticchar *p;
17135982Sbostic
17239478Sbosticwhile ((c = getopt(argc, argv, "0123456789")) != EOF)
17339478Sbostic	switch (c) {
17439478Sbostic	case '0': case '1': case '2': case '3': case '4':
17539478Sbostic	case '5': case '6': case '7': case '8': case '9':
17639478Sbostic		p = argv[optind - 1];
17739478Sbostic		if (p[0] == '-' && p[1] == ch && !p[2])
17839478Sbostic			length = atoi(++p);
17939478Sbostic		else
18039478Sbostic			length = atoi(argv[optind] + 1);
18139478Sbostic		break;
18235982Sbostic	}
18339478Sbostic}
184*48349Scael.Ed
185