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