Argument parser class. More...
#include <Parser.hpp>
Public Member Functions | |
template<typename... Args> | |
ArgumentParser (Args &&...args) | |
Construct a new ArgumentParser. More... | |
virtual | ~ArgumentParser () |
ArgumentParser destructor. | |
ArgumentParser & | program_name (const std::string &programName) |
Set the program name. More... | |
const std::string & | program_name () const |
Returns the program name. More... | |
template<typename... Args> | |
ArgumentParser & | addAll (Args &&...args) |
Add all given arguments, or constraints, to the parser. More... | |
template<typename Arg > | |
ArgumentParser & | add (Arg &&arg) |
Add the given argument, or constraint, to the parser. More... | |
ArgumentParser & | add (ArgumentSet argSet) |
Add the given ArgumentSet to the parser. More... | |
template<typename Arg > | |
ArgumentParser & | addConstraint (Arg &&constr) |
Add the given constraint, to the parser. More... | |
const Argument & | operator[] (char flag) const |
Get the Argument given the flag. More... | |
const Argument & | operator[] (const std::string &name) const |
Get the Argument given the flag. More... | |
std::string | help () const |
Generate a help string for the user to see. More... | |
void | parse (int argc, const char *const argv[]) |
Parses the given arguments as they are presented on main() (see the parsing rules in the description of the ArgumentParser class). More... | |
Protected Attributes | |
std::vector< ArgumentSet > | m_argSets |
Collection ArgumentSets (groups) | |
ArgumentSet | m_constraints |
Stored argument constraints. | |
std::string | m_programName |
Program name as displayed in help text. More... | |
Private Member Functions | |
const Argument * | findArg () const |
Find positional argument (see Argument::matches()), either the first one that can be set, or the last one that matches. More... | |
template<typename Ident > | |
const Argument * | findArg (Ident ident) const |
Find the argument matching the given parameter (see Argument::matches()), either the first one that can be set, or the last one that matches. More... | |
void | parse (std::vector< std::string > &argv) const |
Parses the given argument vector (see the parsing rules in the description of the ArgumentParser class). More... | |
void | set_arg_value (const Argument *arg, const std::string &value) const |
Helper function to set the value of an Argument of which takes_value() returns true. More... | |
Argument parser class.
Main job is to parse a given set of command line options and feed them into a set of Argument instances, checking the constraints afterwards. Arguments are identified either as a flag argument (see TAP::flagStart), a named argument (see TAP::nameStart) or a positional argument. If the skip marker has been defined (see TAP::skip), any argument presented after this marker is treated as a positional argument. Flag arguments are allowed to be joined together (e.g. '-abc' instead of '-a -b -c'). The final flag is allowed to have its value attached (e.g '-abvalue' is treated as '-a -b value' if '-b' accepts a value). Name arguments can optionally have their value joined with a delimiter to the argument itself (e.g. '–alpha=value', see TAP::nameDelim). Effectively, parsing is similar to that of GNU get_opt_long(), except Perl like arguments are not supported.
TAP::ArgumentParser::ArgumentParser | ( | Args &&... | args | ) |
Construct a new ArgumentParser.
The given list of Arguments is added to the parser immediately. Does not generate help text.
args | Arguments (or constraints) to add |
ArgumentParser& TAP::ArgumentParser::add | ( | Arg && | arg | ) |
Add the given argument, or constraint, to the parser.
arg | Argument to add |
ArgumentParser& TAP::ArgumentParser::add | ( | ArgumentSet | argSet | ) |
Add the given ArgumentSet to the parser.
argSet | ArgumentSet to add |
ArgumentParser& TAP::ArgumentParser::addAll | ( | Args &&... | args | ) |
Add all given arguments, or constraints, to the parser.
args | Arguments to add |
ArgumentParser& TAP::ArgumentParser::addConstraint | ( | Arg && | constr | ) |
Add the given constraint, to the parser.
Constraints are not shown in the help, but are checked. Argument instances in the constraint must have been added before as a regular argument for the parser to recognize them (see add()).
constr | Argument constraint to add |
|
private |
Find positional argument (see Argument::matches()), either the first one that can be set, or the last one that matches.
Returns a null-pointer if not found.
|
private |
Find the argument matching the given parameter (see Argument::matches()), either the first one that can be set, or the last one that matches.
Returns a null-pointer if not found.
ident | void, char or std::string to identify the argument to find |
std::string TAP::ArgumentParser::help | ( | ) | const |
Generate a help string for the user to see.
Contains a short usage line, and a list of accepted arguments with their descriptions.
|
inline |
|
inline |
void TAP::ArgumentParser::parse | ( | int | argc, |
const char *const | argv[] | ||
) |
Parses the given arguments as they are presented on main() (see the parsing rules in the description of the ArgumentParser class).
Throws an exception if parsing fails, otherwise returns a boolean indicating if the program should continue or stop (e.g. after displaying help).
argc | Number of items in the argv array |
argv | Program arguments. The first item is expected to be the program invocation name |
|
private |
Parses the given argument vector (see the parsing rules in the description of the ArgumentParser class).
Throws an exception if parsing fails, otherwise returns a Boolean indicating if the program should continue or stop (e.g. after displaying help).
argv | Program arguments. This vector may be modified by this function |
|
inline |
Set the program name.
This name is used in the usage string when displaying help. If not set, the first argument from calling parse() is used instead.
|
inline |
Returns the program name.
|
private |
|
protected |
Program name as displayed in help text.
If not set explicitly, will use the first argument of the parse function