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

Simple argument class. More...

#include <Argument.hpp>

Inheritance diagram for TAP::Argument:
TAP::BaseArgument TAP::TypedArgument< T, multi > TAP::TypedArgument< bool, false > TAP::TypedArgument< T, false > TAP::VariableArgument< T, multi > TAP::SwitchArgument TAP::ConstArgument< T > TAP::ValueArgument< T, multi >

Public Member Functions

 Argument (std::string description)
 Create an argument with flag and/or name set by description. More...
 
 Argument (std::string description, char flag)
 Create an argument that is identified by a flag. More...
 
 Argument (std::string description, std::string name)
 Create an argument that is identified by a name. More...
 
 Argument (std::string description, char flag, std::string name)
 Create an argument that is identified by both a name and flag. More...
 
 Argument (const Argument &)=default
 Argument copy constructor.
 
 Argument (Argument &&)=default
 Argument move constructor.
 
virtual ~Argument ()
 Argument destructor.
 
Argumentoperator= (const Argument &other)=default
 Argument assignment operator.
 
Argumentoperator= (Argument &&other)=default
 Argument move assignment operator.
 
Argumentalias (char flag)
 An a flag alias for the argument. More...
 
Argumentalias (std::string name)
 An a name alias for the argument. More...
 
Argumentalias (char flag, std::string name)
 An both a flag and name alias for the argument. More...
 
const std::string & description () const
 Returns the description of this argument. More...
 
virtual Argumentcheck (ArgumentCheckFunc checkFunc)
 Set the check function to use. More...
 
bool matches () const
 Returns a pointer to this argument if it matches as a positional argument, otherwise a null-pointer. More...
 
bool matches (char flag) const
 Returns a pointer to this argument if it matches the given flag with any alias, otherwise a null-pointer. More...
 
bool matches (const std::string &name) const
 Returns a pointer to this argument if it matches the given name with any alias, otherwise a null-pointer. More...
 
void find_all_arguments (std::vector< const Argument * > &collector) const override
 See BaseArgument::find_all_arguments()
 
Argumentmany (bool many=true)
 Allow the argument to occur multiple times or not. More...
 
Argumentmin (unsigned int min)
 Set the minimum number of required occurrences if set. More...
 
unsigned int min () const
 Returns the minimum number of occurrences of this argument for it to be satisfied if set. More...
 
Argumentmax (unsigned int max)
 Set the maximum number of required occurrences. More...
 
unsigned int max () const
 Returns the maximum number of occurrences of this argument. More...
 
unsigned int count () const override
 See BaseArgument::count()
 
bool can_set () const
 Returns whether this argument is still allowed to occur on the command line. More...
 
virtual void set () const
 Set the argument (mark as occurred).
 
virtual bool takes_value () const
 Returns whether or not this argument requires a value when it occurs (see also set()). More...
 
void check_valid () const override
 See BaseArgument::check_valid()
 
std::string usage () const override
 See BaseArgument::usage()
 
virtual std::string ident () const
 Print a string representation of this argument to the given stream. More...
 
std::unique_ptr< BaseArgumentclone () const &override
 See BaseArgument::clone().
 
std::unique_ptr< BaseArgumentclone ()&&override
 See BaseArgument::clone().
 
- Public Member Functions inherited from TAP::BaseArgument
 BaseArgument ()
 Base constructor.
 
 BaseArgument (const BaseArgument &)=default
 BaseArgument copy constructor.
 
 BaseArgument (BaseArgument &&)=default
 BaseArgument move constructor.
 
virtual ~BaseArgument ()
 BaseArgument destructor.
 
BaseArgumentoperator= (const BaseArgument &)=default
 BaseArgument assignment operator.
 
