This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
getopt.3
235 lines (222 loc) · 4.03 KB
/
getopt.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
.TH getopt 3
.SH NAME
getopt \- get option character from command line
.SH SYNTAX
.B #include <getopt.h>
char *\fBoptarg\fP;
.br
int \fBoptind\fP;
.br
int \fBoptpos\fP;
.br
int \fBopteof\fP;
.br
int \fBoptproblem\fP;
.br
char *\fBoptprogname\fP;
.br
int \fBopterr\fP;
int \fBgetopt(\fP\fIargc,argv,opts\fR\fB)\fP;
int \fIargc\fR;
.br
char **\fIargv\fR;
.br
char *\fIopts\fR;
.SH DESCRIPTION
This is a clone version of
the standard
.B getopt
library,
built on top of
.BR subgetopt(3) .
See
.B subgetopt(3)
for a detailed description of
.B getopt
processing.
The main difference between
.B getopt
and
.B subgetopt
is that
.B getopt
prints error messages
in case of problems.
To turn off these error messages, set
.B opterr
(default nonzero)
to zero.
This clone version of
.B getopt
also provides an
.B optprogname
variable.
There are two uses for this variable:
(1)
By default
.B optprogname
is null.
When
.B getopt
sees this,
it
attempts to initialize
.B optprogname
from
.IR argv\fB[0] ,
stripping the directory name.
The calling program can use
.B optprogname
after calling
.B getopt
at least once.
This is appropriate if the name of the program should be
determined from its command line.
(2)
.B getopt
prints
.B optprogname
at the beginning
of any error messages.
So the calling program can,
before calling
.BR getopt ,
initialize
.B optprogname
as desired.
This is appropriate if the name of the program should not be
determined from its command line.
.SH "COMPATIBILITY"
Old versions of
.B getopt
do not include
.BR opterr .
.BR optpos ,
.BR opteof ,
.BR optproblem ,
and
.B optprogname
are specific to this clone version of
.BR getopt .
Many features of this clone version of
.B getopt
are poorly defined, if available at all,
in most versions of
.BR getopt .
For example, the standard
.B getopt
interface does not define
.B optind
until the end of the option list.
And
.B optarg
is not defined
unless
.B getopt
has just returned
an option which takes an argument.
In this clone version,
.B optind
and
.B optpos
always indicate the next character to be read,
and
.B optarg
is null whenever
the current option does not take an argument.
See
.B subgetopt(3)
for precise definitions of the parsing procedure.
When it reaches the end of the option list,
this version of
.B getopt
always returns
.BR opteof ,
which is the same as
.BR subgetoptdone ,
which is initialized to
.BR SUBGETOPTDONE ,
which is defined as \-1.
The standard behavior is to return
EOF
from
.B stdio(3).
This is incompatible
on any weird machine where
EOF is different from \-1.
The calling program could set
.B opteof
to EOF to imitate the standard behavior.
Like most versions of
.BR getopt ,
this clone version allows, but does not demand, that
option arguments be
separated from the option by whitespace, i.e., be
in the next command-line argument.
Some versions of
.B getopt
provide an
.B optopt
variable.
.B optopt
is incompatible across systems:
for example,
GNU
.B getopt
uses it the same way that this clone version uses
.BR optproblem ,
while
BSD
.B getopt
uses it to
indicate the last option character returned by
.BR getopt .
This clone version does not provide
.BR optopt .
The use of
.B optopt
is strongly discouraged.
Some versions of
.B getopt
do not recognize a double hyphen as the end of arguments.
This version allows a double hyphen, or in fact any argument beginning
with two hyphens.
A lone hyphen is always recognized as the end of arguments.
Some versions of
.B getopt
allow lone hyphens as options.
This practice is wrong and is strongly discouraged.
.SH "SYNTAX NOTE"
.B getopt
is actually a macro abbreviation for
.BR getoptmine .
The external
.B opterr
and
.B optprogname
variables
are macros for
.B getopterr
and
.BR getoptprogname .
All the other
.B opt
variables are macros
for
.BR subgetopt .
These macros are defined in
.BR <getopt.h> ,
unless
.B GETOPTNOSHORT
is defined.
Further macros are defined in
.BR <subgetopt.h> ,
which is included by
.BR <getopt.h> ,
unless
.B SUBGETOPTNOSHORT
is defined.
.SH VERSION
getopt version 1.9, 931129.
.SH AUTHOR
Placed into the public domain by Daniel J. Bernstein.