Protocol++® (Protocolpp®)
v5.6.2
|
Checks argument vectors for validity and parses them into data structures that are easier to work with. More...
#include <optionparser.h>
Classes | |
struct | Action |
class | StoreOptionAction |
Public Member Functions | |
Parser () | |
Creates a new Parser. | |
Parser (bool gnu, const Descriptor usage[], int argc, const char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) | |
Creates a new Parser and immediately parses the given argument vector. More... | |
Parser (bool gnu, const Descriptor usage[], int argc, char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) | |
Parser(...) with non-const argv. | |
Parser (const Descriptor usage[], int argc, const char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) | |
POSIX Parser(...) (gnu==false). | |
Parser (const Descriptor usage[], int argc, char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) | |
POSIX Parser(...) (gnu==false) with non-const argv. | |
void | parse (bool gnu, const Descriptor usage[], int argc, const char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) |
Parses the given argument vector. More... | |
void | parse (bool gnu, const Descriptor usage[], int argc, char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) |
parse() with non-const argv. | |
void | parse (const Descriptor usage[], int argc, const char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) |
POSIX parse() (gnu==false). | |
void | parse (const Descriptor usage[], int argc, char **argv, Option options[], Option buffer[], int min_abbr_len=0, bool single_minus_longopt=false, int bufmax=-1) |
POSIX parse() (gnu==false) with non-const argv. | |
int | optionsCount () |
Returns the number of valid Option objects in buffer []. More... | |
int | nonOptionsCount () |
Returns the number of non-option arguments that remained at the end of the most recent parse() that actually encountered non-option arguments. More... | |
const char ** | nonOptions () |
Returns a pointer to an array of non-option arguments (only valid if nonOptionsCount() >0 ). More... | |
const char * | nonOption (int i) |
Returns nonOptions()[i] (without checking if i is in range!). | |
bool | error () |
Returns true if an unrecoverable error occurred while parsing options. More... | |
Friends | |
struct | Stats |
Checks argument vectors for validity and parses them into data structures that are easier to work with.
|
inline |
Creates a new Parser and immediately parses the given argument vector.
gnu | if true, parse() will not stop at the first non-option argument. Instead it will reorder arguments so that all non-options are at the end. This is the default behaviour of GNU getopt() but is not conforming to POSIX. Note, that once the argument vector has been reordered, the gnu flag will have no further effect on this argument vector. So it is enough to pass gnu==true when creating Stats. |
usage | Array of Descriptor objects that describe the options to support. The last entry of this array must have 0 in all fields. |
argc | The number of elements from argv that are to be parsed. If you pass -1, the number will be determined automatically. In that case the argv list must end with a NULL pointer. |
argv | The arguments to be parsed. If you pass -1 as argc the last pointer in the argv list must be NULL to mark the end. |
options | Each entry is the first element of a linked list of Options. Each new option that is parsed will be appended to the list specified by that Option's Descriptor::index. If an entry is not yet used (i.e. the Option is invalid), it will be replaced rather than appended to. The minimum length of this array is the greatest Descriptor::index value that occurs in usage PLUS ONE. |
buffer | Each argument that is successfully parsed (including unknown arguments, if they have a Descriptor whose CheckArg does not return ARG_ILLEGAL) will be stored in this array. parse() scans the array for the first invalid entry and begins writing at that index. You can pass bufmax to limit the number of options stored. |
min_abbr_len | Passing a value min_abbr_len > 0 enables abbreviated long options. The parser will match a prefix of a long option as if it was the full long option (e.g. –foob=10 will be interpreted as if it was –foobar=10 ), as long as the prefix has at least min_abbr_len characters (not counting the – ) and is unambiguous. Be careful if combining min_abbr_len=1 with single_minus_longopt=true because the ambiguity check does not consider short options and abbreviated single minus long options will take precedence over short options. |
single_minus_longopt | Passing true for this option allows long options to begin with a single minus. The double minus form will still be recognized. Note that single minus long options take precedence over short options and short option groups. E.g. -file would be interpreted as –file and not as -f -i -l -e (assuming a long option named "file" exists). |
bufmax | The greatest index in the buffer [] array that parse() will write to is bufmax-1 . If there are more options, they will be processed (in particular their CheckArg will be called) but not stored. If you used Stats::buffer_max to dimension this array, you can pass -1 (or not pass bufmax at all) which tells parse() that the buffer is "large enough". |
options
and buffer
store Option objects, not pointers. Therefore it is not possible for the same object to be in both arrays. For those options that are found in both buffer
[] and options
[] the respective objects are independent copies. And only the objects in options
[] are properly linked via Option::next() and Option::prev(). You can iterate over buffer
[] to process all options in the order they appear in the argument vector, but if you want access to the other Options with the same Descriptor::index, then you must access the linked list via options
[]. You can get the linked list in options from a buffer object via something like options
[buffer[i].index()].
|
inline |
Returns true
if an unrecoverable error occurred while parsing options.
An illegal argument to an option (i.e. CheckArg returns ARG_ILLEGAL) is an unrecoverable error that aborts the parse. Unknown options are only an error if their CheckArg function returns ARG_ILLEGAL. Otherwise they are collected. In that case if you want to exit the program if either an illegal argument or an unknown option has been passed, use code like this
|
inline |
Returns a pointer to an array of non-option arguments (only valid if nonOptionsCount() >0
).
|
inline |
Returns the number of non-option arguments that remained at the end of the most recent parse() that actually encountered non-option arguments.
|
inline |
Returns the number of valid Option objects in buffer
[].
|
inline |
Parses the given argument vector.
gnu | if true, parse() will not stop at the first non-option argument. Instead it will reorder arguments so that all non-options are at the end. This is the default behaviour of GNU getopt() but is not conforming to POSIX. Note, that once the argument vector has been reordered, the gnu flag will have no further effect on this argument vector. So it is enough to pass gnu==true when creating Stats. |
usage | Array of Descriptor objects that describe the options to support. The last entry of this array must have 0 in all fields. |
argc | The number of elements from argv that are to be parsed. If you pass -1, the number will be determined automatically. In that case the argv list must end with a NULL pointer. |
argv | The arguments to be parsed. If you pass -1 as argc the last pointer in the argv list must be NULL to mark the end. |
options | Each entry is the first element of a linked list of Options. Each new option that is parsed will be appended to the list specified by that Option's Descriptor::index. If an entry is not yet used (i.e. the Option is invalid), it will be replaced rather than appended to. The minimum length of this array is the greatest Descriptor::index value that occurs in usage PLUS ONE. |
buffer | Each argument that is successfully parsed (including unknown arguments, if they have a Descriptor whose CheckArg does not return ARG_ILLEGAL) will be stored in this array. parse() scans the array for the first invalid entry and begins writing at that index. You can pass bufmax to limit the number of options stored. |
min_abbr_len | Passing a value min_abbr_len > 0 enables abbreviated long options. The parser will match a prefix of a long option as if it was the full long option (e.g. –foob=10 will be interpreted as if it was –foobar=10 ), as long as the prefix has at least min_abbr_len characters (not counting the – ) and is unambiguous. Be careful if combining min_abbr_len=1 with single_minus_longopt=true because the ambiguity check does not consider short options and abbreviated single minus long options will take precedence over short options. |
single_minus_longopt | Passing true for this option allows long options to begin with a single minus. The double minus form will still be recognized. Note that single minus long options take precedence over short options and short option groups. E.g. -file would be interpreted as –file and not as -f -i -l -e (assuming a long option named "file" exists). |
bufmax | The greatest index in the buffer [] array that parse() will write to is bufmax-1 . If there are more options, they will be processed (in particular their CheckArg will be called) but not stored. If you used Stats::buffer_max to dimension this array, you can pass -1 (or not pass bufmax at all) which tells parse() that the buffer is "large enough". |
options
and buffer
store Option objects, not pointers. Therefore it is not possible for the same object to be in both arrays. For those options that are found in both buffer
[] and options
[] the respective objects are independent copies. And only the objects in options
[] are properly linked via Option::next() and Option::prev(). You can iterate over buffer
[] to process all options in the order they appear in the argument vector, but if you want access to the other Options with the same Descriptor::index, then you must access the linked list via options
[]. You can get the linked list in options from a buffer object via something like options
[buffer[i].index()].