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