source: trunk/minix/man/man3/getopt.3@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.8 KB
RevLine 
[9]1.\" Copyright (c) 1985 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5.\" @(#)getopt.3 6.4 (Berkeley) 5/27/86
6.\"
7.TH GETOPT 3 "May 27, 1986"
8.UC 6
9.SH NAME
10getopt \- get option letter from argv
11.SH SYNOPSIS
12.ft B
13int getopt(argc, argv, optstring)
14.br
15int argc;
16.br
17char **argv;
18.br
19char *optstring;
20.sp
21extern char *optarg;
22.br
23extern int optind;
24.ft
25.SH DESCRIPTION
26.I Getopt
27returns the next option letter in
28.I argv
29that matches a letter in
30.IR optstring .
31.I Optstring
32is a string of recognized option letters;
33if a letter is followed by a colon, the option is expected to have
34an argument that may or may not be separated from it by white space.
35.I Optarg
36is set to point to the start of the option argument on return from
37.IR getopt .
38.PP
39.I Getopt
40places in
41.I optind
42the
43.I argv
44index of the next argument to be processed.
45Because
46.I optind
47is external, it is normally initialized to zero automatically
48before the first call to
49.IR getopt .
50.PP
51When all options have been processed (i.e., up to the first
52non-option argument),
53.I getopt
54returns
55.BR EOF .
56The special option
57.B \-\-
58may be used to delimit the end of the options;
59.B EOF
60will be returned, and
61.B \-\-
62will be skipped.
63.SH DIAGNOSTICS
64.I Getopt
65prints an error message on
66.I stderr
67and returns a question mark
68.RB ( ? )
69when it encounters an option letter not included in
70.IR optstring .
71.SH EXAMPLE
72The following code fragment shows how one might process the arguments
73for a command that can take the mutually exclusive options
74.B a
75and
76.BR b ,
77and the options
78.B f
79and
80.BR o ,
81both of which require arguments:
82.PP
83.RS
84.nf
85main(argc, argv)
86int argc;
87char **argv;
88{
89 int c;
90 extern int optind;
91 extern char *optarg;
92 \&.
93 \&.
94 \&.
95 while ((c = getopt(argc, argv, "abf:o:")) != EOF)
96 switch (c) {
97 case `a':
98 if (bflg)
99 errflg++;
100 else
101 aflg++;
102 break;
103 case `b':
104 if (aflg)
105 errflg++;
106 else
107 bproc();
108 break;
109 case `f':
110 ifile = optarg;
111 break;
112 case `o':
113 ofile = optarg;
114 break;
115 case `?':
116 default:
117 errflg++;
118 break;
119 }
120 if (errflg) {
121 fprintf(stderr, "Usage: ...");
122 exit(2);
123 }
124 for (; optind < argc; optind++) {
125 \&.
126 \&.
127 \&.
128 }
129 \&.
130 \&.
131 \&.
132}
133.RE
134.SH HISTORY
135Written by Henry Spencer, working from a Bell Labs manual page.
136Modified by Keith Bostic to behave more like the System V version.
137.SH BUGS
138It is not obvious how
139`\-'
140standing alone should be treated; this version treats it as
141a non-option argument, which is not always right.
142.PP
143Option arguments are allowed to begin with `\-';
144this is reasonable but reduces the amount of error checking possible.
145.PP
146.I Getopt
147is quite flexible but the obvious price must be paid: there is much
148it could do that it doesn't, like
149checking mutually exclusive options, checking type of
150option arguments, etc.
Note: See TracBrowser for help on using the repository browser.