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, String env_prefix, String property_path_prefix, String flag_prefix)
 
template<typename V_ = T_, typename = std::enable_if_t<std::is_same_v<std::deque<String>, V_>>>
ParameterBuilder & bootstrap ()
 Mark this parameter as a bootstrap parameter. More...
 
template<typename U_ , typename Fn_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param, Fn_ &&fn)
 ArgParser::derive_from wrapper for casting lambdas to function pointers. More...
 
template<typename U_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param, T_(*fn)(const U_ &))
 Derive this parameter from another parameter if it has not been set by the user. More...
 
template<typename U_ , typename V_ , typename W_ , typename Fn_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param_one, const Parameter< V_ > &param_two, const Parameter< W_ > &param_three, Fn_ &&fn)
 ArgParser::derive_from wrapper for casting lambdas to function pointers. More...
 
template<typename U_ , typename V_ , typename W_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param_one, const Parameter< V_ > &param_two, const Parameter< W_ > &param_three, T_(*fn)(const U_ &, const V_ &, const W_ &))
 Derive this parameter from three other parameters if it has not been set by the user. More...
 
template<typename U_ , typename V_ , typename Fn_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param_one, const Parameter< V_ > &param_two, Fn_ &&fn)
 ArgParser::derive_from wrapper for casting lambdas to function pointers. More...
 
template<typename U_ , typename V_ >
ParameterBuilder & derives_from (const Parameter< U_ > &param_one, const Parameter< V_ > &param_two, T_(*fn)(const U_ &, const V_ &))
 Derive this parameter from two other parameters if it has not been set by the user. More...
 
ParameterBuilder & env (const String &s)
 Set the environment variable for a parameter. More...
 
ParameterBuilder & help_string (String &&s)
 Set help string for this parameter. More...
 
template<typename U_ >
ParameterBuilder & hide_if (const Parameter< U_ > &parameter, bool(*condition)(const T_ &))
 Conditionally mark another parameter as hidden. More...
 
ParameterBuilder & long_flag (String s)
 Set the long flag for a parameter. More...
 
ParameterBuilder & name (String &&s)
 Set a name for this parameter. More...
 
template<typename U_ >
ParameterBuilder & needs (const Parameter< U_ > &parameter)
 Mark another parameter as needed by this one. More...
 
template<typename U_ >
ParameterBuilder & needs_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...
 
ParameterBuilder & parser (T_(*parser_fn)(const String &))
 Set a custom parser for this parameter. More...
 
ParameterBuilder & placeholder (String &&s)
 Set a placeholder text for the argument of this parameter. More...
 
ParameterBuilder & property (String &&s)
 Set the property map path for a parameter. More...
 
ParameterBuilder & required ()
 Mark this parameter as required. More...
 
ParameterBuilder & short_flag (String &&s)
 Set the short flag for a parameter. More...
 
ParameterBuilder & validator (bool(*validator_fn)(const T_ &))
 Set a validator for this paramater. More...
 
ParameterBuilder & validator (void(*validator_fn)(const T_ &))
 Set a validator for this paramater. More...
 

Private Attributes

std::shared_ptr< Intern::ParameterCore > _core
 
String _env_prefix
 
String _flag_prefix
 
String _property_path_prefix
 

Detailed Description

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

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

Definition at line 485 of file arg_parser.hpp.

Constructor & Destructor Documentation

◆ ParameterBuilder()

template<typename T_ >
FEAT::ParameterBuilder< T_ >::ParameterBuilder ( std::shared_ptr< Intern::ParameterCore >  core,
String  env_prefix,
String  property_path_prefix,
String  flag_prefix 
)
inlineexplicit

Definition at line 495 of file arg_parser.hpp.

Member Function Documentation

◆ bootstrap()

template<typename T_ >
template<typename V_ = T_, typename = std::enable_if_t<std::is_same_v<std::deque<String>, V_>>>
ParameterBuilder & FEAT::ParameterBuilder< T_ >::bootstrap ( )
inline

Mark this parameter as a bootstrap parameter.

A common FEAT idiom is parsing a path (or paths) to a property map from the command line, loading that property map, and then parsing remaining options from that map. Naively, this would required defining at least two parsers, where the first parser just determines the paths of the property maps and the second parser does the full parsing run. To avoid this parameters can be marked as bootstrap parameters. The ArgParser::bootstrap method will parse just those parameters, with no further validation, and assemble a merged property map from the given paths. With that merged property map the full parsing can then be run.

Warning
Bootstrap parameters must be of type std::deque<String>.

Definition at line 888 of file arg_parser.hpp.

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

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

◆ derives_from() [1/6]

template<typename T_ >
template<typename U_ , typename Fn_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param,
Fn_ &&  fn 
)
inline

ArgParser::derive_from wrapper for casting lambdas to function pointers.

