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