C++ Utilities 5.26.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
|
The ArgumentParser class provides a means for handling command line arguments. More...
#include <argumentparser.h>
Public Member Functions | |
ArgumentParser () | |
Constructs a new ArgumentParser. | |
const ArgumentVector & | mainArguments () const |
Returns the main arguments. | |
void | setMainArguments (const ArgumentInitializerList &mainArguments) |
Sets the main arguments for the parser. | |
void | addMainArgument (Argument *argument) |
Adds the specified argument to the main argument. | |
void | printHelp (std::ostream &os) const |
Prints help text for all assigned arguments. | |
void | parseArgs (int argc, const char *const *argv, ParseArgumentBehavior behavior=ParseArgumentBehavior::CheckConstraints|ParseArgumentBehavior::InvokeCallbacks|ParseArgumentBehavior::ExitOnFailure) |
Parses the specified command line arguments. | |
void | readArgs (int argc, const char *const *argv) |
Parses the specified command line arguments. | |
void | resetArgs () |
Resets all Argument instances assigned as mainArguments() and sub arguments. | |
void | checkConstraints () |
Checks whether constraints are violated. | |
void | invokeCallbacks () |
Invokes all assigned callbacks. | |
unsigned int | actualArgumentCount () const |
Returns the actual number of arguments that could be found when parsing. | |
const char * | executable () const |
Returns the name of the current executable. | |
UnknownArgumentBehavior | unknownArgumentBehavior () const |
Returns how unknown arguments are treated. | |
void | setUnknownArgumentBehavior (UnknownArgumentBehavior behavior) |
Sets how unknown arguments are treated. | |
Argument * | defaultArgument () const |
Returns the default argument. | |
void | setDefaultArgument (Argument *argument) |
Sets the default argument. | |
Argument * | specifiedOperation () const |
Returns the first operation argument specified by the user or nullptr if no operation has been specified. | |
bool | isUncombinableMainArgPresent () const |
Checks whether at least one uncombinable main argument is present. | |
void | setExitFunction (std::function< void(int)> exitFunction) |
Specifies a function quit the application. | |
const HelpArgument & | helpArg () const |
Returns the --help argument. | |
HelpArgument & | helpArg () |
Returns the --help argument. | |
const NoColorArgument & | noColorArg () const |
Returns the --no-color argument. | |
NoColorArgument & | noColorArg () |
Returns the --no-color argument. | |
The ArgumentParser class provides a means for handling command line arguments.
To setup the parser create instances of ApplicationUtilities::Argument to define a set of known arguments and assign these to the parser using setMainArguments().
To invoke parsing call parseArgs(). The parser will verify the previously assigned definitions (and might throw std::invalid_argument) and then parse the given command line arguments according the definitions (and might throw CppUtilities::Failure).
Definition at line 457 of file argumentparser.h.
CppUtilities::ArgumentParser::ArgumentParser | ( | ) |
Constructs a new ArgumentParser.
Definition at line 772 of file argumentparser.cpp.
|
inline |
Returns the actual number of arguments that could be found when parsing.
Definition at line 1103 of file argumentparser.h.
void CppUtilities::ArgumentParser::addMainArgument | ( | Argument * | argument | ) |
Adds the specified argument to the main argument.
Definition at line 821 of file argumentparser.cpp.
|
inline |
Checks whether constraints are violated.
Throws | Failure if constraints are violated. |
Definition at line 1159 of file argumentparser.h.
|
inline |
Returns the default argument.
Definition at line 1140 of file argumentparser.h.
|
inline |
Returns the name of the current executable.
Definition at line 1111 of file argumentparser.h.
|
inline |
Returns the --help
argument.
Definition at line 1193 of file argumentparser.h.
|
inline |
Returns the --help
argument.
Definition at line 1185 of file argumentparser.h.
|
inline |
Invokes all assigned callbacks.
Definition at line 1168 of file argumentparser.h.
bool CppUtilities::ArgumentParser::isUncombinableMainArgPresent | ( | ) | const |
Checks whether at least one uncombinable main argument is present.
Definition at line 1062 of file argumentparser.cpp.
|
inline |
Returns the main arguments.
Definition at line 1095 of file argumentparser.h.
|
inline |
Returns the --no-color
argument.
Definition at line 1209 of file argumentparser.h.
|
inline |
Returns the --no-color
argument.
Definition at line 1201 of file argumentparser.h.
void CppUtilities::ArgumentParser::parseArgs | ( | int | argc, |
const char *const * | argv, | ||
ParseArgumentBehavior | behavior = ParseArgumentBehavior::CheckConstraints | ParseArgumentBehavior::InvokeCallbacks | ParseArgumentBehavior::ExitOnFailure ) |
Parses the specified command line arguments.
The behavior is configurable by specifying the behavior argument. See ParseArgumentBehavior for the options. By default, all options are present.
Throws | Failure if the specified arguments are invalid and the ParseArgumentBehavior::ExitOnFailure flag is not present. |
Definition at line 936 of file argumentparser.cpp.
void CppUtilities::ArgumentParser::printHelp | ( | std::ostream & | os | ) | const |
Prints help text for all assigned arguments.
Definition at line 830 of file argumentparser.cpp.
void CppUtilities::ArgumentParser::readArgs | ( | int | argc, |
const char *const * | argv ) |
Parses the specified command line arguments.
Throws | Failure if the specified arguments are invalid. |
Definition at line 972 of file argumentparser.cpp.
void CppUtilities::ArgumentParser::resetArgs | ( | ) |
Resets all Argument instances assigned as mainArguments() and sub arguments.
Definition at line 1036 of file argumentparser.cpp.
|
inline |
Sets the default argument.
Definition at line 1149 of file argumentparser.h.
|
inline |
Specifies a function quit the application.
Definition at line 1177 of file argumentparser.h.
void CppUtilities::ArgumentParser::setMainArguments | ( | const ArgumentInitializerList & | mainArguments | ) |
Sets the main arguments for the parser.
The parser will use these argument definitions to when parsing the command line arguments and when printing help information.
Definition at line 790 of file argumentparser.cpp.
|
inline |
Sets how unknown arguments are treated.
The default value is UnknownArgumentBehavior::Fail.
Definition at line 1131 of file argumentparser.h.
Argument * CppUtilities::ArgumentParser::specifiedOperation | ( | ) | const |
Returns the first operation argument specified by the user or nullptr if no operation has been specified.
Definition at line 1049 of file argumentparser.cpp.
|
inline |
Returns how unknown arguments are treated.
The default value is UnknownArgumentBehavior::Fail.
Definition at line 1121 of file argumentparser.h.