BaseArgumentoperator= (BaseArgument &&)=default
 BaseArgument move assignment operator.
 
 operator bool () const
 Boolean conversion operator (so you can do things like if (arg) { ... More...
 
BaseArgumentset_required (bool required=true)
 Mark the argument as required or not. More...
 
bool required () const
 Returns whether the argument is required. More...
 

Protected Member Functions

virtual void check () const
 Executes the associated check function. More...
 

Protected Attributes

std::string m_flags
 Flags this arguments matches.
 
std::vector< std::string > m_names
 Names this argument matches.
 
bool m_isPositional
 True if used as positional argument.
 
std::string m_description
 Description or help text of argument.
 
unsigned int m_min = 1
 Minimum number of occurrences if argument is set.
 
unsigned int m_max = 1
 Maximum number of occurrences. More...
 
std::shared_ptr< unsigned int > m_count
 Actual counted number of occurrences.
 
ArgumentCheckFunc m_checkFunc = nullptr
 Callback for check.
 
- Protected Attributes inherited from TAP::BaseArgument
bool m_required = false
 True if the argument has to be set.
 

Private Member Functions

void parse_description ()
 When TAP_AUTOFLAG is defined, this function finds flag and/or name markers in the description of the argument, and uses these to set aliases. More...
 

Detailed Description

Simple argument class.

Arguments are identified by a flag ('-a') or name ('–alpha'), or can be positional (only if they accept a value, see ValuedArgument). An Argument by default is optional (as they normally should be for most applications, makes them easier to use), but has functions to mark it required. By default an argument is allowed to occur only once, but can be set to occur an arbitrary amount of times.

Constructor & Destructor Documentation

TAP::Argument::Argument ( std::string  description)
inline

Create an argument with flag and/or name set by description.

Using this constructing with a description without a flag or name is invalid.

Parameters
descriptionDescription of the argument, requires a flag or name to be defined
TAP::Argument::Argument ( std::string  description,
char  flag 
)
inline

Create an argument that is identified by a flag.

Parameters
descriptionDescription of the argument (used in help text)
flagFlag identifier of this argument
TAP::Argument::Argument ( std::string  description,
std::string  name 
)
inline

Create an argument that is identified by a name.

Parameters
descriptionDescription of the argument (used in help text)
nameName identifier of this argument
TAP::Argument::Argument ( std::string  description,
char  flag,
std::string  name 
)
inline

Create an argument that is identified by both a name and flag.

Parameters
descriptionDescription of the argument (used in help text)
flagFlag identifier of this argument
nameName identifier of this argument

Member Function Documentation

Argument& TAP::Argument::alias ( char  flag)
inline

An a flag alias for the argument.

Parameters
flagFlag alias of Argument
Returns
Reference to this argument
Argument& TAP::Argument::alias ( std::string  name)
inline

An a name alias for the argument.

Parameters
nameName alias of Argument
Returns
Reference to this argument
Argument& TAP::Argument::alias ( char  flag,
std::string  name 
)
inline

An both a flag and name alias for the argument.

Parameters
flagFlag alias of Argument
nameName alias of Argument
Returns
Reference to this argument
bool TAP::Argument::can_set ( ) const
inline

Returns whether this argument is still allowed to occur on the command line.

Returns
True iff the argument is still allowed to occur
virtual Argument& TAP::Argument::check ( ArgumentCheckFunc  checkFunc)
inlinevirtual

Set the check function to use.

Parameters
checkFuncCheck function to use (see ArgumentCheckFunc)
Returns
Reference to this argument
virtual void TAP::Argument::check ( ) const
inlineprotectedvirtual

Executes the associated check function.

Returns
Result of the check function, or true if not set

Reimplemented in TAP::TypedArgument< T, multi >, TAP::TypedArgument< bool, false >, TAP::TypedArgument< T, false >, TAP::TypedArgument< T, multi >, TAP::TypedArgument< bool, false >, and TAP::TypedArgument< T, false >.

const std::string& TAP::Argument::description ( ) const
inline

Returns the description of this argument.

See also description(string).

Returns
Argument description
virtual std::string TAP::Argument::ident ( ) const
virtual

Print a string representation of this argument to the given stream.

This is usually represented in the first column of help text.

Returns
String representation.

Reimplemented in TAP::VariableArgument< T, multi >.

Argument& TAP::Argument::many ( bool  many = true)
inline

Allow the argument to occur multiple times or not.

See also max()

Parameters
manyIf true, set the maximum number of occurrences to infinite, otherwise the current limit or 1.
Returns
Reference to this argument
bool TAP::Argument::matches ( ) const
inline

Returns a pointer to this argument if it matches as a positional argument, otherwise a null-pointer.

Returns
Either a pointer to this argument if positional, otherwise nullptr.
bool TAP::Argument::matches ( char  flag) const
inline

Returns a pointer to this argument if it matches the given flag with any alias, otherwise a null-pointer.

Parameters
flagFlag to check
Returns
Either a pointer to this argument if positional, otherwise nullptr.
bool TAP::Argument::matches ( const std::string &  name) const
inline

Returns a pointer to this argument if it matches the given name with any alias, otherwise a null-pointer.

Parameters
nameName to check
Returns
Either a pointer to this argument if positional, otherwise nullptr.
Argument& TAP::Argument::max ( unsigned int  max)
inline

Set the maximum number of required occurrences.

This value may be 0 to indicate unbounded.

Parameters
maxMaximum number of occurrences
Returns
Reference to this argument
unsigned int TAP::Argument::max ( ) const
inline

Returns the maximum number of occurrences of this argument.

Returns
Maximum number of occurrences
Argument& TAP::Argument::min ( unsigned int  min)
inline

Set the minimum number of required occurrences if set.

If this value is higher than the current maximum, the maximum is set to the same value.

Parameters
minMinimum number of occurrences, must be >0
Returns
Reference to this argument
unsigned int TAP::Argument::min ( ) const
inline

Returns the minimum number of occurrences of this argument for it to be satisfied if set.

Returns
Minimum number of occurrences
void TAP::Argument::parse_description ( )
inlineprivate

When TAP_AUTOFLAG is defined, this function finds flag and/or name markers in the description of the argument, and uses these to set aliases.

Names are delimited by word boundaries, which is any non-alphanumeric character. Empty names are ignored. Markers:

  • % : Next character is a flag
  • $ : Next word is a name
  • & : Next character is a flag, and next word is a name

For example, the description "Show this &help text" will set a flag 'h' and a name "help".

virtual bool TAP::Argument::takes_value ( ) const
inlinevirtual

Returns whether or not this argument requires a value when it occurs (see also set()).

Returns
True iff the argument requires a value when set

Reimplemented in TAP::SwitchArgument, TAP::ConstArgument< T >, TAP::VariableArgument< T, multi >, TAP::TypedArgument< T, multi >, TAP::TypedArgument< bool, false >, and TAP::TypedArgument< T, false >.

Member Data Documentation

unsigned int TAP::Argument::m_max = 1
protected

Maximum number of occurrences.

0 means no limit


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