| 1 |
.TH POPT 3 "June 30, 1998" "" "Linux Programmer's Manual" |
| 2 |
.SH NAME |
| 3 |
popt \- Parse command line options |
| 4 |
.SH SYNOPSIS |
| 5 |
.nf |
| 6 |
.B #include <popt.h> |
| 7 |
.sp |
| 8 |
.BI "poptContext poptGetContext(const char * " name ", int " argc , |
| 9 |
.BI " const char ** "argv , |
| 10 |
.BI " const struct poptOption * " options , |
| 11 |
.BI " unsigned int " flags ); |
| 12 |
.sp |
| 13 |
.BI "void poptFreeContext(poptContext " con ); |
| 14 |
.sp |
| 15 |
.BI "void poptResetContext(poptContext " con ); |
| 16 |
.sp |
| 17 |
.BI "int poptGetNextOpt(poptContext " con ); |
| 18 |
.sp |
| 19 |
.BI "const char * poptGetOptArg(poptContext " con ); |
| 20 |
.sp |
| 21 |
.BI "const char * poptGetArg(poptContext " con ); |
| 22 |
.sp |
| 23 |
.BI "const char * poptPeekArg(poptContext " con ); |
| 24 |
.sp |
| 25 |
.BI "const char ** poptGetArgs(poptContext " con ); |
| 26 |
.sp |
| 27 |
.BI "const char *const poptStrerror(const int " error ); |
| 28 |
.sp |
| 29 |
.BI "const char * poptBadOption(poptContext " con ", int " flags ); |
| 30 |
.sp |
| 31 |
.BI "int poptReadDefaultConfig(poptContext " con ", int " flags ); |
| 32 |
.sp |
| 33 |
.BI "int poptReadConfigFile(poptContext " con ", char * " fn ); |
| 34 |
.sp |
| 35 |
.BI "int poptAddAlias(poptContext " con ", struct poptAlias " alias , |
| 36 |
.BI " int " flags ); |
| 37 |
.sp |
| 38 |
.BI "int poptParseArgvString(char * " s ", int * " argcPtr , |
| 39 |
.BI " const char *** " argvPtr ); |
| 40 |
.sp |
| 41 |
.BI "int poptDupArgv(int " argc ", const char ** " argv ", int * " argcPtr ", |
| 42 |
.BI " const char *** " argvPtr ");" |
| 43 |
.sp |
| 44 |
.BI "int poptStuffArgs(poptContext " con ", const char ** " argv ); |
| 45 |
.sp |
| 46 |
.fi |
| 47 |
.SH DESCRIPTION |
| 48 |
The popt library exists essentially for parsing command-line |
| 49 |
options. It is found superior in many ways when compared to |
| 50 |
parsing the argv array by hand or using the getopt functions |
| 51 |
.B getopt() |
| 52 |
and |
| 53 |
.B getopt_long() |
| 54 |
[see |
| 55 |
.BR getopt "(3)]." |
| 56 |
Some specific advantages of popt are: it does not utilize global |
| 57 |
.RI "variables, thus enabling multiple passes in parsing " argv |
| 58 |
.RI "; it can parse an arbitrary array of " argv "-style elements, " |
| 59 |
allowing parsing of command-line-strings from any source; |
| 60 |
it provides a standard method of option aliasing (to be |
| 61 |
discussed at length below.); it can exec external option filters; and, |
| 62 |
finally, it can automatically generate help and usage messages for |
| 63 |
the application. |
| 64 |
.sp |
| 65 |
Like |
| 66 |
.BR getopt_long() , |
| 67 |
the popt library supports short and long style options. Recall |
| 68 |
that a |
| 69 |
.B short option |
| 70 |
consists of a - character followed by a single alphanumeric character. |
| 71 |
A |
| 72 |
.BR "long option" , |
| 73 |
common in GNU utilities, consists of two - characters followed by a |
| 74 |
string made up of letters, numbers and hyphens. Long options are |
| 75 |
optionally allowed to begin with a single -, primarily to allow command-line |
| 76 |
compatibility between popt applications and X toolkit applications. |
| 77 |
Either type of option may be followed by an argument. A space separates a |
| 78 |
short option from its arguments; either a space or an = separates a long |
| 79 |
option from an argument. |
| 80 |
.sp |
| 81 |
The popt library is highly portable and should work on any POSIX |
| 82 |
platform. The latest version is distributed with rpm and is always available |
| 83 |
from: ftp://ftp.rpm.org/pub/rpm/dist. |
| 84 |
.sp |
| 85 |
It may be redistributed under the X consortium license, see the file COPYING |
| 86 |
in the popt source distribution for details. |
| 87 |
.SH "BASIC POPT USAGE" |
| 88 |
.SS "1. THE OPTION TABLE" |
| 89 |
Applications provide popt with information on their command-line |
| 90 |
options by means of an "option table," i.e., an array of |
| 91 |
.B struct poptOption |
| 92 |
structures: |
| 93 |
.sp |
| 94 |
#include <popt.h> |
| 95 |
.sp |
| 96 |
.nf |
| 97 |
struct poptOption { |
| 98 |
const char * longName; /* may be NULL */ |
| 99 |
char shortName; /* may be '\\0' */ |
| 100 |
unsigned int argInfo; /* type of argument expected after the option */ |
| 101 |
void * arg; /* depends on argInfo */ |
| 102 |
int val; /* 0 means don't return, just update arg */ |
| 103 |
const char * descrip; /* description for autohelp -- may be NULL */ |
| 104 |
const char * argDescrip; /* argument description for autohelp -- may be NULL*/ |
| 105 |
}; |
| 106 |
.fi |
| 107 |
.sp |
| 108 |
Each member of the table defines a single option that may be |
| 109 |
passed to the program. Long and short options are considered |
| 110 |
a single option that may occur in two different forms. The |
| 111 |
first two members, |
| 112 |
.IR longName " and " shortName ", define the names of the option;" |
| 113 |
the first is a long name, while the latter is a single character. |
| 114 |
.sp |
| 115 |
The |
| 116 |
.IR argInfo " member tells popt what type of argument is expected" |
| 117 |
after the option. If no argument is expected, |
| 118 |
.B POPT_ARG_NONE |
| 119 |
should be used. |
| 120 |
The valid values of |
| 121 |
.IR argInfo |
| 122 |
are shown in the following table: |
| 123 |
.sp |
| 124 |
.TS |
| 125 |
lfB lfB lfB |
| 126 |
lfB lfR lfR. |
| 127 |
Value Description arg Type |
| 128 |
POPT_ARG_NONE No argument expected int |
| 129 |
POPT_ARG_STRING No type checking to be performed char * |
| 130 |
POPT_ARG_ARGV No type checking to be performed char ** |
| 131 |
POPT_ARG_SHORT A short argument is expected short |
| 132 |
POPT_ARG_INT An integer argument is expected int |
| 133 |
POPT_ARG_LONG A long integer is expected long |
| 134 |
POPT_ARG_LONGLONG A long long integer is expected long long |
| 135 |
POPT_ARG_VAL Integer value taken from \f(CWval\fR int |
| 136 |
POPT_ARG_FLOAT A float argument is expected float |
| 137 |
POPT_ARG_DOUBLE A double argument is expected double |
| 138 |
.TE |
| 139 |
.sp |
| 140 |
For numeric values, if the \fIargInfo\fR value is bitwise or'd with one of |
| 141 |
\fBPOPT_ARGFLAG_OR\fR, \fBPOPT_ARGFLAG_AND\fR, or \fBPOPT_ARGFLAG_XOR\fR, |
| 142 |
the value is saved by performing an OR, AND, or XOR. |
| 143 |
If the \fIargInfo\fR value is bitwise or'd with \fBPOPT_ARGFLAG_NOT\fR, |
| 144 |
the value will be negated before saving. For the common operations of |
| 145 |
setting and/or clearing bits, \fBPOPT_BIT_SET\fR and \fBPOPT_BIT_CLR\fR |
| 146 |
have the appropriate flags set to perform bit operations. |
| 147 |
.sp |
| 148 |
If the \fIargInfo\fR value is bitwise or'd with \fBPOPT_ARGFLAG_ONEDASH\fR, |
| 149 |
the long argument may be given with a single - instead of two. For example, |
| 150 |
if \fB--longopt\fR is an option with \fBPOPT_ARGFLAG_ONEDASH\fR, is |
| 151 |
specified, \fB-longopt\fR is accepted as well. |
| 152 |
.sp |
| 153 |
.RI "The next element, " arg ", allows popt to automatically update " |
| 154 |
.RI "program variables when the option is used. If " arg " is " |
| 155 |
.BR NULL ", it is ignored and popt takes no special action. " |
| 156 |
Otherwise it should point to a variable of the type indicated in the |
| 157 |
.RB "right-most column of the table above. A " POPT_ARG_ARGV " arg will |
| 158 |
(re-)allocate an array of char * string pointers, append the string argument, and add a |
| 159 |
.BR NULL " sentinel at the end of the array as needed." |
| 160 |
.RB "The target char ** address of a " POPT_ARG_ARGV " arg should be initialized to " NULL "." |
| 161 |
.sp |
| 162 |
.RI "If the option takes no argument (" argInfo " is " |
| 163 |
.BR POPT_ARG_NONE "), the variable pointed to by " |
| 164 |
.IR arg " is set to 1 when the option is used. (Incidentally, it " |
| 165 |
will perhaps not escape the attention of hunt-and-peck typists that |
| 166 |
.RB "the value of " POPT_ARG_NONE " is 0.) If the option does take " |
| 167 |
an argument, the variable that |
| 168 |
.IR arg " points to is updated to reflect the value of the argument." |
| 169 |
.RB "Any string is acceptable for " POPT_ARG_STRING " and " POPT_ARG_ARGV " arguments, but " |
| 170 |
.BR POPT_ARG_INT ", " POPT_ARG_SHORT ", " POPT_ARG_LONG ", " POPT_ARG_LONGLONG ", " POPT_ARG_FLOAT ", and " |
| 171 |
.BR POPT_ARG_DOUBLE " are converted to the appropriate type, and an " |
| 172 |
error returned if the conversion fails. |
| 173 |
.sp |
| 174 |
\fBPOPT_ARG_VAL\fR causes \fIarg\fP to be set to the (integer) value of |
| 175 |
\fIval\fP when the argument is found. This is most often useful for |
| 176 |
mutually-exclusive arguments in cases where it is not an error for |
| 177 |
multiple arguments to occur and where you want the last argument |
| 178 |
specified to win; for example, "rm -i -f". \fBPOPT_ARG_VAL\fP causes |
| 179 |
the parsing function not to return a value, since the value of \fIval\fP |
| 180 |
has already been used. |
| 181 |
.sp |
| 182 |
If the \fIargInfo\fR value is bitwise or'd with \fBPOPT_ARGFLAG_OPTIONAL\fR, |
| 183 |
the argument to the long option may be omitted. If the long option |
| 184 |
is used without an argument, a default value of zero or NULL will be saved |
| 185 |
(if the arg pointer is present), otherwise behavior will be identical to |
| 186 |
a long option with argument. |
| 187 |
.sp |
| 188 |
.RI "The next option, " val ", is the value popt's parsing function |
| 189 |
should return when the option is encountered. If it is 0, the parsing |
| 190 |
function does not return a value, instead parsing the next |
| 191 |
command-line argument. |
| 192 |
.sp |
| 193 |
.RI "The last two options, " descrip " and " argDescrip " are only required |
| 194 |
if automatic help messages are desired (automatic usage messages can |
| 195 |
.RI "be generated without them). " descrip " is a text description of the |
| 196 |
.RI "argument and " argDescrip " is a short summary of the type of arguments |
| 197 |
.RI "the option expects, or NULL if the option doesn't require any |
| 198 |
arguments. |
| 199 |
.sp |
| 200 |
.RB "If popt should automatically provide " --usage " and " --help " (" -? ") |
| 201 |
.RB "options, one line in the table should be the macro " POPT_AUTOHELP ". |
| 202 |
.RB "This macro includes another option table (via " POPT_ARG_INCLUDE_TABLE |
| 203 |
; see below) in the main one which provides the table entries for these |
| 204 |
.RB "arguments. When " --usage " or " --help " are passed to programs which |
| 205 |
use popt's automatic help, popt displays the appropriate message on |
| 206 |
stderr as soon as it finds the option, and exits the program with a |
| 207 |
return code of 0. If you want to use popt's automatic help generation in |
| 208 |
a different way, you need to explicitly add the option entries to your programs |
| 209 |
.RB "option table instead of using " POPT_AUTOHELP ". |
| 210 |
.sp |
| 211 |
If the \fIargInfo\fR value is bitwise or'd with \fBPOPT_ARGFLAG_DOC_HIDDEN\fR, |
| 212 |
the argument will not be shown in help output. |
| 213 |
.sp |
| 214 |
If the \fIargInfo\fR value is bitwise or'd with \fBPOPT_ARGFLAG_SHOW_DEFAULT\fR, |
| 215 |
the initial value of the arg will be shown in help output. |
| 216 |
.sp |
| 217 |
The final structure in the table should have all the pointer values set |
| 218 |
.RB "to " NULL " and all the arithmetic values set to 0, marking the " |
| 219 |
.RB "end of the table. The macro " POPT_TABLEEND " is provided to do that. |
| 220 |
.sp |
| 221 |
There are two types of option table entries which do not specify command |
| 222 |
line options. When either of these types of entries are used, the |
| 223 |
\fIlongName\fR element must be \fBNULL\fR and the \fBshortName\fR element |
| 224 |
must be \fB'\\0'\fR. |
| 225 |
.sp |
| 226 |
The first of these special entry types allows the application to nest |
| 227 |
another option table in the current one; such nesting may extend quite |
| 228 |
deeply (the actual depth is limited by the program's stack). Including |
| 229 |
other option tables allows a library to provide a standard set of |
| 230 |
command-line options to every program which uses it (this is often done |
| 231 |
in graphical programming toolkits, for example). To do this, set |
| 232 |
the \fIargInfo\fR field to \fBPOPT_ARG_INCLUDE_TABLE\fR and the |
| 233 |
\fRarg\fR field to point to the table which is being included. If |
| 234 |
automatic help generation is being used, the \fIdescrip\fR field should |
| 235 |
contain an overall description of the option table being included. |
| 236 |
.sp |
| 237 |
The other special option table entry type tells popt to call a function (a |
| 238 |
callback) when any option in that table is found. This is especially useful |
| 239 |
when included option tables are being used, as the program which provides |
| 240 |
the top-level option table doesn't need to be aware of the other options |
| 241 |
which are provided by the included table. When a callback is set for |
| 242 |
a table, the parsing function never returns information on an option in |
| 243 |
the table. Instead, options information must be retained via the callback |
| 244 |
or by having popt set a variable through the option's \fIarg\fR field. |
| 245 |
Option callbacks should match the following prototype: |
| 246 |
.sp |
| 247 |
.nf |
| 248 |
.BI "void poptCallbackType(poptContext con, |
| 249 |
.BI " const struct poptOption * opt, |
| 250 |
.BI " const char * arg, void * data); |
| 251 |
.fi |
| 252 |
.sp |
| 253 |
The first parameter is the context which is being parsed (see the next |
| 254 |
section for information on contexts), \fIopt\fR points to the option |
| 255 |
which triggered this callback, and \fIarg\fR is the option's argument. |
| 256 |
If the option does not take an argument, \fIarg\fR is \fBNULL\fR. The |
| 257 |
final parameter, \fIdata\fR is taken from the \fIdescrip\fR field |
| 258 |
of the option table entry which defined the callback. As \fIdescrip\fR |
| 259 |
is a pointer, this allows callback functions to be passed an arbitrary |
| 260 |
set of data (though a typecast will have to be used). |
| 261 |
.sp |
| 262 |
The option table entry which defines a callback has an \fIargInfo\fR of |
| 263 |
\fBPOPT_ARG_CALLBACK\fR, an \fIarg\fR which points to the callback |
| 264 |
function, and a \fIdescrip\fR field which specifies an arbitrary pointer |
| 265 |
to be passed to the callback. |
| 266 |
.SS "2. CREATING A CONTEXT" |
| 267 |
popt can interleave the parsing of multiple command-line sets. It allows |
| 268 |
this by keeping all the state information for a particular set of |
| 269 |
command-line arguments in a |
| 270 |
.BR poptContext " data structure, an opaque type that should not be " |
| 271 |
modified outside the popt library. |
| 272 |
.sp |
| 273 |
.RB "New popt contexts are created by " poptGetContext() ":" |
| 274 |
.sp |
| 275 |
.nf |
| 276 |
.BI "poptContext poptGetContext(const char * " name ", int "argc ", |
| 277 |
.BI " const char ** "argv ", |
| 278 |
.BI " const struct poptOption * "options ", |
| 279 |
.BI " unsigned int "flags ");" |
| 280 |
.fi |
| 281 |
.sp |
| 282 |
The first parameter, |
| 283 |
.IR name ", is used only for alias handling (discussed later). It " |
| 284 |
should be the name of the application whose options are being parsed, |
| 285 |
.RB "or should be " NULL " if no option aliasing is desired. The next " |
| 286 |
two arguments specify the command-line arguments to parse. These are |
| 287 |
.RB "generally passed to " poptGetContext() " exactly as they were " |
| 288 |
.RB "passed to the program's " main() " function. The " |
| 289 |
.IR options " parameter points to the table of command-line options, " |
| 290 |
which was described in the previous section. The final parameter, |
| 291 |
.IR flags , |
| 292 |
can be any bitwise or combination of the following four values: |
| 293 |
.br |
| 294 |
.TS |
| 295 |
lfB lfB |
| 296 |
lfB lfR. |
| 297 |
Value Description |
| 298 |
POPT_CONTEXT_NO_EXEC Ignore exec expansions |
| 299 |
POPT_CONTEXT_KEEP_FIRST Do not ignore argv[0] |
| 300 |
POPT_CONTEXT_POSIXMEHARDER Options cannot follow arguments |
| 301 |
POPT_CONTEXT_ARG_OPTS Return the arguments as options of value 0 |
| 302 |
.TE |
| 303 |
.sp |
| 304 |
Here is a more detailed discussion of each flag: |
| 305 |
.sp |
| 306 |
.TP |
| 307 |
.B POPT_CONTEXT_NO_EXEC |
| 308 |
This bit disables all uses of |
| 309 |
.BR exec "(2) from within the given popt context." |
| 310 |
.sp |
| 311 |
.TP |
| 312 |
.B POPT_CONTEXT_KEEP_FIRST |
| 313 |
By default, popt ignores the value in |
| 314 |
.I argv[0] |
| 315 |
as it is typically the name of the program being run rather than a command-line |
| 316 |
argument. Specifying this flag causes popt to treat |
| 317 |
.I argv[0] |
| 318 |
as an option. |
| 319 |
.sp |
| 320 |
.TP |
| 321 |
.B POPT_CONTEXT_POSIXMEHARDER |
| 322 |
When this flag is set, it is required that all options is placed before any |
| 323 |
arguments. This flag is also in effect if any of the environment variables |
| 324 |
.B POSIXLY_CORRECT |
| 325 |
or |
| 326 |
.B POSIX_ME_HARDER |
| 327 |
is set. |
| 328 |
.sp |
| 329 |
.TP |
| 330 |
.B POPT_CONTEXT_ARG_OPTS |
| 331 |
When this flag is set, all non-option arguments will get the value 0. This means |
| 332 |
that |
| 333 |
.B poptGetNextOpt() |
| 334 |
returns 0 for all non-option arguments, while |
| 335 |
.BR poptGetArg() " returns " NULL . |
| 336 |
.sp |
| 337 |
.PP |
| 338 |
.RB "A " poptContext " keeps track of which options have already been " |
| 339 |
parsed and which remain, among other things. If a program wishes to |
| 340 |
restart option processing of a set of arguments, it can reset the |
| 341 |
.BR poptContext " by passing the context as the sole argument to " |
| 342 |
.BR poptResetContext() . |
| 343 |
.sp |
| 344 |
When argument processing is complete, the process should free the |
| 345 |
.BR poptContext " as it contains dynamically allocated components. The " |
| 346 |
.BR poptFreeContext() " function takes a " |
| 347 |
.BR poptContext " as its sole argument and frees the resources the " |
| 348 |
context is using. |
| 349 |
.sp |
| 350 |
.RB "Here are the prototypes of both " poptResetContext() " and " |
| 351 |
.BR poptFreeContext() : |
| 352 |
.sp |
| 353 |
.nf |
| 354 |
.B #include <popt.h> |
| 355 |
.BI "void poptFreeContext(poptContext " con ");" |
| 356 |
.BI "void poptResetContext(poptContext " con ");" |
| 357 |
.fi |
| 358 |
.sp |
| 359 |
.SS "3. PARSING THE COMMAND LINE" |
| 360 |
.RB "After an application has created a " poptContext ", it may begin " |
| 361 |
.RB "parsing arguments. " poptGetNextOpt() " performs the actual " |
| 362 |
argument parsing. |
| 363 |
.sp |
| 364 |
.nf |
| 365 |
.B #include <popt.h> |
| 366 |
.BI "int poptGetNextOpt(poptContext " con ");" |
| 367 |
.fi |
| 368 |
.sp |
| 369 |
Taking the context as its sole argument, this function parses the next |
| 370 |
command-line argument found. After finding the next argument in the |
| 371 |
option table, the function fills in the object pointed to by the option |
| 372 |
.RI "table entry's " arg |
| 373 |
.RB "pointer if it is not " NULL ". If the val entry for the option is " |
| 374 |
non-0, the function then returns that value. Otherwise, |
| 375 |
.BR poptGetNextOpt() " continues on to the next argument." |
| 376 |
.sp |
| 377 |
.BR poptGetNextOpt() " returns -1 when the final argument has been " |
| 378 |
parsed, and other negative values when errors occur. This makes it a |
| 379 |
good idea to |
| 380 |
.RI "keep the " val " elements in the options table greater than 0." |
| 381 |
.sp |
| 382 |
.RI "If all of the command-line options are handled through " arg |
| 383 |
pointers, command-line parsing is reduced to the following line of code: |
| 384 |
.sp |
| 385 |
.nf |
| 386 |
rc = poptGetNextOpt(poptcon); |
| 387 |
.fi |
| 388 |
.sp |
| 389 |
Many applications require more complex command-line parsing than this, |
| 390 |
however, and use the following structure: |
| 391 |
.sp |
| 392 |
.nf |
| 393 |
while ((rc = poptGetNextOpt(poptcon)) > 0) { |
| 394 |
switch (rc) { |
| 395 |
/* specific arguments are handled here */ |
| 396 |
} |
| 397 |
} |
| 398 |
.fi |
| 399 |
.sp |
| 400 |
When returned options are handled, the application needs to know the |
| 401 |
value of any arguments that were specified after the option. There are two |
| 402 |
ways to discover them. One is to ask popt to fill in a variable with the |
| 403 |
.RI "value of the option through the option table's " arg " elements. The " |
| 404 |
.RB "other is to use " poptGetOptArg() ":" |
| 405 |
.sp |
| 406 |
.nf |
| 407 |
.B #include <popt.h> |
| 408 |
.BI "char * poptGetOptArg(poptContext " con ");" |
| 409 |
.fi |
| 410 |
.sp |
| 411 |
This function returns the argument given for the final option returned by |
| 412 |
.BR poptGetNextOpt() ", or it returns " NULL " if no argument was specified." |
| 413 |
The calling function is responsible for deallocating this string. |
| 414 |
.sp |
| 415 |
.SS "4. LEFTOVER ARGUMENTS" |
| 416 |
Many applications take an arbitrary number of command-line arguments, |
| 417 |
such as a list of file names. When popt encounters an argument that does |
| 418 |
not begin with a -, it assumes it is such an argument and adds it to a list |
| 419 |
of leftover arguments. Three functions allow applications to access such |
| 420 |
arguments: |
| 421 |
.nf |
| 422 |
.HP |
| 423 |
.BI "const char * poptGetArg(poptContext " con ");" |
| 424 |
.fi |
| 425 |
This function returns the next leftover argument and marks it as |
| 426 |
processed. |
| 427 |
.PP |
| 428 |
.nf |
| 429 |
.HP |
| 430 |
.BI "const char * poptPeekArg(poptContext " con ");" |
| 431 |
.fi |
| 432 |
The next leftover argument is returned but not marked as processed. |
| 433 |
This allows an application to look ahead into the argument list, |
| 434 |
without modifying the list. |
| 435 |
.PP |
| 436 |
.nf |
| 437 |
.HP |
| 438 |
.BI "const char ** poptGetArgs(poptContext " con ");" |
| 439 |
.fi |
| 440 |
All the leftover arguments are returned in a manner identical to |
| 441 |
.IR argv ". The final element in the returned array points to " |
| 442 |
.BR NULL ", indicating the end of the arguments. |
| 443 |
.sp |
| 444 |
.SS "5. AUTOMATIC HELP MESSAGES" |
| 445 |
The \fBpopt\fR library can automatically generate help messages which |
| 446 |
describe the options a program accepts. There are two types of help |
| 447 |
messages which can be generated. Usage messages are a short messages |
| 448 |
which lists valid options, but does not describe them. Help messages |
| 449 |
describe each option on one (or more) lines, resulting in a longer, but |
| 450 |
more useful, message. Whenever automatic help messages are used, the |
| 451 |
\fBdescrip\fR and \fBargDescrip\fR fields \fBstruct poptOption\fR members |
| 452 |
should be filled in for each option. |
| 453 |
.sp |
| 454 |
The \fBPOPT_AUTOHELP\fR macro makes it easy to add \fB--usage\fR and |
| 455 |
\fB--help\fR messages to your program, and is described in part 1 |
| 456 |
of this man page. If more control is needed over your help messages, |
| 457 |
the following two functions are available: |
| 458 |
.sp |
| 459 |
.nf |
| 460 |
.B #include <popt.h> |
| 461 |
.BI "void poptPrintHelp(poptContext " con ", FILE * " f ", int " flags "); |
| 462 |
.BI "void poptPrintUsage(poptContext " con ", FILE * " f ", int " flags "); |
| 463 |
.fi |
| 464 |
.sp |
| 465 |
\fBpoptPrintHelp()\fR displays the standard help message to the stdio file |
| 466 |
descriptor f, while \fBpoptPrintUsage()\fR displays the shorter usage |
| 467 |
message. Both functions currently ignore the \fBflags\fR argument; it is |
| 468 |
there to allow future changes. |
| 469 |
.sp |
| 470 |
.SH "ERROR HANDLING" |
| 471 |
All of the popt functions that can return errors return integers. |
| 472 |
When an error occurs, a negative error code is returned. The |
| 473 |
following table summarizes the error codes that occur: |
| 474 |
.sp |
| 475 |
.TS |
| 476 |
lfB lfB |
| 477 |
lfB lfR. |
| 478 |
Error Description |
| 479 |
POPT_ERROR_NOARG Argument missing for an option. |
| 480 |
POPT_ERROR_BADOPT Option's argument couldn't be parsed. |
| 481 |
POPT_ERROR_UNWANTEDARG Option does not take an argument. |
| 482 |
POPT_ERROR_OPTSTOODEEP Option aliasing nested too deeply. |
| 483 |
POPT_ERROR_BADQUOTE Quotations do not match. |
| 484 |
POPT_ERROR_ERRNO errno set, use strerror(errno). |
| 485 |
POPT_ERROR_BADNUMBER Option couldn't be converted to number. |
| 486 |
POPT_ERROR_OVERFLOW A given number was too big or small. |
| 487 |
POPT_ERROR_BADOPERATION Mutually exclusive logical operations requested. |
| 488 |
POPT_ERROR_NULLARG opt->arg should not be NULL. |
| 489 |
POPT_ERROR_MALLOC Memory allocation failed. |
| 490 |
POPT_ERROR_BADCONFIG Config file failed sanity test. |
| 491 |
.TE |
| 492 |
.sp |
| 493 |
Here is a more detailed discussion of each error: |
| 494 |
.sp |
| 495 |
.TP |
| 496 |
.B POPT_ERROR_NOARG |
| 497 |
An option that requires an argument was specified on the command |
| 498 |
line, but no argument was given. This can be returned only by |
| 499 |
.BR poptGetNextOpt() . |
| 500 |
.sp |
| 501 |
.TP |
| 502 |
.B POPT_ERROR_BADOPT |
| 503 |
.RI "An option was specified in " argv " but is not in the option |
| 504 |
.RB "table. This error can be returned only from " poptGetNextOpt() . |
| 505 |
.sp |
| 506 |
.TP |
| 507 |
.B POPT_ERROR_OPTSTOODEEP |
| 508 |
A set of option aliases is nested too deeply. Currently, popt |
| 509 |
follows options only 10 levels |
| 510 |
.B (POPT_OPTION_DEPTH) |
| 511 |
to prevent infinite recursion. Only |
| 512 |
.B poptGetNextOpt() |
| 513 |
can return this error. |
| 514 |
.sp |
| 515 |
.TP |
| 516 |
.B POPT_ERROR_BADQUOTE |
| 517 |
A parsed string has a quotation mismatch (such as a single quotation |
| 518 |
.RB "mark). " poptParseArgvString() ", " poptReadConfigFile() ", or " |
| 519 |
.BR poptReadDefaultConfig() " can return this error." |
| 520 |
.sp |
| 521 |
.TP |
| 522 |
.B POPT_ERROR_ERRNO |
| 523 |
.RI "A system call returned with an error, and " errno " still |
| 524 |
contains the error from the system call. Both |
| 525 |
.BR poptReadConfigFile() " and " poptReadDefaultConfig() " can " |
| 526 |
return this error. |
| 527 |
.sp |
| 528 |
.TP |
| 529 |
.B POPT_ERROR_BADNUMBER |
| 530 |
A conversion from a string to a number (int or long) failed due |
| 531 |
to the string containing non-numeric characters. This occurs when |
| 532 |
.BR poptGetNextOpt() " is processing an argument of type " |
| 533 |
.BR POPT_ARG_INT ", " POPT_ARG_SHORT ", " POPT_ARG_LONG ", " POPT_ARG_LONGLONG ", " |
| 534 |
.BR POPT_ARG_FLOAT ", or " POPT_ARG_DOUBLE "." |
| 535 |
.sp |
| 536 |
.TP |
| 537 |
.B POPT_ERROR_OVERFLOW |
| 538 |
A string-to-number conversion failed because the number was too |
| 539 |
.RB "large or too small. Like " POPT_ERROR_BADNUMBER ", this error |
| 540 |
.RB "can occur only when " poptGetNextOpt() " is processing an " |
| 541 |
.RB "argument of type " POPT_ARG_INT ", " POPT_ARG_SHORT ", " POPT_ARG_LONG ", " POPT_ARG_LONGLONG ", " |
| 542 |
.BR POPT_ARG_FLOAT ", or " POPT_ARG_DOUBLE "." |
| 543 |
.sp |
| 544 |
.TP |
| 545 |
.B POPT_ERROR_BADOPERATION |
| 546 |
More than one logical operation (AND, OR, XOR) was specified for an option, or |
| 547 |
.B POPT_ARGFLAG_RANDOM |
| 548 |
was specified but the platform does not support the |
| 549 |
.B random() |
| 550 |
function. This can be returned only by |
| 551 |
.BR poptSaveLongLong() ", " poptSaveLong() ", " poptSaveInt() ", " |
| 552 |
.BR poptSaveShort() " and " poptGetNextOpt() "." |
| 553 |
.sp |
| 554 |
.TP |
| 555 |
.B POPT_ERROR_NULLARG |
| 556 |
An operation was invoked on a null target |
| 557 |
.I arg |
| 558 |
(including zero-length string arguments). In the |
| 559 |
.B poptBitsArgs() |
| 560 |
case, this includes an empty leftover |
| 561 |
.I argv |
| 562 |
array. This can only be returned by the |
| 563 |
.B poptBits*() |
| 564 |
and |
| 565 |
.B poptSave*() |
| 566 |
functions, |
| 567 |
.B poptConfigFileToString() |
| 568 |
and |
| 569 |
.BR poptGetNextOpt() . |
| 570 |
.sp |
| 571 |
.TP |
| 572 |
.B POPT_ERROR_MALLOC |
| 573 |
Memory allocation failed. This can only be returned by |
| 574 |
.BR poptReadFile() , |
| 575 |
.BR poptDupArgv() , |
| 576 |
.BR poptParseArgvString() , |
| 577 |
.B poptConfigFileToString() |
| 578 |
and |
| 579 |
.BR poptGetNextOpt() . |
| 580 |
.sp |
| 581 |
.TP |
| 582 |
.B POPT_ERROR_BADCONFIG |
| 583 |
The popt configuration files are corrupted. This can only be returned by |
| 584 |
.B poptReadConfigFile() |
| 585 |
and |
| 586 |
.BR poptReadConfigFiles() . |
| 587 |
.sp |
| 588 |
.PP |
| 589 |
Two functions are available to make it easy for applications to provide |
| 590 |
good error messages. |
| 591 |
.HP |
| 592 |
.nf |
| 593 |
.BI "const char *const poptStrerror(const int " error ");" |
| 594 |
.fi |
| 595 |
This function takes a popt error code and returns a string describing |
| 596 |
.RB "the error, just as with the standard " strerror() " function." |
| 597 |
.PP |
| 598 |
.HP |
| 599 |
.nf |
| 600 |
.BI "const char * poptBadOption(poptContext " con ", int " flags ");" |
| 601 |
.fi |
| 602 |
.RB "If an error occurred during " poptGetNextOpt() ", this function " |
| 603 |
.RI "returns the option that caused the error. If the " flags " argument" |
| 604 |
.RB "is set to " POPT_BADOPTION_NOALIAS ", the outermost option is " |
| 605 |
.RI "returned. Otherwise, " flags " should be 0, and the option that is " |
| 606 |
returned may have been specified through an alias. |
| 607 |
.PP |
| 608 |
These two functions make popt error handling trivial for most |
| 609 |
applications. When an error is detected from most of the functions, |
| 610 |
an error message is printed along with the error string from |
| 611 |
.BR poptStrerror() ". When an error occurs during argument parsing, " |
| 612 |
code similar to the following displays a useful error message: |
| 613 |
.sp |
| 614 |
.nf |
| 615 |
fprintf(stderr, "%s: %s\\n", |
| 616 |
poptBadOption(optCon, POPT_BADOPTION_NOALIAS), |
| 617 |
poptStrerror(rc)); |
| 618 |
.fi |
| 619 |
.sp |
| 620 |
.SH "OPTION ALIASING" |
| 621 |
.RB "One of the primary benefits of using popt over " getopt() " is the " |
| 622 |
ability to use option aliasing. This lets the user specify options that |
| 623 |
popt expands into other options when they are specified. If the standard |
| 624 |
.RB "grep program made use of popt, users could add a " --text " option " |
| 625 |
.RB "that expanded to " "-i -n -E -2" " to let them more easily find " |
| 626 |
information in text files. |
| 627 |
.sp |
| 628 |
.SS "1. SPECIFYING ALIASES" |
| 629 |
.RI "Aliases are normally specified in two places: " /etc/popt |
| 630 |
.RB "and the " .popt " file in the user's home directory (found through " |
| 631 |
.RB "the " HOME " environment variable). Both files have the same format, " |
| 632 |
an arbitrary number of lines formatted like this: |
| 633 |
.sp |
| 634 |
.IB appname " alias " newoption "" " expansion" |
| 635 |
.sp |
| 636 |
.RI "The " appname " is the name of the application, which must be the " |
| 637 |
.RI "same as the " name " parameter passed to " |
| 638 |
.BR poptGetContext() ". This allows each file to specify aliases for " |
| 639 |
.RB "multiple programs. The " alias " keyword specifies that an alias is " |
| 640 |
being defined; currently popt configuration files support only aliases, but |
| 641 |
other abilities may be added in the future. The next option is the option |
| 642 |
that should be aliased, and it may be either a short or a long option. The |
| 643 |
rest of the line specifies the expansion for the alias. It is parsed |
| 644 |
similarly to a shell command, which allows \\, ", and ' to be used for |
| 645 |
quoting. If a backslash is the final character on a line, the next line |
| 646 |
in the file is assumed to be a logical continuation of the line containing |
| 647 |
the backslash, just as in shell. |
| 648 |
.sp |
| 649 |
.RB "The following entry would add a " --text " option to the grep command, " |
| 650 |
as suggested at the beginning of this section. |
| 651 |
.sp |
| 652 |
.B "grep alias --text -i -n -E -2" |
| 653 |
.SS "2. ENABLING ALIASES" |
| 654 |
.RB "An application must enable alias expansion for a " poptContext |
| 655 |
.RB "before calling " poptGetNextArg() " for the first time. There are " |
| 656 |
three functions that define aliases for a context: |
| 657 |
.HP |
| 658 |
.nf |
| 659 |
.BI "int poptReadDefaultConfig(poptContext " con ", int " flags ");" |
| 660 |
.fi |
| 661 |
.RI "This function reads aliases from " /etc/popt " and the " |
| 662 |
.BR .popt " file in the user's home directory. Currently, " |
| 663 |
.IR flags " should be " |
| 664 |
.BR NULL ", as it is provided only for future expansion." |
| 665 |
.PP |
| 666 |
.HP |
| 667 |
.nf |
| 668 |
.BI "int poptReadConfigFile(poptContext " con ", char * " fn ");" |
| 669 |
.fi |
| 670 |
.RI "The file specified by " fn " is opened and parsed as a popt " |
| 671 |
configuration file. This allows programs to use program-specific |
| 672 |
configuration files. |
| 673 |
.PP |
| 674 |
.HP |
| 675 |
.nf |
| 676 |
.BI "int poptAddAlias(poptContext " con ", struct poptAlias " alias ", |
| 677 |
.BI " int " flags ");" |
| 678 |
.fi |
| 679 |
Occasionally, processes want to specify aliases without having to |
| 680 |
read them from a configuration file. This function adds a new alias |
| 681 |
.RI "to a context. The " flags " argument should be 0, as it is " |
| 682 |
currently reserved for future expansion. The new alias is specified |
| 683 |
.RB "as a " "struct poptAlias" ", which is defined as:" |
| 684 |
.sp |
| 685 |
.nf |
| 686 |
struct poptAlias { |
| 687 |
const char * longName; /* may be NULL */ |
| 688 |
char shortName; /* may be '\\0' */ |
| 689 |
int argc; |
| 690 |
const char ** argv; /* must be free()able */ |
| 691 |
}; |
| 692 |
.fi |
| 693 |
.sp |
| 694 |
.RI "The first two elements, " longName " and " shortName ", specify " |
| 695 |
.RI "the option that is aliased. The final two, " argc " and " argv "," |
| 696 |
define the expansion to use when the aliases option is encountered. |
| 697 |
.PP |
| 698 |
.SH "PARSING ARGUMENT STRINGS" |
| 699 |
Although popt is usually used for parsing arguments already divided into |
| 700 |
.RI "an " argv "-style array, some programs need to parse strings that " |
| 701 |
are formatted identically to command lines. To facilitate this, popt |
| 702 |
provides a function that parses a string into an array of strings, |
| 703 |
using rules similar to normal shell parsing. |
| 704 |
.sp |
| 705 |
.nf |
| 706 |
.B "#include <popt.h>" |
| 707 |
.BI "int poptParseArgvString(char * " s ", int * " argcPtr ", |
| 708 |
.BI " char *** " argvPtr ");" |
| 709 |
.BI "int poptDupArgv(int " argc ", const char ** " argv ", int * " argcPtr ", |
| 710 |
.BI " const char *** " argvPtr ");" |
| 711 |
.fi |
| 712 |
.sp |
| 713 |
.RI "The string s is parsed into an " argv "-style array. The integer " |
| 714 |
.RI "pointed to by the " argcPtr " parameter contains the number of elements " |
| 715 |
.RI "parsed, and the final " argvPtr " parameter contains the address of the" |
| 716 |
newly created array. |
| 717 |
.RB "The routine " poptDupArgv() " can be used to make a copy of an existing " |
| 718 |
argument array. |
| 719 |
.sp |
| 720 |
.RI "The " argvPtr |
| 721 |
.RB "created by " poptParseArgvString() " or " poptDupArgv() " is suitable to pass directly " |
| 722 |
.RB "to " poptGetContext() . |
| 723 |
Both routines return a single dynamically allocated contiguous |
| 724 |
.RB "block of storage and should be " free() "ed when the application is" |
| 725 |
finished with the storage. |
| 726 |
.SH "HANDLING EXTRA ARGUMENTS" |
| 727 |
Some applications implement the equivalent of option aliasing but need |
| 728 |
.RB "to do so through special logic. The " poptStuffArgs() " function " |
| 729 |
allows an application to insert new arguments into the current |
| 730 |
.BR poptContext . |
| 731 |
.sp |
| 732 |
.nf |
| 733 |
.B "#include <popt.h>" |
| 734 |
.BI "int poptStuffArgs(poptContext "con ", const char ** " argv ");" |
| 735 |
.fi |
| 736 |
.sp |
| 737 |
.RI "The passed " argv |
| 738 |
.RB "must have a " NULL " pointer as its final element. When " |
| 739 |
.BR poptGetNextOpt() " is next called, the " |
| 740 |
"stuffed" arguments are the first to be parsed. popt returns to the |
| 741 |
normal arguments once all the stuffed arguments have been exhausted. |
| 742 |
.SH "EXAMPLE" |
| 743 |
The following example is a simplified version of the program "robin" |
| 744 |
which appears in Chapter 15 of the text cited below. Robin has |
| 745 |
been stripped of everything but its argument-parsing logic, slightly |
| 746 |
reworked, and renamed "parse." It may prove useful in illustrating |
| 747 |
at least some of the features of the extremely rich popt library. |
| 748 |
.sp |
| 749 |
.nf |
| 750 |
#include <popt.h> |
| 751 |
#include <stdio.h> |
| 752 |
#include <stdlib.h> |
| 753 |
|
| 754 |
void usage(poptContext optCon, int exitcode, char *error, char *addl) { |
| 755 |
poptPrintUsage(optCon, stderr, 0); |
| 756 |
if (error) fprintf(stderr, "%s: %s\\n", error, addl); |
| 757 |
exit(exitcode); |
| 758 |
} |
| 759 |
|
| 760 |
int main(int argc, char *argv[]) { |
| 761 |
int c; /* used for argument parsing */ |
| 762 |
int i = 0; /* used for tracking options */ |
| 763 |
int speed = 0; /* used in argument parsing to set speed */ |
| 764 |
int raw = 0; /* raw mode? */ |
| 765 |
int j; |
| 766 |
char buf[BUFSIZ+1]; |
| 767 |
const char *portname; |
| 768 |
poptContext optCon; /* context for parsing command-line options */ |
| 769 |
|
| 770 |
struct poptOption optionsTable[] = { |
| 771 |
{ "bps", 'b', POPT_ARG_INT, &speed, 0, |
| 772 |
"signaling rate in bits-per-second", "BPS" }, |
| 773 |
{ "crnl", 'c', 0, 0, 'c', |
| 774 |
"expand cr characters to cr/lf sequences", NULL }, |
| 775 |
{ "hwflow", 'h', 0, 0, 'h', |
| 776 |
"use hardware (RTS/CTS) flow control", NULL }, |
| 777 |
{ "noflow", 'n', 0, 0, 'n', |
| 778 |
"use no flow control", NULL }, |
| 779 |
{ "raw", 'r', 0, &raw, 0, |
| 780 |
"don't perform any character conversions", NULL }, |
| 781 |
{ "swflow", 's', 0, 0, 's', |
| 782 |
"use software (XON/XOF) flow control", NULL } , |
| 783 |
POPT_AUTOHELP |
| 784 |
{ NULL, 0, 0, NULL, 0 } |
| 785 |
}; |
| 786 |
|
| 787 |
optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); |
| 788 |
poptSetOtherOptionHelp(optCon, "[OPTIONS]* <port>"); |
| 789 |
|
| 790 |
if (argc < 2) { |
| 791 |
poptPrintUsage(optCon, stderr, 0); |
| 792 |
exit(1); |
| 793 |
} |
| 794 |
|
| 795 |
/* Now do options processing, get portname */ |
| 796 |
while ((c = poptGetNextOpt(optCon)) >= 0) { |
| 797 |
switch (c) { |
| 798 |
case 'c': |
| 799 |
buf[i++] = 'c'; |
| 800 |
break; |
| 801 |
case 'h': |
| 802 |
buf[i++] = 'h'; |
| 803 |
break; |
| 804 |
case 's': |
| 805 |
buf[i++] = 's'; |
| 806 |
break; |
| 807 |
case 'n': |
| 808 |
buf[i++] = 'n'; |
| 809 |
break; |
| 810 |
} |
| 811 |
} |
| 812 |
portname = poptGetArg(optCon); |
| 813 |
if((portname == NULL) || !(poptPeekArg(optCon) == NULL)) |
| 814 |
usage(optCon, 1, "Specify a single port", ".e.g., /dev/cua0"); |
| 815 |
|
| 816 |
if (c < -1) { |
| 817 |
/* an error occurred during option processing */ |
| 818 |
fprintf(stderr, "%s: %s\\n", |
| 819 |
poptBadOption(optCon, POPT_BADOPTION_NOALIAS), |
| 820 |
poptStrerror(c)); |
| 821 |
return 1; |
| 822 |
} |
| 823 |
|
| 824 |
/* Print out options, portname chosen */ |
| 825 |
printf("Options chosen: "); |
| 826 |
for(j = 0; j < i ; j++) |
| 827 |
printf("-%c ", buf[j]); |
| 828 |
if(raw) printf("-r "); |
| 829 |
if(speed) printf("-b %d ", speed); |
| 830 |
printf("\\nPortname chosen: %s\\n", portname); |
| 831 |
|
| 832 |
poptFreeContext(optCon); |
| 833 |
exit(0); |
| 834 |
} |
| 835 |
.fi |
| 836 |
.sp |
| 837 |
RPM, a popular Linux package management program, makes heavy use |
| 838 |
of popt's features. Many of its command-line arguments are implemented |
| 839 |
through popt aliases, which makes RPM an excellent example of how to |
| 840 |
take advantage of the popt library. For more information on RPM, see |
| 841 |
http://www.rpm.org. The popt source code distribution includes test |
| 842 |
program(s) which use all of the features of the popt libraries in |
| 843 |
various ways. If a feature isn't working for you, the popt test code |
| 844 |
is the first place to look. |
| 845 |
.SH BUGS |
| 846 |
None presently known. |
| 847 |
.SH AUTHOR |
| 848 |
Erik W. Troan <[email protected]> |
| 849 |
.PP |
| 850 |
This man page is derived in part from |
| 851 |
.IR "Linux Application Development" |
| 852 |
by Michael K. Johnson and Erik W. Troan, Copyright (c) 1998 by Addison |
| 853 |
Wesley Longman, Inc., and included in the popt documentation with the |
| 854 |
permission of the Publisher and the appreciation of the Authors. |
| 855 |
.PP |
| 856 |
Thanks to Robert Lynch for his extensive work on this man page. |
| 857 |
.SH "SEE ALSO" |
| 858 |
.BR getopt (3) |
| 859 |
.sp |
| 860 |
.IR "Linux Application Development" ", by Michael K. Johnson and " |
| 861 |
Erik W. Troan (Addison-Wesley, 1998; ISBN 0-201-30821-5), Chapter 24. |
| 862 |
.sp |
| 863 |
.BR popt.ps " is a Postscript version of the above cited book " |
| 864 |
chapter. It can be found in the source archive for popt available at: |
| 865 |
ftp://ftp.rpm.org/pub/rpm. |