TAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
TAP::ArgumentParser Class Reference

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.
 
ArgumentParserprogram_name (const std::string &programName)
 Set the program name. More...
 
const std::string & program_name () const
 Returns the program name. More...
 
template<typename... Args>
ArgumentParseraddAll (Args &&...args)
 Add all given arguments, or constraints, to the parser. More...
 
template<typename Arg >
ArgumentParseradd (Arg &&arg)
 Add the given argument, or constraint, to the parser. More...
 
ArgumentParseradd (ArgumentSet argSet)
 Add the given ArgumentSet to the parser. More...
 
template<typename Arg >
ArgumentParseraddConstraint (Arg &&constr)
 Add the given constraint, to the parser. More...
 
const Argumentoperator[] (char flag) const
 Get the Argument given the flag. More...
 
const Argumentoperator[] (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< ArgumentSetm_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 ArgumentfindArg () 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 ArgumentfindArg (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...
 

Detailed Description

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.

Constructor & Destructor Documentation

template<typename... Args>
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.

Parameters
argsArguments (or constraints) to add

Member Function Documentation

template<typename Arg >
ArgumentParser& TAP::ArgumentParser::add ( Arg &&  arg)

Add the given argument, or constraint, to the parser.

Parameters
argArgument to add
Returns
Reference to this ArgumentParser
ArgumentParser& TAP::ArgumentParser::add ( ArgumentSet  argSet)

Add the given ArgumentSet to the parser.

Parameters
argSetArgumentSet to add
Returns
Reference to this ArgumentParser
template<typename... Args>
ArgumentParser& TAP::ArgumentParser::addAll ( Args &&...  args)

Add all given arguments, or constraints, to the parser.

Parameters
argsArguments to add
Returns
Reference to this ArgumentParser
template<typename Arg >
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()).

Parameters
constrArgument constraint to add
Returns
Reference to this ArgumentParser
const Argument* TAP::ArgumentParser::findArg ( ) const
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.

Returns
First matching argument that can be set, last matching argument if none can be set, or nullptr if no matching arguments
template<typename Ident >
const Argument* TAP::ArgumentParser::findArg ( Ident  ident) const
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.

Parameters
identvoid, char or std::string to identify the argument to find
Returns
First matching argument that can be set, last matching argument if none can be set, or nullptr if no matching arguments
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.

Returns
A string with help text.
const Argument& TAP::ArgumentParser::operator[] ( char  flag) const
inline

Get the Argument given the flag.

If the Argument is not contained exactly once, behavior is undefined.

Parameters
flagFlag to search Argument by
Returns
The Argument that matches the flag
const Argument& TAP::ArgumentParser::operator[] ( const std::string &  name) const
inline

Get the Argument given the flag.

If the Argument is not contained exactly once, behavior is undefined.

Parameters
nameName to search Argument by
Returns
The Argument that matches the name
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).

Parameters
argcNumber of items in the argv array
argvProgram arguments. The first item is expected to be the program invocation name
void TAP::ArgumentParser::parse ( std::vector< std::string > &  argv) const
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).

Parameters
argvProgram arguments. This vector may be modified by this function
ArgumentParser& TAP::ArgumentParser::program_name ( const std::string &  programName)
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.

Returns
Reference to this ArgumentParser
const std::string& TAP::ArgumentParser::program_name ( ) const
inline

Returns the program name.

Returns
Program name
void TAP::ArgumentParser::set_arg_value ( const Argument arg,
const std::string &  value 
) const
private

Helper function to set the value of an Argument of which takes_value() returns true.

Parameters
argArgument to set value to
valueValue to set

Member Data Documentation

std::string TAP::ArgumentParser::m_programName
protected

Program name as displayed in help text.

If not set explicitly, will use the first argument of the parse function


The documentation for this class was generated from the following file: