chiark / gitweb /
@@@ wip type definitions in manpage synopses
[mLib] / ui / mdwopt.3
1 .\" -*-nroff-*-
2 .TH mdwopt 3 "6 July 1999" "Straylight/Edgeware" "mLib utilities library"
3 .SH "NAME"
4 mdwopt \- command-line option parser
5 .\" @mdwopt
6 .SH "SYNOPSIS"
7 .nf
8 .B "#include <mLib/mdwopt.h>"
9
10 .B "typedef struct {"
11 .B "\h'4n'char *arg, *prog;"
12 .B "\h'4n'int opt, ind, err;"
13 .B "\h'4n'..."
14 .B "} mdwopt_data;"
15
16 .B "char *optarg, optprog;"
17 .B "int optopt, opterr, optind;"
18
19 .B "struct option {"
20 .B "\h'4n'const char *name;"
21 .B "\h'4n'int has_arg;"
22 .B "\h'4n'int *flag;"
23 .B "\h'4n'int val;"
24 .B "};"
25
26 .B "#define OPTF_NOARG = ..."
27 .B "#define OPTF_ARGREQ = ..."
28 .B "#define OPTF_ARGOPT = ..."
29 .B "#define OPTF_ARG = ..."
30 .B "#define OPTF_SWITCH = ..."
31 .B "#define OPTF_NEGATE = ..."
32
33 .B "#define OPTF_NOLONGS = ..."
34 .B "#define OPTF_NOSHORTS = ..."
35 .B "#define OPTF_NUMBERS = ..."
36 .B "#define OPTF_NEGATION = ..."
37 .B "#define OPTF_ENVVAR = ..."
38 .B "#define OPTF_NOPROGNAME = ..."
39 .B "#define OPTF_NEGNUMBER = ..."
40
41 .B "#define OPTF_NEGATED = ..."
42
43 .ds mT \fBint mdwopt(
44 .BI "\*(mTint " argc ", char *const *" argv ,
45 .BI "\h'\w'\*(mT'u'const char *" shortopt ,
46 .BI "\h'\w'\*(mT'u'const struct option *" longopt ", int *" longind ,
47 .BI "\h'\w'\*(mT'u'mdwopt_data *" data ", int " flags );
48
49 .BI "int getopt(int " argc ", char *const *" argv ", const char *" o );
50
51 .ds mT \fBint getopt_long(
52 .BI "\*(mTint " argc ", char *const *" argv ,
53 .BI "\h'\w'\*(mT'u'const char * "shortopt ,
54 .BI "\h'\w'\*(mT'u'const struct option *" longopt ", int *" longind );
55
56 .ds mT \fBint getopt_long_only(
57 .BI "\*(mTint " argc ", char *const *" argv ,
58 .BI "\h'\w'\*(mT'u'const char * "shortopt ,
59 .BI "\h'\w'\*(mT'u'const struct option *" longopt ", int *" longind );
60 .fi
61 .SH "OVERVIEW"
62 The
63 .B mdwopt
64 function is a command line options parser which is (mostly) compatible
65 with the standard POSIX and GNU
66 .B getopt
67 functions, although provides more features than either.  It's not the
68 most featureful options parser around, but it's good enough for my
69 purposes at the moment.
70 .SH "OPTION SYNTAX"
71 A command line consists of a number of
72 .I words
73 (which may contain spaces, according to various shell quoting
74 conventions).  A word may be an option, an argument to an option, or a
75 non-option.  An option begins with a special character, usually
76 .RB ` \- ',
77 although
78 .RB ` + '
79 is also used sometimes.  As special exceptions, the word containing only
80 a
81 .RB ` \- '
82 is considered to be a non-option, since it usually represents standard
83 input or output as a filename, and the word containing only a
84 double-dash
85 .RB ` \-\- '
86 is used to mark all following words as being non-options regardless of
87 their initial character.
88 .PP
89 Traditionally, all words after the first non-option have been considered
90 to be non-options automatically, so that options must be specified
91 before filenames.  However, this implementation can extract all the
92 options from the command line regardless of their position.  This can
93 usually be disabled by setting one of the environment variables
94 .B POSIXLY_CORRECT
95 or
96 .BR _POSIX_OPTION_ORDER .
97 .PP
98 There are two different styles of options:
99 .I short
100 and
101 .IR long .
102 Traditional Unix (and POSIX) only uses short options.  The long options
103 are a GNU convention.
104 .SS "Short option syntax"
105 Short options are the sort which Unix has known for ages: an option is a
106 single letter, preceded by a
107 .RB ` \- '.
108 Short options can be joined together to save space (and possibly to make
109 silly words): e.g., instead of giving options
110 .RB ` "\-x \-y" ',
111 a user could write
112 .RB ` \-xy '.
113 Some short options can have arguments which appear after the option
114 letter, either immediately following, or in the next word; so an option
115 with an argument could be written as
116 .RB ` "\-o foo" '
117 or as
118 .RB ` \-ofoo ').
119 Note that options with optional arguments must be written in the second
120 style.
121 .PP
122 When a short option controls a flag setting, it is sometimes possible to
123 explicitly turn the flag off, as well as turning it on, (usually to
124 override default options).  This is usually done by using a
125 .RB ` + '
126 instead of a
127 .RB ` \- '
128 to introduce the option.  (Some programs use upper-case option letters
129 to indicate this instead.)
130 .SS "Long option syntax"
131 Long options, as popularized by the GNU utilities, are given long-ish
132 memorable names, preceded by a double-dash
133 .RB ` \-\- '.
134 Since their names are more than a single character, long options can't
135 be combined in the same way as short options.  Arguments to long options
136 may be given either in the same word, separated from the option name by
137 an equals sign, or in the following word.
138 .PP
139 Long option names can be abbreviated if necessary, as long as the
140 abbreviation is unique.  This means that options can have sensible and
141 memorable names but still not require much typing from an experienced
142 user.
143 .PP
144 Like short options, long options can control flag settings.  The options
145 to manipulate these settings come in pairs: an option of the form
146 .RB ` \-\-set\-flag '
147 might set the flag, while an option of the form
148 .RB ` \-\-no\-set\-flag '
149 might clear it.
150 .PP
151 It is usual for applications to provide both short and long options with
152 identical behaviour.  Some applications with lots of options may only
153 provide long options (although they will often be only two or three
154 characters long).  In this case, long options can be preceded with a
155 single
156 .RB ` \- '
157 character, and negated by a
158 .RB ` + '
159 character.
160 .SS "Numerical options"
161 Finally, some (older) programs accept arguments of the form
162 .RB ` \- \c
163 .IR number ',
164 to set some numerical parameter, typically a line count of some kind.
165 .SH "PARSING OPTIONS WITH \fBmdwopt\fP"
166 An application parses its options by calling
167 .B mdwopt
168 repeatedly.  Each time it is called,
169 .B mdwopt
170 returns a value describing the option just read, and stores information
171 about the option in a data block.
172 .PP
173 The data block is a structure containing at least the following members:
174 .TP
175 .B "char *arg"
176 Pointer to the argument of the current option, or null.  Argument
177 strings persist for as long as the underlying command line argument
178 array
179 .I argv
180 does, so it's usually safe just to remember the pointer.
181 .TP
182 .B "int opt"
183 Value of the current option
184 .TP
185 .B "int ind"
186 Must be initialized to 0 before the first call to
187 .BR mdwopt .
188 After the last call, it is the index into
189 .I argv
190 of the first nonoption argument.
191 .TP
192 .B "int err"
193 Set to nonzero to allow
194 .B mdwopt
195 to emit error messages about illegal option syntax.  (This would be a
196 flag setting, but it has to be here for
197 .B getopt
198 compatibility.)
199 .TP
200 .B "char *prog"
201 Contains the program's name, stripped of any path prefix.  This is an
202 obsolete feature: the
203 .BR quis (3)
204 module does the job in a more sensible way.
205 .PP
206 Prior to the first call to
207 .BR mdwopt ,
208 the
209 .B err
210 and
211 .B ind
212 members of the structure must be initialized.
213 .PP
214 The arguments
215 .I argc
216 and
217 .I argv
218 describe the command-line argument array which is to be parsed.  These
219 will usually be exactly the arguments passed to the program's
220 .B main
221 function.
222 .SS "Short option parsing"
223 Short options are described by a string,
224 .IR shortopt ,
225 which once upon a time just contained the permitted option characters.
226 Now the options string begins with a collection of flag characters, and
227 various flag characters can be put after options characters to change
228 their properties.
229 .PP
230 If the first character of the short options string is
231 .RB ` + ',
232 .RB ` \- '
233 or
234 .RB ` ! ',
235 the order in which options are read is modified, as follows:
236 .TP
237 .RB ` + '
238 Forces the POSIX order to be used. As soon as a non-option is found,
239 .B mdwopt
240 returns \-1.
241 .TP
242 .RB ` \- '
243 Makes
244 .B mdwopt
245 treat non-options as being `special' sorts of option. When a non-option
246 word is found, the value 0 is returned, and the actual text of the word
247 is stored as being the option's argument.
248 .TP
249 .RB ` ! '
250 forces the default order to be used regardless of environment variable
251 settings.  The entire command line is scanned for options, which are
252 returned in order.  However, during this process, the options are moved
253 in the
254 .I argv
255 array, so that they appear before the non-options.
256 .PP
257 A
258 .RB ` : '
259 character may be placed after the ordering flag (or at the very
260 beginning if no ordering flag is given) which indicates that the
261 character
262 .RB ` : ',
263 rather than
264 .RB ` ? ',
265 should be returned if a missing argument error is detected.
266 .PP
267 Each option in the string can be followed by a
268 .RB ` + '
269 sign, indicating that it can be negated, a
270 .RB ` : '
271 sign indicating that it requires an argument, or a
272 .RB ` :: '
273 string, indicating an optional argument.  Both
274 .RB ` + '
275 and one of
276 .RB ` : '
277 or
278 .RB ` :: '
279 may be given, although the
280 .RB ` + '
281 must come first.
282 .PP
283 If an option is found, the option character is returned to the caller.
284 A pointer to an argument is stored in the
285 .B arg
286 member of the data block; a null pointer is stored if there was no
287 argument.  If a negated option was found, the option character is
288 returned ORed with
289 .B OPTF_NEGATED
290 (bit 8 set).
291 .SS "Long option parsing"
292 Long options are described in a table.  Each entry in the
293 table is of type
294 .BR "struct option" ,
295 which contains the following members (in order):
296 .TP
297 .B "const char *name"
298 Pointer to the option's name.
299 .TP
300 .B "int has_arg"
301 A flags word describing the option.  (The name is historical.)
302 .TP
303 .B "int *flag"
304 Address of the flag variable to use when this option is matched.
305 .TP
306 .B "int val"
307 Value to store or return when this option is matched.
308 .PP
309 The table is terminated by an entry whose
310 .B name
311 field is a null pointer.
312 .PP
313 When
314 .B mdwopt
315 finds a long option, it looks the name up in the table. The index of the
316 matching entry is stored in the
317 .I longind
318 variable, passed to
319 .B mdwopt
320 (unless
321 .I longind
322 is null): a value of \-1 indicates that no long option was found. The
323 behaviour is then dependent on the values in the table entry.
324 .PP
325 If the flag bit
326 .B OPTF_ARGREQ
327 is set in
328 .B has_arg
329 then the option has a required argument, which may be separated from the
330 option name by an equals sign or placed in the following word.  If the
331 flag bit
332 .B OPTF_ARGOPT
333 is set then the argument is optional.  If present, the argument must be
334 in the same word as the option name, separated by an equals sign.  It is
335 an error for both flags to be set; if neither is set then the option
336 does not take an argument.
337 .PP
338 If
339 .B flag
340 is nonzero, it points to an integer to be modified by
341 .BR mdwopt .
342 Usually the value in the
343 .B val
344 field is simply stored in the
345 .B flag
346 variable. If the flag
347 .B OPTF_SWITCH
348 is set in the
349 .B has_arg
350 member, however, the value is combined with the existing value of the
351 flags using a bitwise OR.  If
352 .B OPTF_NEGATE
353 is set in the
354 .B has_arg
355 field, then the flag bit will be cleared if a matching negated long
356 option is found.  The value 0 is returned.
357 .PP
358 If
359 .B flag
360 is zero, the value in
361 .B val
362 is returned by
363 .BR mdwopt ,
364 possibly with bit 8 set if the option was
365 negated.
366 .PP
367 Arguments from long options are stored in the
368 .B arg
369 member of the data block.
370 .SS "Other optional features"
371 The
372 .I flags
373 argument contains a bitmask of features which may be enabled:
374 .TP
375 .B OPTF_NOLONGS
376 Don't allow any long options.  This makes
377 .B mdwopt
378 compatible with traditional Unix
379 .BR getopt .
380 .TP
381 .B OPTF_NOSHORTS
382 A slightly misnamed flag.  Short options are read normally.  However,
383 long options may also begin with a single dash
384 .RB ` \- '
385 (or the
386 .RB ` + '
387 sign if negated).  Long options may not be combined with short options:
388 an option word which begins with a short option must contain only short
389 options.
390 .TP
391 .B OPTF_NUMBERS
392 Read numeric options.  If a numeric option is found, the character
393 .RB ` # '
394 is returned and the text of the number is stored in the
395 .B arg
396 member of the data block.
397 .TP
398 .B OPTF_NEGATION
399 Allow negation of options.  Negated options are returned ORed with
400 .BR OPTF_NEGATED .
401 .TP
402 .B OPTF_ENVVAR
403 Options will be read from an environment variable before scanning the
404 actual command line provided.  The name of the environment variable is
405 found by capitalizing the program name.  (This allows a user to have
406 different default settings for a program, by calling it through
407 different symbolic links.)
408 .TP
409 .B OPTF_NOPROGNAME
410 Don't read the program name from
411 .IR argv \c
412 .BR [0] ,
413 and don't set the
414 .B prog
415 data block member.  Options start right at the beginning of
416 .IR argv .
417 .TP
418 .B OPTF_NEGNUMBER
419 Allow negated numeric options.  Negated numeric options begin with a
420 .RB ` + '
421 rather than a
422 .RB ` \- '.
423 The return value is
424 .RB ` # ' " | OPTF_NEGATED" .
425 .SS "Compatibility features"
426 The macros
427 .BR getopt ,
428 .B getopt_long
429 and
430 .B getopt_long_only
431 correspond to calls to
432 .B mdwopt
433 with various flag settings.  See the macro definitions for the actual
434 mappings, and the documentation for the functions to see how they're
435 meant to work.
436 .PP
437 Additionally, there is a global data block, which is specified by
438 passing a null
439 .I data
440 argument to
441 .BR mdwopt .
442 The members of this block may be referred to by their traditional names:
443 .TP
444 .B optarg
445 The argument of the current option.
446 .TP
447 .B optopt
448 Option code of the current option.
449 .TP
450 .B opterr
451 Nonzero if
452 .B mdwopt
453 is to report errors.  This is the default.
454 .TP
455 .B optind
456 Index of the first non-option argument.
457 .TP
458 .B optprog
459 Name of the program, stripped of path prefix.
460 .PP
461 These names aren't considered deprecated: they help make the code easier
462 to read by people used to the traditional
463 .B getopt
464 function.
465 .SH "SEE ALSO"
466 .BR getopt (3),
467 .BR mLib (3).
468 .SH "AUTHOR"
469 Mark Wooding, <mdw@distorted.org.uk>