Definition at line 934 of file arg_parser.hpp.

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

◆ derives_from() [2/6]

template<typename T_ >
template<typename U_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param,
T_(*)(const U_ &)  fn 
)
inline

Derive this parameter from another parameter if it has not been set by the user.

Parameters
[in]paramParameter this parameter derives from
[in]fnFunction which calculates value from other parameter.

Indicates that this parameter should have its value set based on the value of one other parameter, if this parameter has not been set by the user. If this parameter has been set by the user, the value set by the user will be kept. A parameter that has no flags, properties, or environment variables associated with it will always be derived.

Definition at line 908 of file arg_parser.hpp.

References XASSERT, and XASSERTM.

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

◆ derives_from() [3/6]

template<typename T_ >
template<typename U_ , typename V_ , typename W_ , typename Fn_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param_one,
const Parameter< V_ > &  param_two,
const Parameter< W_ > &  param_three,
Fn_ &&  fn 
)
inline

ArgParser::derive_from wrapper for casting lambdas to function pointers.

Definition at line 1019 of file arg_parser.hpp.

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

◆ derives_from() [4/6]

template<typename T_ >
template<typename U_ , typename V_ , typename W_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param_one,
const Parameter< V_ > &  param_two,
const Parameter< W_ > &  param_three,
T_(*)(const U_ &, const V_ &, const W_ &)  fn 
)
inline

Derive this parameter from three other parameters if it has not been set by the user.

Parameters
[in]param_oneFirst parameter this parameter derives from
[in]param_twoSecond parameter this parameter derives from
[in]param_threeThird parameter this parameter derives from
[in]fnFunction which calculates value from other parameter.

Indicates that this parameter should have its value set based on the value of three other parameters, if this parameter has not been set by the user. If this parameter has been set by the user, the value set by the user will be kept. A parameter that has no flags, properties, or environment variables associated with it will always be derived.

Definition at line 992 of file arg_parser.hpp.

References XASSERT, and XASSERTM.

◆ derives_from() [5/6]

template<typename T_ >
template<typename U_ , typename V_ , typename Fn_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param_one,
const Parameter< V_ > &  param_two,
Fn_ &&  fn 
)
inline

ArgParser::derive_from wrapper for casting lambdas to function pointers.

Definition at line 972 of file arg_parser.hpp.

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

◆ derives_from() [6/6]

template<typename T_ >
template<typename U_ , typename V_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::derives_from ( const Parameter< U_ > &  param_one,
const Parameter< V_ > &  param_two,
T_(*)(const U_ &, const V_ &)  fn 
)
inline

Derive this parameter from two other parameters if it has not been set by the user.

Parameters
[in]param_oneFirst parameter this parameter derives from
[in]param_twoSecond parameter this parameter derives from
[in]fnFunction which calculates value from other parameter.

Indicates that this parameter should have its value set based on the value of two other parameters, if this parameter has not been set by the user. If this parameter has been set by the user, the value set by the user will be kept. A parameter that has no flags, properties, or environment variables associated with it will always be derived.

Definition at line 953 of file arg_parser.hpp.

References XASSERT, and XASSERTM.

◆ env()

template<typename T_ >
ParameterBuilder & FEAT::ParameterBuilder< T_ >::env ( const 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 562 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 617 of file arg_parser.hpp.

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

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

◆ hide_if()

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

Conditionally mark another parameter as hidden.

Parameters
[in]parameterThe parameter to be conditionally hidden
[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 should be hidden. A parameter is hidden as soon as any of the hide_if conditions regarding the parameter are fulfilled.

To ensure that no parameter is accidentally hidden, accessing the value of hidden parameter is an error and aborts the program.

Use this option if one parameter is unused, depending on another parameters value.

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 861 of file arg_parser.hpp.

References FEAT::value, and XASSERTM.

◆ 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, can be of form "foo" or "--foo". Additional dashes within the flag are allowed, e.g. '–foo-bar'

Definition at line 534 of file arg_parser.hpp.

References FEAT::String::starts_with(), and XASSERTM.

◆ 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 658 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 799 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 830 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 1032 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 676 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 642 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.

See also
Path specification in PropertyMap

Definition at line 586 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 772 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, can be of form "x" or "-x" where x is some letter.

Definition at line 512 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 741 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 704 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 488 of file arg_parser.hpp.

◆ _env_prefix

template<typename T_ >
String FEAT::ParameterBuilder< T_ >::_env_prefix
private

Definition at line 490 of file arg_parser.hpp.

◆ _flag_prefix

template<typename T_ >
String FEAT::ParameterBuilder< T_ >::_flag_prefix
private

Definition at line 492 of file arg_parser.hpp.

◆ _property_path_prefix

template<typename T_ >
String FEAT::ParameterBuilder< T_ >::_property_path_prefix
private

Definition at line 491 of file arg_parser.hpp.


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