|
Click
|
Functions for parsing command line options. More...
#include <stdio.h>#include <stdarg.h>Go to the source code of this file.
Classes | |
| struct | Clp_Option |
| Option description. More... | |
| struct | Clp_Parser |
| Command line parser. More... | |
Macros | |
| #define | Clp_NotOption 0 |
| Clp_Next value: argument was not an option. | |
| #define | Clp_Done -1 |
| Clp_Next value: there are no more arguments. | |
| #define | Clp_BadOption -2 |
| Clp_Next value: argument was an erroneous option. | |
| #define | Clp_Error -3 |
| Clp_Next value: internal CLP error. | |
| #define | Clp_ValSize 40 |
| Minimum size of the Clp_Parser val.cs field. | |
| #define | Clp_ValIntSize 10 |
| Minimum size of the Clp_Parser val.is field. | |
| #define | Clp_DisallowOptions (1<<0) |
| Value type flag: value can't be an option string. | |
| #define | Clp_DashTerminated (1<<1) |
| #define | Clp_AllowNumbers (1<<0) |
| String list flag: allow explicit numbers. | |
| #define | Clp_StringListLong (1<<1) |
| String list flag: values have long type. | |
Value types | |
These values describe the type of an option's argument and are used in the Clp_Option val_type field. For example, if an option took integers, its Clp_Option structure would have val_type set to Clp_ValInt. | |
| #define | Clp_NoVal 0 |
| Option takes no value. | |
| #define | Clp_ValString 1 |
| Option value is an arbitrary string. | |
| #define | Clp_ValStringNotOption 2 |
| Option value is a non-option string. | |
| #define | Clp_ValBool 3 |
| Option value is a boolean. | |
| #define | Clp_ValInt 4 |
| Option value is a signed int. | |
| #define | Clp_ValUnsigned 5 |
| Option value is an unsigned int. | |
| #define | Clp_ValLong 6 |
| Option value is a signed long. | |
| #define | Clp_ValUnsignedLong 7 |
| Option value is an unsigned long. | |
| #define | Clp_ValDouble 8 |
| Option value is a double. Accepts a real number as defined by strtod(). | |
| #define | Clp_ValStringDashTerimnated 9 |
| Option value is a string terminated by a double dash. | |
| #define | Clp_ValFirstUser 10 |
| Value types >= Clp_ValFirstUser are available for user types. | |
Option flags | |
These flags are used in the Clp_Option flags field. | |
| #define | Clp_Mandatory (1<<0) |
| Option flag: value is mandatory. | |
| #define | Clp_Optional (1<<1) |
| Option flag: value is optional. | |
| #define | Clp_Negate (1<<2) |
| Option flag: option may be negated. | |
| #define | Clp_OnlyNegated (1<<3) |
| Option flag: option must be negated. | |
| #define | Clp_PreferredMatch (1<<4) |
| Option flag: prefer this option when matching. | |
Option character types | |
These flags are used in to define character types in Clp_SetOptionChar(). | |
| #define | Clp_Short (1<<0) |
| Option character begins a set of short options. | |
| #define | Clp_Long (1<<1) |
| Option character begins a long option. | |
| #define | Clp_ShortNegated (1<<2) |
| Option character begins a set of negated short options. | |
| #define | Clp_LongNegated (1<<3) |
| Option character begins a negated long option. | |
| #define | Clp_LongImplicit (1<<4) |
| Option character can begin a long option, and is part of that long option. | |
Typedefs | |
| typedef struct Clp_Option | Clp_Option |
| typedef struct Clp_Parser | Clp_Parser |
| typedef struct Clp_ParserState | Clp_ParserState |
| typedef int(* | Clp_ValParseFunc) (Clp_Parser *clp, const char *vstr, int complain, void *user_data) |
| A value parsing function. | |
| typedef void(* | Clp_ErrorHandler) (Clp_Parser *clp, const char *message) |
| A function for reporting option errors. | |
Functions | |
| Clp_Parser * | Clp_NewParser (int argc, const char *const *argv, int nopt, const Clp_Option *opt) |
| Create a new Clp_Parser. | |
| void | Clp_DeleteParser (Clp_Parser *clp) |
| Destroy a Clp_Parser object. | |
| const char * | Clp_ProgramName (Clp_Parser *clp) |
| Return clp's program name. | |
| const char * | Clp_SetProgramName (Clp_Parser *clp, const char *name) |
| Set clp's program name. | |
| Clp_ErrorHandler | Clp_SetErrorHandler (Clp_Parser *clp, Clp_ErrorHandler errh) |
| Set clp's error handler function. | |
| int | Clp_SetUTF8 (Clp_Parser *clp, int utf8) |
| Set clp's UTF-8 mode. | |
| int | Clp_OptionChar (Clp_Parser *clp, int c) |
| Return clp's treatment of character c. | |
| int | Clp_SetOptionChar (Clp_Parser *clp, int c, int type) |
| Set clp's treatment of character c. | |
| int | Clp_SetOptions (Clp_Parser *clp, int nopt, const Clp_Option *opt) |
| Set clp's option definitions. | |
| void | Clp_SetArguments (Clp_Parser *clp, int argc, const char *const *argv) |
| Set clp's arguments. | |
| int | Clp_SetOptionProcessing (Clp_Parser *clp, int on) |
| Set whether clp is searching for options. | |
| int | Clp_AddType (Clp_Parser *clp, int val_type, int flags, Clp_ValParseFunc parser, void *user_data) |
| Define a new value type for clp. | |
| int | Clp_AddStringListTypeVec (Clp_Parser *clp, int val_type, int flags, int nstrs, const char *const *strs, const int *vals) |
| Define a new string list value type for clp. | |
| int | Clp_AddStringListType (Clp_Parser *clp, int val_type, int flags,...) CLP_SENTINEL |
| Define a new string list value type for clp. | |
| int | Clp_Next (Clp_Parser *clp) |
| Parse and return the next argument from clp. | |
| const char * | Clp_Shift (Clp_Parser *clp, int allow_options) |
| Return the next argument from clp without option parsing. | |
| Clp_ParserState * | Clp_NewParserState (void) |
| Create a new Clp_ParserState. | |
| void | Clp_DeleteParserState (Clp_ParserState *state) |
| Destroy a Clp_ParserState object. | |
| void | Clp_SaveParser (const Clp_Parser *clp, Clp_ParserState *state) |
| Save clp's current state in state. | |
| void | Clp_RestoreParser (Clp_Parser *clp, const Clp_ParserState *state) |
| Restore parser state from state into clp. | |
| int | Clp_OptionError (Clp_Parser *clp, const char *format,...) |
| Report a parser error. | |
| int | Clp_vsnprintf (Clp_Parser *clp, char *str, size_t size, const char *format, va_list val) |
| Format a message. | |
| int | Clp_fprintf (Clp_Parser *clp, FILE *f, const char *format,...) |
| Print a message. | |
| int | Clp_vfprintf (Clp_Parser *clp, FILE *f, const char *format, va_list val) |
| Print a message. | |
| int | Clp_CurOptionNameBuf (Clp_Parser *clp, char *buf, int len) |
| Extract the current option as a string. | |
| const char * | Clp_CurOptionName (Clp_Parser *clp) |
| Extract the current option as a string. | |
| int | Clp_IsLong (Clp_Parser *clp, const char *long_name) |
| Test if the current option had long name name. | |
| int | Clp_IsShort (Clp_Parser *clp, int short_name) |
| Test if the current option had short name name. | |
Functions for parsing command line options.
The CLP functions are used to parse command line arguments into options. It automatically handles value parsing, error messages, long options with minimum prefix matching, short options, and negated options.
The CLP model works like this.
Unlike many command line parsing libraries, CLP steps through all arguments one at a time, rather than slurping up all options at once. This makes it meaningful to give an option more than once.
Here's an example.
Here are a couple of executions.
% ./20q --animal=cat animal is cat % ./20q --animal=cat -a dog -afish --animal bird --an=snake animal is cat animal is dog animal is fish animal is bird animal is snake % ./20q --no-vegetables no vegetables! % ./20q -v vegetables OK % ./20q -vkale vegetable is kale % ./20q -m10 10 minerals % ./20q -m foo '-m' expects an integer, not 'foo'
| #define Clp_NoVal 0 |
Option takes no value.
| #define Clp_ValString 1 |
Option value is an arbitrary string.
| #define Clp_ValStringNotOption 2 |
Option value is a non-option string.
See Clp_DisallowOptions.
| #define Clp_ValBool 3 |
Option value is a boolean.
Accepts "true", "false", "yes", "no", "1", and "0", or any prefixes thereof. The match is case-insensitive.
| #define Clp_ValInt 4 |
Option value is a signed int.
Accepts an optional "+" or "-" sign, followed by one or more digits. The digits may be include a "0x" or "0X" prefix, for a hexadecimal number, or a "0" prefix, for an octal number; otherwise it is decimal.
| #define Clp_ValUnsigned 5 |
Option value is an unsigned int.
Accepts an optional "+" sign, followed by one or more digits. The digits may be include a "0x" or "0X" prefix, for a hexadecimal number, or a "0" prefix, for an octal number; otherwise it is decimal.
| #define Clp_ValLong 6 |
Option value is a signed long.
| #define Clp_ValUnsignedLong 7 |
Option value is an unsigned long.
| #define Clp_ValDouble 8 |
Option value is a double. Accepts a real number as defined by strtod().
| #define Clp_ValStringDashTerimnated 9 |
Option value is a string terminated by a double dash.
| #define Clp_ValFirstUser 10 |
Value types >= Clp_ValFirstUser are available for user types.
| #define Clp_Mandatory (1<<0) |
Option flag: value is mandatory.
It is an error if the option has no value. This is the default if an option has arg_type != 0 and the Clp_Optional flag is not provided.
| #define Clp_Optional (1<<1) |
Option flag: value is optional.
| #define Clp_Negate (1<<2) |
Option flag: option may be negated.
–no-[long_name] will be accepted in argument lists.
| #define Clp_OnlyNegated (1<<3) |
Option flag: option must be negated.
–no-[long_name] will be accepted in argument lists, but –[long_name] will not. This is the default if long_name begins with "no-".
| #define Clp_PreferredMatch (1<<4) |
Option flag: prefer this option when matching.
Prefixes of –[long_name] should map to this option, even if other options begin with –[long_name].
| #define Clp_Short (1<<0) |
Option character begins a set of short options.
| #define Clp_Long (1<<1) |
Option character begins a long option.
| #define Clp_ShortNegated (1<<2) |
Option character begins a set of negated short options.
| #define Clp_LongNegated (1<<3) |
Option character begins a negated long option.
| #define Clp_LongImplicit (1<<4) |
Option character can begin a long option, and is part of that long option.
| #define Clp_NotOption 0 |
Clp_Next value: argument was not an option.
| #define Clp_Done -1 |
Clp_Next value: there are no more arguments.
| #define Clp_BadOption -2 |
Clp_Next value: argument was an erroneous option.
| #define Clp_Error -3 |
Clp_Next value: internal CLP error.
| #define Clp_ValSize 40 |
Minimum size of the Clp_Parser val.cs field.
| #define Clp_ValIntSize 10 |
Minimum size of the Clp_Parser val.is field.
| #define Clp_DisallowOptions (1<<0) |
Value type flag: value can't be an option string.
See Clp_AddType().
| #define Clp_AllowNumbers (1<<0) |
String list flag: allow explicit numbers.
| #define Clp_StringListLong (1<<1) |
String list flag: values have long type.
| typedef int(* Clp_ValParseFunc) (Clp_Parser *clp, const char *vstr, int complain, void *user_data) |
A value parsing function.
| clp | the parser |
| vstr | the value to be parsed |
| complain | if nonzero, report error messages via Clp_OptionError |
| user_data | user data passed to Clp_AddType() |
| typedef void(* Clp_ErrorHandler) (Clp_Parser *clp, const char *message) |
A function for reporting option errors.
| clp | the parser |
| message | error message |
| Clp_Parser * Clp_NewParser | ( | int | argc, |
| const char *const * | argv, | ||
| int | nopt, | ||
| const Clp_Option * | opt | ||
| ) |
Create a new Clp_Parser.
never
| argc | number of arguments |
| argv | argument array |
| nopt | number of option definitions |
| opt | option definition array |
The new Clp_Parser that will parse the arguments in argv according to the option definitions in opt.
The Clp_Parser is created with the following characteristics:
Clp_SetOptionChar(clp, '-', Clp_Short)). LANG environment variable contains one of the substrings "UTF-8", "UTF8", or "utf8". Override this with Clp_SetUTF8(). You may also create a Clp_Parser with no arguments or options (Clp_NewParser(0, 0, 0, 0)) and set the arguments and options later.
Returns NULL if there isn't enough memory to construct the parser.
| void Clp_DeleteParser | ( | Clp_Parser * | clp | ) |
| const char * Clp_ProgramName | ( | Clp_Parser * | clp | ) |
Return clp's program name.
| const char * Clp_SetProgramName | ( | Clp_Parser * | clp, |
| const char * | name | ||
| ) |
Set clp's program name.
| clp | the parser |
| name | new program name |
The calling program should not modify or free name until clp itself is destroyed.
| Clp_ErrorHandler Clp_SetErrorHandler | ( | Clp_Parser * | clp, |
| Clp_ErrorHandler | errh | ||
| ) |
Set clp's error handler function.
| int Clp_SetUTF8 | ( | Clp_Parser * | clp, |
| int | utf8 | ||
| ) |
Set clp's UTF-8 mode.
| clp | the parser |
| utf8 | does the parser support UTF-8? |
In UTF-8 mode, all input strings (arguments and long names for options) are assumed to be encoded via UTF-8, and all character names (Clp_SetOptionChar() and short names for options) may cover the whole Unicode range. Out of UTF-8 mode, all input strings are treated as binary, and all character names must be unsigned char values.
Furthermore, error messages in UTF-8 mode may contain Unicode quote characters.
| int Clp_OptionChar | ( | Clp_Parser * | clp, |
| int | c | ||
| ) |
Return clp's treatment of character c.
| clp | the parser |
| c | character |
Returns an integer specifying how CLP treats arguments that begin with character c. See Clp_SetOptionChar for possibilities.
| int Clp_SetOptionChar | ( | Clp_Parser * | clp, |
| int | c, | ||
| int | type | ||
| ) |
Set clp's treatment of character c.
| clp | the parser |
| c | character |
| type | option character treatment |
type specifies how CLP treats arguments that begin with character c. Possibilities are:
In UTF-8 mode, c may be any Unicode character. Otherwise, c must be an unsigned char value. The special character 0 assigns type to every character.
It is an error if c is out of range, type is illegal, or there are too many character definitions stored in clp already. The function returns -1 on error.
A double hyphen "--" always introduces a long option. This behavior cannot currently be changed with Clp_SetOptionChar().
| int Clp_SetOptions | ( | Clp_Parser * | clp, |
| int | nopt, | ||
| const Clp_Option * | opt | ||
| ) |
Set clp's option definitions.
| clp | the parser |
| nopt | number of option definitions |
| opt | option definition array |
Installs the option definitions in opt. Future option parsing will use opt to search for options.
Also checks opt's option definitions for validity. "CLP internal errors" are reported via Clp_OptionError() if:
If necessary memory cannot be allocated, this function returns -1 without modifying the parser.
| void Clp_SetArguments | ( | Clp_Parser * | clp, |
| int | argc, | ||
| const char *const * | argv | ||
| ) |
Set clp's arguments.
| clp | the parser |
| argc | number of arguments |
| argv | argument array |
Installs the arguments in argv for parsing. Future option parsing will analyze argv.
Unlike Clp_NewParser(), this function does not treat argv[0] specially. The first subsequent call to Clp_Next() will analyze argv[0].
This function also sets option processing to on, as by Clp_SetOptionProcessing(clp, 1).
| int Clp_SetOptionProcessing | ( | Clp_Parser * | clp, |
| int | on | ||
| ) |
Set whether clp is searching for options.
| clp | the parser |
| on | whether to search for options |
When option processing is off, every call to Clp_Next() returns Clp_NotOption. By default the option "--" turns off option processing and is otherwise ignored.
| int Clp_AddType | ( | Clp_Parser * | clp, |
| int | val_type, | ||
| int | flags, | ||
| Clp_ValParseFunc | parser, | ||
| void * | user_data | ||
| ) |
Define a new value type for clp.
| clp | the parser |
| val_type | value type ID |
| flags | value type flags |
| parser | parser function |
| user_data | user data for parser function |
Defines argument type val_type in parser clp. The parsing function parser will be passed argument values for type val_type. It should parse the argument into values (usually in clp->val, but sometimes elsewhere), report errors if necessary, and return whether the parse was successful.
Any prior argument parser match val_type is removed. val_type must be greater than zero.
flags specifies additional parsing flags. At the moment the only relevant flag is Clp_DisallowOptions, which means that separated values must not look like options. For example, assume argument –a/-a has mandatory value type Clp_ValStringNotOption (which has Clp_DisallowOptions). Then:
–a=–b will parse with value –b. -a–b will parse with value –b. –a –b will not parse, since the mandatory value looks like an option. -a –b will not parse, since the mandatory value looks like an option. | int Clp_AddStringListTypeVec | ( | Clp_Parser * | clp, |
| int | val_type, | ||
| int | flags, | ||
| int | nstrs, | ||
| const char *const * | strs, | ||
| const int * | vals | ||
| ) |
Define a new string list value type for clp.
| clp | the parser |
| val_type | value type ID |
| flags | string list flags |
| nstrs | number of strings in list |
| strs | array of strings |
| vals | array of values |
Defines argument type val_type in parser clp. The parsing function sets clp->val.i to an integer. The value string is matched against the strs. If there's a unique match, the corresponding entry from vals is returned. Unique prefix matches also work. Finally, if flags contains the Clp_AllowNumbers flag, then integers are also accepted.
For example:
Say that option –animal takes value type 100. Then:
–animal=cat will succeed and set clp->val.i = 1. –animal=cattle will succeed and set clp->val.i = 2. –animal=dog will succeed and set clp->val.i = 3. –animal=d will succeed and set clp->val.i = 3. –animal=c will fail, since c is ambiguous. –animal=4 will succeed and set clp->val.i = 4. | int Clp_AddStringListType | ( | Clp_Parser * | clp, |
| int | val_type, | ||
| int | flags, | ||
| ... | |||
| ) |
Define a new string list value type for clp.
| clp | the parser |
| val_type | value type ID |
| flags | string list flags |
Defines argument type val_type in parser clp. The parsing function sets clp->val.i to an integer. The value string is matched against strings provided in the ellipsis arguments. For example, the Clp_AddStringListType() call below has the same effect as the Clp_AddStringListTypeVec() call:
For example:
| int Clp_Next | ( | Clp_Parser * | clp | ) |
Parse and return the next argument from clp.
| clp | the parser |
Parse the next argument from the argument list, store information about that argument in the fields of clp, and return the option's ID.
If an argument was successfully parsed, that option's ID is returned. Other possible return values are:
The fields of clp are set as follows.
negated have_val vstr val The parsed argument is shifted off the argument list, so that sequential calls to Clp_Next() step through the argument list.
| const char * Clp_Shift | ( | Clp_Parser * | clp, |
| int | allow_options | ||
| ) |
Return the next argument from clp without option parsing.
| clp | the parser |
| allow_options | whether options will be allowed |
Remove and return the next argument from clp's argument array. If there are no arguments left, or if the next argument is an option and allow_options != 0, then returns null.
| Clp_ParserState * Clp_NewParserState | ( | void | ) |
Create a new Clp_ParserState.
A Clp_ParserState object can store a parsing state of a Clp_Parser object. This state specifies exactly how far the Clp_Parser has gotten in parsing an argument list. The Clp_SaveParser() and Clp_RestoreParser() functions can be used to save this state and then restore it later, allowing a Clp_Parser to switch among argument lists.
The initial state is empty, in that after Clp_RestoreParser(clp, state), Clp_Next(clp) would return Clp_Done.
Parser states can be saved and restored among different parser objects.
| void Clp_DeleteParserState | ( | Clp_ParserState * | state | ) |
Destroy a Clp_ParserState object.
| state | parser state |
The memory associated with state is freed.
| void Clp_SaveParser | ( | const Clp_Parser * | clp, |
| Clp_ParserState * | state | ||
| ) |
Save clp's current state in state.
| clp | the parser |
| state | parser state |
| void Clp_RestoreParser | ( | Clp_Parser * | clp, |
| const Clp_ParserState * | state | ||
| ) |
Restore parser state from state into clp.
| clp | the parser |
| state | parser state |
The parser state in state is restored into clp. The next call to Clp_Next() will return the same result as it would have at the time state was saved (probably by Clp_SaveParser(clp, state)).
A parser state contains information about arguments (argc and argv; see Clp_SetArguments()) and option processing (Clp_SetOptionProcessing()), but not about options (Clp_SetOptions()). Changes to options and value types are preserved across Clp_RestoreParser().
| int Clp_OptionError | ( | Clp_Parser * | clp, |
| const char * | format, | ||
| ... | |||
| ) |
Report a parser error.
| clp | the parser |
| format | error format |
Format an error message from format and any additional arguments in the ellipsis. The resulting error string is then printed to standard error (or passed to the error handler specified by Clp_SetErrorHandler). Returns the number of characters printed.
The following format characters are accepted:
%c int). Control characters are printed in caret notation. If the parser is in UTF-8 mode, the character is formatted in UTF-8. Otherwise, special characters are printed with backslashes and octal notation. %s const char *). %C const char *). The first character in this string is printed. If the parser is in UTF-8 mode, this may involve multiple bytes. %d int). Printed in decimal. %O %V %O, but also includes the current value, if any. %% %< %> %, Note that no flag characters, precision, or field width characters are currently supported.
| int Clp_vsnprintf | ( | Clp_Parser * | clp, |
| char * | str, | ||
| size_t | size, | ||
| const char * | format, | ||
| va_list | val | ||
| ) |
Format a message.
| clp | the parser |
| str | output string |
| size | size of output string |
| format | error format |
Format an error message from format and any additional arguments in the ellipsis, according to the Clp_OptionError formatting conventions. The resulting string is written to str. At most size characters are written to str, including a terminating null byte. The return value is the number of characters that would have been written (excluding the terminating null byte) if size were large enough to contain the entire string.
| int Clp_fprintf | ( | Clp_Parser * | clp, |
| FILE * | f, | ||
| const char * | format, | ||
| ... | |||
| ) |
Print a message.
| clp | the parser |
| f | output file |
| format | error format |
Format an error message using format and additional arguments in the ellipsis, according to the Clp_OptionError formatting conventions. The resulting message is written to f.
| int Clp_vfprintf | ( | Clp_Parser * | clp, |
| FILE * | f, | ||
| const char * | format, | ||
| va_list | val | ||
| ) |
Print a message.
| clp | the parser |
| f | output file |
| format | error format |
| val | arguments |
Format an error message using format and val, according to the Clp_OptionError formatting conventions. The resulting message is written to f.
| int Clp_CurOptionNameBuf | ( | Clp_Parser * | clp, |
| char * | buf, | ||
| int | len | ||
| ) |
Extract the current option as a string.
| clp | the parser |
| buf | output buffer |
| len | length of output buffer |
A string that looks like the last option parsed by clp is extracted into buf. The correct option characters are put into the string first, followed by the option text. The output buffer is null-terminated unless len == 0.
| const char * Clp_CurOptionName | ( | Clp_Parser * | clp | ) |
Extract the current option as a string.
| clp | the parser |
This function acts like Clp_CurOptionNameBuf(), but returns a pointer into a static buffer that will be rewritten on the next call to Clp_CurOptionName().
| int Clp_IsLong | ( | Clp_Parser * | clp, |
| const char * | long_name | ||
| ) |
Test if the current option had long name name.
| int Clp_IsShort | ( | Clp_Parser * | clp, |
| int | short_name | ||
| ) |
Test if the current option had short name name.