Do command line parsing. More...
#include <CmdLineParser.hpp>
Classes | |
struct | Option |
Public Types | |
enum | EArgumentTypeFlag { E_NO_ARG, E_REQUIRED_ARG, E_OPTIONAL_ARG } |
enum | EErrorCodes { E_INVALID_OPTION, E_MISSING_ARGUMENT, E_INVALID_NON_OPTION_ARG, E_MISSING_OPTION } |
enum | EAllowNonOptionArgsFlag { E_NON_OPTION_ARGS_ALLOWED, E_NON_OPTION_ARGS_INVALID } |
Public Member Functions | |
CmdLineParser (int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs) | |
String | getOptionValue (int id, const char *defaultValue="") const |
Read out a string option. | |
String | mustGetOptionValue (int id, const char *exceptionMessage="") const |
Read out a string option. | |
StringArray | getOptionValueList (int id) const |
Read out all occurences of a string option. | |
StringArray | mustGetOptionValueList (int id, const char *exceptionMessage="") const |
Read out all occurences of a string option. | |
bool | isSet (int id) const |
Read out a boolean option or check for the presence of string option. | |
size_t | getNonOptionCount () const |
Read the number of arguments that aren't options (but, for example, filenames). | |
String | getNonOptionArg (size_t n) const |
Read out an non-option argument. | |
StringArray | getNonOptionArgs () const |
Read out the non-option args. | |
Static Public Member Functions | |
static String | getUsage (const Option *options, unsigned int maxColumns=80) |
Generate a usage string for the options, for example: | |
Private Types | |
typedef SortedVectorMap< int, StringArray > | optionsMap_t |
Private Attributes | |
optionsMap_t | m_parsedOptions |
StringArray | m_nonOptionArgs |
Do command line parsing.
Thread safety: read/write Copy semantics: Value Exception safety: Strong
Definition at line 58 of file CmdLineParser.hpp.
typedef SortedVectorMap<int, StringArray> BLOCXX_NAMESPACE::CmdLineParser::optionsMap_t [private] |
Definition at line 196 of file CmdLineParser.hpp.
E_NON_OPTION_ARGS_ALLOWED |
Non-option arguments are allowed. |
E_NON_OPTION_ARGS_INVALID |
Non-option arguments are invalid. |
Definition at line 87 of file CmdLineParser.hpp.
E_NO_ARG |
the option does not take an argument |
E_REQUIRED_ARG |
the option requires an argument |
E_OPTIONAL_ARG |
the option might have an argument |
Definition at line 61 of file CmdLineParser.hpp.
Definition at line 69 of file CmdLineParser.hpp.
BLOCXX_NAMESPACE::CmdLineParser::CmdLineParser | ( | int | argc, | |
char const *const *const | argv, | |||
const Option * | options, | |||
EAllowNonOptionArgsFlag | allowNonOptionArgs | |||
) |
argc | Count of pointers in argv. Pass value from main(). | |
argv | Arguments. Pass value from main(). Value is not saved. | |
options | An array of Option terminated by a final entry that has a '\0' shortopt && 0 longopt. Value is not saved. | |
allowNonOptionArgs | Indicate whether the presense of non-option arguments is an error. |
CmdLineParserException | if the given command line is invalid. |
Definition at line 79 of file CmdLineParser.cpp.
String BLOCXX_NAMESPACE::CmdLineParser::getNonOptionArg | ( | size_t | n | ) | const |
Read out an non-option argument.
n | The 0-based index of the argument. Valid values are 0 to count()-1. |
Definition at line 373 of file CmdLineParser.cpp.
StringArray BLOCXX_NAMESPACE::CmdLineParser::getNonOptionArgs | ( | ) | const |
Read out the non-option args.
Definition at line 380 of file CmdLineParser.cpp.
size_t BLOCXX_NAMESPACE::CmdLineParser::getNonOptionCount | ( | ) | const |
Read the number of arguments that aren't options (but, for example, filenames).
Definition at line 366 of file CmdLineParser.cpp.
String BLOCXX_NAMESPACE::CmdLineParser::getOptionValue | ( | int | id, | |
const char * | defaultValue = "" | |||
) | const |
Read out a string option.
id | The id of the option. | |
defaultValue | The return value if the option wasn't set. |
Definition at line 308 of file CmdLineParser.cpp.
StringArray BLOCXX_NAMESPACE::CmdLineParser::getOptionValueList | ( | int | id | ) | const |
Read out all occurences of a string option.
id | The id of the option. |
Definition at line 334 of file CmdLineParser.cpp.
String BLOCXX_NAMESPACE::CmdLineParser::getUsage | ( | const Option * | options, | |
unsigned int | maxColumns = 80 | |||
) | [static] |
Generate a usage string for the options, for example:
"Options:\n" " -1, --one first description\n" " -2, --two [arg] second description (default is optional)\n" " -3, --three <arg> third description\n"
The E_OPTIONAL_ARG option arguments are indicated by squared brackets
"[arg]"
and E_REQUIRED_ARG option arguments by angle brackets
"<arg>"
.
options | An array of Option terminated by a final entry that has a '\0' shortopt && 0 longopt. | |
maxColumns | Wrap the descriptions so no line of the usage string exceeds the specified number of columns. |
Definition at line 200 of file CmdLineParser.cpp.
bool BLOCXX_NAMESPACE::CmdLineParser::isSet | ( | int | id | ) | const |
Read out a boolean option or check for the presence of string option.
Definition at line 359 of file CmdLineParser.cpp.
String BLOCXX_NAMESPACE::CmdLineParser::mustGetOptionValue | ( | int | id, | |
const char * | exceptionMessage = "" | |||
) | const |
Read out a string option.
id | The id of the option. |
exceptionMessage | If an exception is thrown this string will be used as the exception message. |
CmdLineParserException | with code E_MISSING_OPTION if the option wasn't specified. |
Definition at line 321 of file CmdLineParser.cpp.
StringArray BLOCXX_NAMESPACE::CmdLineParser::mustGetOptionValueList | ( | int | id, | |
const char * | exceptionMessage = "" | |||
) | const |
Read out all occurences of a string option.
id | The id of the option. |
exceptionMessage | If an exception is thrown this string will be used as the exception message. |
CmdLineParserException | with code E_MISSING_OPTION if the option wasn't specified. |
Definition at line 347 of file CmdLineParser.cpp.
Definition at line 198 of file CmdLineParser.hpp.
Definition at line 197 of file CmdLineParser.hpp.