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