FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
FEAT::ParameterBuilder< T_ > Class Template Reference

Builder-pattern for defining parameters. See methods for details. More...

#include <arg_parser.hpp>

Public Member Functions

 ParameterBuilder (std::shared_ptr< Intern::ParameterCore > core)
 
ParameterBuilderenv (String &&s)
 Set the environment variable for a parameter. More...
 
ParameterBuilderhelp_string (String &&s)
 Set help string for this parameter. More...
 
ParameterBuilderlong_flag (String &&s)
 Set the long flag for a parameter. More...
 
ParameterBuildername (String &&s)
 Set a name for this parameter. More...
 
template<typename U_ >
ParameterBuilderneeds (const Parameter< U_ > &parameter)
 Mark another parameter as needed by this one. More...
 
template<typename U_ >
ParameterBuilderneeds_if (const Parameter< U_ > &parameter, bool(*condition)(const T_ &))
 Conditionally mark another parameter as needed by this one. More...
 
 operator Parameter< T_ > ()
 Conversion operator. More...
 
ParameterBuilderparser (T_(*parser_fn)(const String &))
 Set a custom parser for this parameter. More...
 
ParameterBuilderplaceholder (String &&s)
 Set a placeholder text for the argument of this parameter. More...
 
ParameterBuilderproperty (String &&s)
 Set the property map path for a parameter. More...
 
ParameterBuilderrequired ()
 Mark this parameter as required. More...
 
ParameterBuildershort_flag (String &&s)
 Set the short flag for a parameter. More...
 
ParameterBuildervalidator (bool(*validator_fn)(const T_ &))
 Set a validator for this paramater. More...
 
ParameterBuildervalidator (void(*validator_fn)(const T_ &))
 Set a validator for this paramater. More...
 

Private Attributes

std::shared_ptr< Intern::ParameterCore_core
 

Detailed Description

template<typename T_>
class FEAT::ParameterBuilder< T_ >

Builder-pattern for defining parameters. See methods for details.

Definition at line 378 of file arg_parser.hpp.

Constructor & Destructor Documentation

◆ ParameterBuilder()

template<typename T_ >
FEAT::ParameterBuilder< T_ >::ParameterBuilder ( std::shared_ptr< Intern::ParameterCore core)
inlineexplicit

Definition at line 384 of file arg_parser.hpp.

Member Function Documentation

◆ env()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::env ( String &&  s)
inline

Set the environment variable for a parameter.

Parameters
[in]sEnvironment variable

The parameters value will be taken from this environment variable during parsing, if the environment variable is set.

Definition at line 423 of file arg_parser.hpp.

◆ help_string()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::help_string ( String &&  s)
inline

Set help string for this parameter.

Parameters
[in]sHelp string

Sets the description of this parameter for the help text shown by ArgParser::usage.

The parameter definition

parameter("mesh").long_flag("--mesh").placeholder("mesh").help_string("Mesh to operate on")

will result in the following snippet in the help text

* --mesh <mesh>
*    Mesh to operate on
* 

Definition at line 462 of file arg_parser.hpp.

References FEAT::ParameterBuilder< T_ >::help_string().

Referenced by FEAT::ParameterBuilder< T_ >::help_string().

◆ long_flag()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::long_flag ( String &&  s)
inline

Set the long flag for a parameter.

Parameters
[in]sLong flag, must be of the form '–foo'. Additional dashes within the flag are allowed, e.g. '–foo-bar'

Definition at line 409 of file arg_parser.hpp.

References FEAT::ParameterBuilder< T_ >::long_flag().

Referenced by FEAT::ParameterBuilder< T_ >::long_flag().

◆ name()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::name ( String &&  s)
inline

Set a name for this parameter.

Parameters
[in]sName for parameter

The name will be used in error messages and the ArgParser::display() method to identify the parameter. If no name is set, the name will be derived from either a flag, the property path, or the environment variable. If none of these are set the name is '<anonymous>'.

Definition at line 503 of file arg_parser.hpp.

◆ needs()

template<typename T_ >
template<typename U_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::needs ( const Parameter< U_ > &  parameter)
inline

Mark another parameter as needed by this one.

Parameters
[in]parameterThe parameter needed by this parameter

Indicates that if this parameter is set by the user, then the given parameter must be set by the user as well.

Use this option if one parameter is not meaningful without another parameter. A filename for example might be meaningless without setting an output directory.

Alternatively consider combining the data into a single type with a custom parser and combining the dependent parameters into a single parameter.

Warning
The parameter given to this function must already be fully defined. In other words, it must come before the current parameter in the parser struct.
See also
ParameterBuilder::needs_if

Definition at line 644 of file arg_parser.hpp.

