SPH
Public Member Functions | List of all members
ArgParser Class Reference

Provides functions for parsing command-line arguments. More...

#include <ArgsParser.h>

Public Member Functions

 ArgParser (ArrayView< const ArgDesc > args)
 Creates a parser, given a set of parameter descriptors. More...
 
void parse (const int argc, char **argv)
 Parses the input arguments and stored the parsed values. More...
 
void printHelp (ILogger &logger)
 Prints the help information into given logger. More...
 
template<typename TValue >
TValue getArg (const std::string &name) const
 Returns the value of an argument, given its short name (without the dash). More...
 
template<typename TValue >
Optional< TValue > tryGetArg (const std::string &name) const
 Returns the value of an argument or NOTHING if the argument was not parsed. More...
 
template<typename TEnum , typename TConvertor >
bool tryStore (Settings< TEnum > &settings, const std::string &name, const TEnum idx, TConvertor &&conv)
 Stores the value of given argument into an instance of Settings. More...
 
template<typename TEnum >
bool tryStore (Settings< TEnum > &settings, const std::string &name, const TEnum idx)
 Stores the value of given argument into an instance of Settings. More...
 
template<typename TFunctor >
void forEach (const TFunctor &functor)
 Enumerates all parsed arguments and executes a generic functor with parsed values. More...
 
Size size () const
 Returns the number of parsed arguments. More...
 
bool empty () const
 Returns true if no arguments have been parsed. More...
 

Detailed Description

Provides functions for parsing command-line arguments.

Definition at line 62 of file ArgsParser.h.

Constructor & Destructor Documentation

◆ ArgParser()

NAMESPACE_SPH_BEGIN ArgParser::ArgParser ( ArrayView< const ArgDesc args)
explicit

Creates a parser, given a set of parameter descriptors.

The "help" parameter (-h or –help) is automatically added into the set. It can be used to print the program help, using data provided in the descriptors.

Definition at line 6 of file ArgsParser.cpp.

Member Function Documentation

◆ empty()

bool ArgParser::empty ( ) const
inline

Returns true if no arguments have been parsed.

Definition at line 175 of file ArgsParser.h.

◆ forEach()

template<typename TFunctor >
void ArgParser::forEach ( const TFunctor &  functor)
inline

Enumerates all parsed arguments and executes a generic functor with parsed values.

Functor must be either generic lambda, or it must overload operator() for each parsed type, as listed in enum ArgEnum. The operator must have signature void(std::string, Type). The arguments are identified using its short name, without the dash.

Definition at line 163 of file ArgsParser.h.

◆ getArg()

template<typename TValue >
TValue ArgParser::getArg ( const std::string &  name) const
inline

Returns the value of an argument, given its short name (without the dash).

Exceptions
ArgErrorif the argument was not been parsed or it has different type.

Definition at line 95 of file ArgsParser.h.

◆ parse()

void ArgParser::parse ( const int  argc,
char **  argv 
)

Parses the input arguments and stored the parsed values.

Parsed values can be later queried using getArg or forEach functions. Previous values are removed.

Exceptions
ArgErrorif the input is not valid.

Definition at line 16 of file ArgsParser.cpp.

◆ printHelp()

void ArgParser::printHelp ( ILogger logger)

Prints the help information into given logger.

This is automatically called when parse is called with argument -h or –help.

Todo:
make padding adaptive

Definition at line 51 of file ArgsParser.cpp.

◆ size()

Size ArgParser::size ( ) const
inline

Returns the number of parsed arguments.

Definition at line 170 of file ArgsParser.h.

◆ tryGetArg()

template<typename TValue >
Optional<TValue> ArgParser::tryGetArg ( const std::string &  name) const
inline

Returns the value of an argument or NOTHING if the argument was not parsed.

Exceptions
ArgErrorif the name does not match any argument descriptor or it has different type.

Definition at line 110 of file ArgsParser.h.

◆ tryStore() [1/2]

template<typename TEnum >
bool ArgParser::tryStore ( Settings< TEnum > &  settings,
const std::string &  name,
const TEnum  idx 
)
inline

Stores the value of given argument into an instance of Settings.

Overload without value convertor.

Definition at line 153 of file ArgsParser.h.

◆ tryStore() [2/2]

template<typename TEnum , typename TConvertor >
bool ArgParser::tryStore ( Settings< TEnum > &  settings,
const std::string &  name,
const TEnum  idx,
TConvertor &&  conv 
)
inline

Stores the value of given argument into an instance of Settings.

If the value was not parsed, no value is stored and function returns false. Otherwise, true is returned.

Parameters
settingsSettings object where the parsed value is stored (if present).
nameShort name identifying the argument.
idxIndex of the target entry in Settings.
convGeneric value convertor, applied on value saved to Settings. Applied only on float arguments.
Exceptions
ArgErrorif the name does not match any argument descriptor.

Definition at line 134 of file ArgsParser.h.


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