1
.TH ARGTABLE2 3 "December 2005" "Argtable2\-@PACKAGE_VERSION@" "Argtable programmer's manual"
3
argtable2 \- an ANSI C library for parsing GNU style command line options
6
\fC#include <argtable2.h>\fP
13
.RB "struct " arg_file
14
.RB "struct " arg_date
18
.RB "struct " arg_xxx "* " arg_xxx0 (...)
19
.RB "struct " arg_xxx "* " arg_xxx1 (...)
20
.RB "struct " arg_xxx "* " arg_xxxn (...)
22
.RB "int " arg_nullcheck "(void **argtable)"
23
.RB "int " arg_parse "(int argc, char **argv, void **argtable)"
24
.RB "void " arg_print_option "(FILE *fp, const char *shortopts, const char *longopts,"
25
.RB " const char *datatype, const char *suffix)"
26
.RB "void " arg_print_syntax "(FILE *fp, void **argtable, const char *suffix)"
27
.RB "void " arg_print_syntaxv "(FILE *fp, void **argtable, const char *suffix)"
28
.RB "void " arg_print_glossary "(FILE *fp, void **argtable, const char *format)"
29
.RB "void " arg_print_glossary_gnu "(FILE *fp, void **argtable)"
30
.RB "void " arg_print_errors "(FILE *fp, struct arg_end *end, const char *progname)"
31
.RB "void " arg_freetable "(void **argtable, size_t n)"
33
Argtable is an ANSI C library for parsing GNU style command line arguments with a minimum of fuss. It enables the programmer to define their program's argument syntax directly in the source code as an array of structs. The command line is then parsed according to that specification and the resulting values stored directly into user\-defined program variables where they are accessible to the main program.
35
This man page is only for reference. Introductory and advanced texts on argtable should be
36
available on this system in pdf, html, and postscript from under \fB@prefix@/share/doc/argtable2/\fP along with example source code.
37
Alternatively refer to the argtable homepage at http://argtable.sourceforge.net.
38
.SS Constructing an arg_xxx data structure
39
Each \fBarg_xxx\fP struct has it own unique set of constructor functions
40
and while these may differ slightly between \fBarg_xxx\fP structs,
41
they are generally of the form:
43
struct \fBarg_xxx* arg_xxx0\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
45
struct \fBarg_xxx* arg_xxx1\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
47
struct \fBarg_xxx* arg_xxxn\fP (const char *shortopts, const char *longopts, const char *datatype, int mincount, int maxcount, const char *glossary)
49
The \fBarg_xxx0()\fP and \fBarg_xxx1()\fP forms are merely abbreviated
50
forms of \fBarg_xxxn()\fP and are provided as a convenience for
51
the most common arrangements of command line options; namely those
52
that have zero\-or\-one occurrences (mincount=0,maxcount=1) and those
53
that have one exactly one occurrence (mincount=1,maxcount=1) respectively.
55
The \fIconst char* shortopts\fP parameter defines the option's short
56
form tag (eg: \-x, \-k3, \-D"macro").
57
It can be left as NULL if a short option is not required, otherwise use it to
58
specify the desired short option character in the string (without the
59
leading "\-" and without any whitespace).
60
For example, the short option \-v is defined simply as "v".
61
In fact, a command line option may have multiple alternate short form
62
tags defined for it by concatenating the desired characters into the
64
For instance "abc" defines an option which will accept any of the three
65
equivalent short forms \-a, \-b, \-c interchangeably.
67
The \fIconst char* longopts\fP parameter is similar to \fIshortopts\fP,
68
except it defines the option's long form tags (eg: \-\-help, \-\-depth=3, \-\-name=myfile.txt).
69
It too can be left as NULL if not required, and it too can have multiple
70
equivalent tags defined but these must be separated by commas.
71
For example, if we wish to define two equivalent long options \-\-quiet and
72
\-\-silent then we would give longopts as "quiet,silent". Remember not to
73
include any whitespace.
75
If both \fIshortopts\fP and \fIlongopts\fP are given as NULL then the
76
resulting option is an untagged argument.
78
The \fIconst char* datatype\fP parameter is a descriptive string you
79
can use to customize the appearance of the argument data type in error
80
messages and so forth. It does not affect the actual data type definition
81
as that is a fixed property of the \fBarg_xxx\fP struct.
82
So for example, defining a \fIdatatype\fP of "<bar>" will result in the option
83
being display something like "\-x <bar>" or "\-\-foo=<bar>" depending upon
85
If given as NULL, the \fIdatatype\fP string will revert to the default
86
value for the particular \fBarg_xxx\fP struct.
87
You can effectively disable the default by specifying \fIdatatype\fP
90
The \fIint mincount\fP parameter specifies the minimum number of occurrences
91
that the option must appear on the command line.
92
If the option does not appear at least that many times then the parser
93
reports it as a syntax error.
94
The \fImincount\fP defaults to 0 for the \fBarg_xxx0()\fP functions and
95
1 for \fBarg_xxx1()\fP functions.
97
The \fIint maxcount\fP parameter specifies the maximum number of occurrences
98
that the option may appear on the command line.
99
Any occurrences beyond the maximum are discarded by the parser
100
reported as syntax errors.
101
The \fImaxcount\fP defaults to 1 for both the \fBarg_xxx0()\fP and \fBarg_xxx1()\fP functions.
103
The \fIconst char* glossary\fP parameter is another descriptive string
104
but this one appears in the glossary table summarizing the program's
105
command line options.
106
The glossary table is generated automatically by the \fBarg_print_glossary\fP
107
function (see later). For example, a glossary string of "the foobar factor"
108
would appear in the glossary table along side the option something like:
110
\fC\-\-foo=<bar> the foobar factor\fP
112
Specifying a NULL glossary string causes that option to be omitted from
115
See below for the exact definitions of the individual \fBarg_xxx\fP
116
structs and their constructor functions.
119
.SH FUNCTION REFERENCE
121
.SS "int arg_nullcheck (void **argtable)"
122
Returns non\-zero if the \fIargtable[]\fP array contains any NULL entries
123
up until the terminating \fBarg_end*\fP entry.
124
Returns zero otherwise.
125
.SS "int arg_parse (int argc, char **argv, void **argtable)"
126
Parse the command line arguments in \fIargv[]\fP using the command line syntax
127
specified in \fIargtable[]\fP, returning the number of errors encountered.
128
Error details are recorded in the argument table's
129
\fBarg_end\fP structure from where they can be displayed later with
130
the \fBarg_print_errors\fP function.
131
Upon a successful parse, the \fBarg_xxx\fP structures referenced in \fIargtable[]\fP
132
will contain the argument values extracted from the command line.
133
.SS "void arg_print_option (FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix)"
134
This function prints an option's syntax, as in \fB\-K|\-\-scalar=<int>\fP,
135
where the short options, long options, and datatype are all given
136
as parameters of this function.
137
It is primarily used within the \fBarg_xxx\fP structures' \fIerrorfn\fP
138
functions as a way of displaying an option's syntax inside of error
139
messages. However, it can also be used in user code if desired.
140
The \fIsuffix\fP string is provided as a convenience for appending newlines
141
and so forth to the end of the display and can be given as NULL if not required.
142
.SS "void arg_print_syntax (FILE *fp, void **argtable, const char *suffix)"
143
Prints the GNU style command line syntax for the given argument table,
144
as in: [\-abcv] [\-\-scalar=<n>] [\-o myfile] <file> [<file>]
146
The \fIsuffix\fP string is provided as a convenience for appending newlines
147
and so forth to the end of the display and can be given as NULL if not required.
148
.SS "void arg_print_syntaxv (FILE *fp, void **argtable, const char *suffix)"
149
Prints the verbose form of the command line syntax for the given argument table,
150
as in: [\-a] [\-b] [\-c] [\-\-scalar=<n>] [\-o myfile] [\-v|\-\-verbose] <file> [<file>]
152
The \fIsuffix\fP string is provided as a convenience for appending newlines
153
and so forth to the end of the display and can be given as NULL if not required.
154
.SS "void arg_print_glossary (FILE *fp, void **argtable, const char *format)"
155
Prints a glossary table describing each option in the given argument table.
156
The \fIformat\fP string is passed to printf to control the formatting of
157
each entry in the the glossary.
158
It must have exactly two "%s" format parameters as in "%\-25s %s\\n",
159
the first is for the option's syntax and the second for its glossary string.
160
If an option's glossary string is NULL then that option in omitted from
161
the glossary display.
162
.SS "void arg_print_glossary_gnu (FILE *fp, void **argtable)"
163
An alternate form of \fBarg_print_glossary()\fP that prints the glossary
164
using strict GNU formatting conventions wherein long options are vertically aligned in
165
a second column, and lines are wrapped at 80 characters.
166
.SS "void arg_print_errors (FILE *fp, struct \fBarg_end\fP *end, const char *progname)"
167
Prints the details of all errors stored in the \fIend\fP data structure.
168
The \fIprogname\fP string is prepended to each error message.
169
.SS "void arg_freetable (void ** argtable, size_t n)"
170
Deallocates the memory used by each \fBarg_xxx\fP struct referenced by \fIargtable[]\fP.
171
It does this by calling \fBfree\fP for each of the \fIn\fP pointers in the argtable array
172
and then nulling them for safety.
174
.SH "LITERAL OPTIONS (struct arg_lit)"
175
.SS Command line examples
176
\-x, \-y, \-z, \-\-help, \-\-verbose
181
struct \fBarg_hdr\fP hdr;
185
.SS Constructor Functions
188
struct \fBarg_lit* arg_lit0\fP (const char *shortopts, const char *longopts, const char *glossary)
190
struct \fBarg_lit* arg_lit1\fP (const char *shortopts, const char *longopts, const char *glossary)
192
struct \fBarg_lit* arg_litn\fP (const char *shortopts, const char *longopts, int mincount, int maxcount, const char *glossary)
194
Literal options take no argument values so all that is to be seen in the
195
\fBarg_lit\fP struct is the \fIcount\fP of the number of times the option was
196
present on the command line.
197
Upon a successful parse, \fIcount\fP is guaranteed to be within
198
the \fImincount\fP and \fImaxcount\fP limits set for the option at construction.
200
.SH "INTEGER OPTIONS (struct arg_int)"
201
.SS Command line examples
202
\-x2, \-y 7, \-z\-3, \-\-size=734, \-\-count 124
207
struct \fBarg_hdr\fP hdr;
212
.SS Constructor Functions
215
struct \fBarg_int* arg_int0\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
217
struct \fBarg_int* arg_int1\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
219
struct \fBarg_int* arg_intn\fP (const char *shortopts, const char *longopts, const char *datatype, int mincount, int maxcount, const char *glossary
221
The \fBarg_int\fP struct contains the \fIcount\fP of the number of times
222
the option was present on the command line and a pointer (\fIival\fP) to
223
an array containing the integer values given with those particular options.
224
The array is fixed at construction time to hold \fImaxcount\fP integers at most.
226
Upon a successful parse, \fIcount\fP is guaranteed to be within
227
the \fImincount\fP and \fImaxcount\fP limits set for the option at construction
228
with the appropriate values store in the \fIival\fP array.
229
The parser will not accept any values beyond that limit.
231
It is quite acceptable to set default values in the \fIival\fP array prior
232
to calling arg_parse if desired as the parser does alter \fIival\fP entries
233
for which no command line argument is received.
235
.SH "DOUBLE OPTIONS (struct arg_dbl)"
236
.SS Command line examples
237
\-x2.234, \-y 7e\-03, \-z\-3.3E+6, \-\-pi=3.1415, \-\-tolerance 1.0E-6
242
struct \fBarg_hdr\fP hdr;
247
.SS Constructor Functions
250
struct \fBarg_dbl* arg_dbl0\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
252
struct \fBarg_dbl* arg_dbl1\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
254
struct \fBarg_dbl* arg_dbln\fP (const char *shortopts, const char *longopts, const char *datatype, int mincount, int maxcount, const char *glossary
256
Like \fBarg_int\fP but the arguments values are stored as doubles in \fIdval\fP.
258
.SH "STRING OPTIONS (struct arg_str)"
259
.SS Command line examples
260
\-Dmacro, \-t mytitle, \-m "my message string", \-\-title="hello world"
265
struct \fBarg_hdr\fP hdr;
270
.SS Constructor Functions
273
struct \fBarg_str* arg_str0\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
275
struct \fBarg_str* arg_str1\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
277
struct \fBarg_str* arg_strn\fP (const char *shortopts, const char *longopts, const char *datatype, int mincount, int maxcount, const char *glossary)
279
The \fBarg_str\fP struct contains the \fIcount\fP of the number of times
280
the option was present on the command line and a pointer (\fIsval\fP) to
281
an array containing pointers to the parsed string values.
282
The array is fixed at construction time to hold \fImaxcount\fP string pointers at most.
283
These pointers in this array reference the actual command line string buffers stored in argv[],
284
so the string contents should not be should not be altered.
285
Although it is quite acceptable to set default string pointers in the \fIsval\fP array prior
286
to calling arg_parse as the parser does alter them if no matching command line argument is received.
288
.SH "REGULAR EXPRESSION OPTIONS (struct arg_rex)"
289
.SS Command line examples
290
"hello world", \-t mytitle, \-m "my message string", \-\-title="hello world"
295
struct \fBarg_hdr\fP hdr;
300
.SS Constructor Functions
303
struct \fBarg_rex* arg_rex0\fP (const char *shortopts, const char *longopts, const char *pattern, const char *datatype, int flags, const char *glossary)
305
struct \fBarg_rex* arg_rex1\fP (const char *shortopts, const char *longopts, const char *pattern, const char *datatype, int flags, const char *glossary)
307
struct \fBarg_rex* arg_rexn\fP (const char *shortopts, const char *longopts, const char *pattern, const char *datatype, int mincount, int maxcount, int flags, const char *glossary)
309
Like \fBarg_str\fP but but the string argument values are only accepted if they match a predefined regular expression.
310
The regular expression is defined by the \fIpattern\fP parameter passed to the \fIarg_rex\fP constructor.
311
The regular expression parsing is done using regex, and its behaviour can be controlled via standard regex bit flags
312
which are passed to argtable via the \fIflags\fP parameter in the \fIarg_rex\fP conbstructors. However the only two regex
313
flags that are relevant to argtable are REG_EXTENDED (use extended regular expressions rather than basic ones)
314
and REG_ICASE (ignore case). These may be logically ORed if desired.
315
This argument type is useful for matching command line keywords, particularly if case insensitive strings or pattern matching is required. See \fBregex(3)\fP for more details of regular expression matching.
317
Argtable does not support \fBarg_date\fP functionality under Microsoft Windows platforms as the
318
Microsoft compilers do include the necessary \fBregex\fP support as standard.
320
.SH "FILENAME OPTIONS (struct arg_file)"
321
.SS Command line xamples
322
\-o myfile, \-Ihome/foo/bar, \-\-input=~/doc/letter.txt, \-\-name a.out
325
struct \fBarg_file\fP
327
struct \fBarg_hdr\fP hdr;
329
const char **filename;
330
const char **basename;
331
const char **extension;
334
.SS Constructor Functions
337
struct \fBarg_file* arg_file0\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
339
struct \fBarg_file* arg_file1\fP (const char *shortopts, const char *longopts, const char *datatype, const char *glossary)
341
struct \fBarg_file* arg_filen\fP (const char *shortopts, const char *longopts, const char *datatype, int mincount, int maxcount, const char *glossary)
343
Like \fBarg_str\fP but the argument strings are presumed to
344
have filename qualities so some additional pasring is done to
345
separate out the filename's basename and extension (if they exist).
346
The three arrays filename[], basename[], extension[] each store up
347
to maxcount entries, and the i'th entry of each of these arrays
348
refer to different components of the same string buffer.
350
For instance, \fB\-o /home/heitmann/mydir/foo.txt\fP would be parsed as:
353
filename[i] = "/home/heitmann/mydir/foo.txt"
354
basename[i] = "foo.txt"
359
If the filename has no leading path then the
360
basename is the same as the filename,
361
and if no extension could be identified
362
then it is given as NULL. Note that filename
363
extensions are defined as all text following the last "."
365
Thus \fB\-o foo\fP would be parsed as:
374
As with arg_str, the string pointers in \fIfilename[]\fP,
375
\fIbasename[]\fP, and \fIextension[]\fP actually refer to
376
the original \fIargv[]\fP command line string buffers
377
so you should not attempt to alter them.
379
Note also that the parser only ever treats the filenames as strings
380
and never attempts to open them as files or perform any directory
383
.SH "DATE/TIME OPTIONS (struct arg_date)"
384
.SS Command line examples
385
12/31/04, \-d 1982\-11\-28, \-\-time 23:59
388
struct \fBarg_date\fP
390
struct \fBarg_hdr\fP hdr;
396
.SS Constructor Functions
399
struct \fBarg_date* arg_date0\fP (const char *shortopts, const char *longopts, const char *format, const char *datatype, const char *glossary)
401
struct \fBarg_date* arg_date1\fP (const char *shortopts, const char *longopts, const char *format, const char *datatype, const char *glossary)
403
struct \fBarg_date* arg_daten\fP (const char *shortopts, const char *longopts, const char *format, const char *datatype, int mincount, int maxcount, const char *glossary)
405
Accepts a timestamp string from the command line and converts it to \fIstruct tm\fP format using the system \fBstrptime\fP
406
function. The time format is defined by the \fIformat\fP string passed to the \fIarg_date\fP constructor, and is passed
407
directly to \fBstrptime\fP. See \fBstrptime(3)\fP for more details on the format string.
409
Argtable does not support \fBarg_date\fP functionality under Microsoft Windows as the
410
Microsoft compilers do include the necessary \fBstrptime\fP support as standard.
412
.SH "REMARK OPTIONS (struct arg_rem)"
417
struct \fBarg_hdr\fP hdr;
420
.SS Constructor Function
423
struct \fBarg_rem* arg_rem\fP (const char* datatype, const char* glossary)
425
The \fBarg_rem\fP struct is a dummy struct in the sense it does not
426
represent a command line option to be parsed.
427
Instead it provides a means to include additional \fIdatatype\fP and
428
\fIglossary\fP strings in the output of the \fBarg_print_syntax\fP,
429
\fBarg_print_syntaxv\fP, and \fBarg_print_glossary functions\fP.
430
As such, \fBarg_rem\fP structs may be used in the argument table to
431
insert additional lines of text into the glossary descriptions
432
or to insert additional text fields into the syntax description.
433
It has no data members apart from the mandatory \fIarg_hdr\fP struct.
435
.SH "END\-OF\-TABLE OPTIONS (struct arg_end)"
440
struct \fBarg_hdr\fP hdr;
447
.SS Constructor Function
450
struct \fBarg_end* arg_end\fP (int maxerrors)
452
The arg_end struct is primarily used to mark the end of an argument table
453
and doesn't represent any command line option.
454
Every argument table must have an \fBarg_end\fP structure as its last entry.
456
Apart from terminating the argument table, the \fBarg_end\fP structure
457
also stores the error codes generated by the \fBarg_parse\fP function
458
as it attempts to parse the command line with the given argument table.
459
The \fImaxerrors\fP parameter passed to the \fBarg_end\fP constructor
460
specifies the maximum number of errors that the structure can store.
461
Any further errors are discarded and replaced with the single error code
462
ARG_ELIMIT which is later reported to the user by the message "too many errors".
463
A \fImaxerrors\fP limit of 20 is quite reasonable.
465
The \fBarg_print_errors\fP function will print the errors stored
466
in the \fBarg_end\fP struct in the same order as they occurred,
467
so there is no need to understand the internals of the \fBarg_end\fP struct.
469
For those that are curious, the three arrays \fIerror[]\fP, \fIparent[]\fP,
470
and \fIargval[]\fP are each allocated \fImaxerrors\fP entries at construction.
471
As usual, the \fI count\fP variable gives the number of entries actually
472
stored in these arrays. The same value applies to all three arrays as
473
the i'th entry of each all refer to different aspects of the same error
476
The \fIerror[i]\fP entry holds the error code returned by the \fIhdr.scanfn\fP
477
function of the particular \fBarg_xxx\fP that is reporting the error.
478
The meaning if the code is usually known only to the issuing \fBarg_xxx\fP
479
struct. The predefined error codes that \fBarg_end\fP handles from the
480
parser itself are the exceptions.
482
The \fIparent[i]\fP entry points to the parent \fBarg_xxx\fP
483
structure that reported the error.
484
That same \fBarg_xxx\fP structure is also responsible for displaying a
485
pertinent error message when called on to do so
486
by the \fBarg_print_errors\fP function.
487
It calls the \fIhdr.errorfn\fP function of each parent \fBarg_xxx\fP struct
488
listed in the \fBarg_end\fP structure.
490
Lastly, the \fIargval[i]\fP entry points to the command line argument at
491
which the error occurred, although this may be NULL when there is no relevant
492
command line value. For instance, if an error reports a missing option
493
then there will be no matching command line argument value.
496
@prefix@/include/argtable2.h
498
@prefix@/lib/libargtable2.a
500
@prefix@/lib/libargtable2.so
502
@prefix@/man3/argtable2.3
504
@prefix@/share/doc/argtable2/
506
@prefix@/share/doc/argtable2/example/
509
Stewart Heitmann <sheitmann@users.sourceforge.net>