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