summaryrefslogtreecommitdiff
path: root/subgetopt.3
blob: aae03aafb4fa7a1f56373260dcd819c05371eb27 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
.TH subgetopt 3
.SH NAME
subgetopt \- get option character from command line
.SH SYNTAX
.B #include <subgetopt.h>

char *\fBsgoptarg\fP;
.br
int \fBsgoptind\fP;
.br
int \fBsgoptpos\fP;
.br
int \fBsgoptdone\fP;
.br
int \fBsgoptproblem\fP;

int \fBsgopt(\fP\fIargc,argv,opts\fR\fB)\fP;

int \fIargc\fR;
.br
char **\fIargv\fR;
.br
char *\fIopts\fR;
.SH DESCRIPTION
.B sgopt
returns the next valid command-line option character
from
.IR argv .

Valid option characters are listed in the
.I opts
string.
.I opts
may be empty.
A character in
.I opts
may be followed by a colon,
in which case it
takes an
.I option argument\fR.
Avoid using the characters ?, :, and \- as option characters.

Below
.I option argument
is abbreviated
as
.I optarg
and
.I command-line argument
is abbreviated as
.IR cmdarg .

Options are listed in cmdargs which begin with
a minus sign.
Several options which do not take optargs may be combined
into one cmdarg.

An option which takes an optarg may be handled in two ways.
If it appears at the very end of a cmdarg,
then the entire next cmdarg is the optarg.
But if there are any characters in the cmdarg
after the option character,
then those characters form the optarg.
The optarg is returned in
.BR sgoptarg .
Next time
.B sgopt
looks at the cmdarg which follows the optarg.

If a cmdarg does not begin with a hyphen,
or if it is a lone hyphen not followed by any characters,
or if it begins with two hyphens,
then it terminates option processing,
and
.B sgopt
returns an appropriate code.
If there are two hyphens,
.B sgopt
will advance attention to the next cmdarg,
so it can be called again to read further options.
.SH "PROPER USAGE"
.B sgoptproblem
should be used only when
.B sgopt
returns ?.
.B sgoptind
and
.B sgoptpos
are defined all the time.
.B sgoptarg
is defined all the time;
it is null unless
.B sgopt
has just returned an option with optarg.

.B sgopt
is typically used as follows.

.EX
#include <subgetopt.h>

main(argc,argv) int argc; char **argv; { int opt;

while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone)
.br
  switch(opt) {
.br
    case 'a':
.br
      printf("opt a with optarg %s\\n",sgoptarg); break;
.br
    case 's':
.br
      printf("opt s with no optarg\\n"); break;
.br
    case '?':
.br
      if (argv[sgoptind] && (sgoptind < argc))
.br
        printf("illegal opt %c\\n",sgoptproblem);
.br
      else
.br
        printf("missing arg, opt %c\\n",sgoptproblem);
.br
      exit(1);
.br
  }

argv += sgoptind;
.br
while (*argv) printf("argument %s\\n",*argv++);
.br
exit(0);
.br
}
.EE

The end of the command line is
marked by either
.IR argc ,
or a null pointer in
.IR argv ,
whichever comes first.
Normally
these two markers coincide,
so it is redundant
to test for
both
.I argv\fB[sgoptind]
and
.B sgoptind < \fIargc\fR.
The above code shows both tests as an illustration.

.B Multiple option sets:
One useful technique is to call
.B sgopt
with a primary
.I opts
until it returns EOF,
then call
.B sgopt
with a secondary
.I opts
until it returns EOF.
The user can provide primary options, then a double hyphen,
and then secondary options.
No special handling is needed if some or all of the options are
omitted.
The same technique can be used for any number of option sets
in series.

.B Multiple command lines:
Before parsing a new
.BR argv ,
make sure to
set
.B sgoptind
and
.B sgoptpos
back to
1 and 0.
.SH "PARSING STAGES"
.B sgopt
keeps track of its position in
.I argv
with
.B sgoptind
and
.BR sgoptpos ,
which are initialized to 1 and 0.
It looks at
.I argv\fB[sgoptind][sgoptpos]
and following characters.

.B sgopt
indicates
that no more options are available by
returning
.BR sgoptdone ,
which is initialized to
.BR SUBGETOPTDONE ,
which is defined as \-1.

.B sgopt
begins by setting
.B optarg
to null.

.B Ending conditions:
If
.I argv
is null, or
.B sgoptind
is larger than
.IR argc ,
or the current cmdarg
.I argv\fB[sgoptind]
is null,
then
.B sgopt
returns
.BR optdone .

.B Stage one:
If the current character
is zero,
.B sgopt
moves to the beginning of the next cmdarg.
It then checks the ending conditions again.

.B Stage two:
If
the current position is the begining of the cmdarg,
.B sgopt
checks whether
the current character
is a minus sign.
If not it returns
.BR optdone .
It then
moves
to the next character.
If that character is zero,
.B sgopt
moves
back to the beginning of the cmdarg,
and returns
.BR sgoptdone .
If the character is a minus sign,
.B sgopt
moves to the beginning of the next cmdarg,
and returns
.BR sgoptdone .

.B Stage three:
.B sgopt
records the current character,
.IR c ,
and moves to the next character.
There are three possibilities:
(1)
.I c
is an option character without optarg in
.IR opts ,
or
(2)
.I c
is an option character with optarg in
.IR opts ,
or
(3)
.I c
does not appear in
.IR opts .

(1)
If
.I c
appears as an option character without optarg in
.IR opts ,
.B sgopt
returns
.IR c .

(2)
If
.I c
appears as an option character with optarg in
.IR opts ,
.B sgopt
sets
.B sgoptarg
to the current position,
and moves to the next cmdarg.
If
.B sgoptarg
is nonempty,
.B sgopt
returns
.IR c .

Then
.B sgopt
sets
.B sgoptarg
to
the current cmdarg.
If
the current cmdarg is null,
or past
.IR argc ,
.B sgopt
sets
.B sgoptproblem
to
.I c
and returns ?.
Otherwise
.B sgopt
moves to the next
argument
and returns
.IR c .

(2)
If
.I c
does not appear in
.IR opts ,
.B sgopt
sets
.B sgoptproblem
to
.I c
and returns ?.
.SH "SYNTAX NOTE"
.B sgopt
is actually a macro abbreviation for
.BR subgetopt .
The external
.B sg
variables are also macros
for
.BR subget .
These macros are defined in
.BR <subgetopt.h> ,
unless
.B SUBGETOPTNOSHORT
is defined
when
.B <subgetopt.h>
is included.
.SH VERSION
subgetopt version 0.9, 931129.
.SH AUTHOR
Placed into the public domain by Daniel J. Bernstein.