References XASSERTM.

◆ needs_if()

template<typename T_ >
template<typename U_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::needs_if ( const Parameter< U_ > &  parameter,
bool(*)(const T_ &)  condition 
)
inline

Conditionally mark another parameter as needed by this one.

Parameters
[in]parameterThe parameter needed by this parameter
[in]conditionFunction pointer to predicate

Indicates that if this parameter is set by the user and has a certain value, as indicated by the condition function, then the given parameter must be set by the user as well.

Use this option if one parameter is sometimes not meaningful without another parameter. For example, if this parameter chooses a solver to use, then the different solver settings are only required if that solver is actually chosen.

parameter("Umfpack").long_flag("--solver").needs_if(max-iters, [](const auto& self) { self == "cg"; })
Warning
The parameter given to this function must already be fully defined. In other words, it must come before the current parameter in the parser struct.

Definition at line 675 of file arg_parser.hpp.

References FEAT::value, and XASSERTM.

◆ operator Parameter< T_ >()

template<typename T_ >
FEAT::ParameterBuilder< T_ >::operator Parameter< T_ > ( )
inline

Conversion operator.

Definition at line 690 of file arg_parser.hpp.

◆ parser()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::parser ( T_(*)(const String &)  parser_fn)
inline

Set a custom parser for this parameter.

Parameters
[in]parser_fnPointer to parser function

Allows setting a custom ad-hoc parser for this parameter, rather than relying on the usual String::parse method.

The parsing function is expected to throw a std::invalid_argument exception if any errors occur. The message given in the exception will be shown to the user.

Definition at line 521 of file arg_parser.hpp.

◆ placeholder()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::placeholder ( String &&  s)
inline

Set a placeholder text for the argument of this parameter.

Parameters
[in]sPlaceholder

The placeholder text will be shown in place of the parameters arguments in the help text generated by ArgParser::usage.

The parameter definition

parameter("mesh").long_flag("--mesh").placeholder("mesh").help_string("Mesh to operate on")

will result in the following snippet in the help text

* --mesh <mesh>
*    Mesh to operate on
* 

Definition at line 487 of file arg_parser.hpp.

References FEAT::ParameterBuilder< T_ >::placeholder().

Referenced by FEAT::ParameterBuilder< T_ >::placeholder().

◆ property()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::property ( String &&  s)
inline

Set the property map path for a parameter.

Parameters
[in]sPropertyMap path

The parameter will be filled with the (parsed) value refered to by this path. The path is always interpreted as relative to the root of the property map given to the ArgParser.

Definition at line 438 of file arg_parser.hpp.

◆ required()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::required ( )
inline

Mark this parameter as required.

A required parameter must be set by the user. It is not enough for it to be default initialized.

Definition at line 617 of file arg_parser.hpp.

References FEAT::ParameterBuilder< T_ >::required().

Referenced by FEAT::ParameterBuilder< T_ >::required().

◆ short_flag()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::short_flag ( String &&  s)
inline

Set the short flag for a parameter.

Parameters
[in]sShort flag, must be of the form '-x' where x is some letter

Definition at line 393 of file arg_parser.hpp.

References XASSERTM.

◆ validator() [1/2]

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::validator ( bool(*)(const T_ &)  validator_fn)
inline

Set a validator for this paramater.

Parameters
[in]validator_fnPointer to validator function

Validators are run after argument parsing to ensure that parameters fulfill additional properties beyond just their type. This allows you to ensure that, for example, a integer parameter is within a specific of number, or that a file at a given file path actually exists.

This specific overload of this method expects a validator function that returns a boolean indicating whether the given value passes validation. A failed validation produces an error with a generic "Failed boolean test" message. If you want to supply a more useful error message to your users, see the non-boolean overload of this function.

Definition at line 586 of file arg_parser.hpp.

References XASSERTM.

◆ validator() [2/2]

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::validator ( void(*)(const T_ &)  validator_fn)
inline

Set a validator for this paramater.

Parameters
[in]validator_fnPointer to validator function

Validators are run after argument parsing to ensure that parameters fulfill additional properties beyond just their type. This allows you to ensure that, for example, a integer parameter is within a specific of number, or that a file at a given file path actually exists.

This specific overload of this method expects a validator function that throws a std::invalid_argument exception if the given value does not pass validation. The message given to the std::invalid_argument exception is forwarded to the user.

Definition at line 549 of file arg_parser.hpp.

References XASSERTM.

Member Data Documentation

◆ _core

template<typename T_ >
std::shared_ptr<Intern::ParameterCore> FEAT::ParameterBuilder< T_ >::_core
private

Definition at line 381 of file arg_parser.hpp.


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