All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::optparse::typed_value< T > Struct Template Reference

Represents a command line value. More...

Inherits gears::optparse::value_base.

Public Member Functions

template<typename Action >
 typed_value (Action action)
 Constructs from an action.
 
 typed_value (T &object)
 Constructs a typed_value storing its result to a variable. More...
 
bool is_active () const noexceptoverride
 Checks if the typed_value has gone through parsing. More...
 
template<typename Action >
typed_valueaction (Action &&action)
 Specifies the action callback to use for parsing. More...
 
const T & get () const
 Retrieves the parsed value. More...
 
const T & get_or (const T &def) const noexcept
 Retrieves the parsed value or a reasonable default. More...
 

Public Attributes

std::string metavar
 The value name to use in help messages.
 
size_t nargs = 1
 Number of arguments to expect.
 

Related Functions

(Note that these are not member functions.)

template<typename Container , typename Action = store<typename Container::value_type>>
std::unique_ptr< value_base > compose (std::string metavar="", Action action=Action{})
 Returns a typed_value for composed values. More...
 
template<typename T , typename Action = store<T>>
std::unique_ptr< value_base > bind_to (T &t, std::string metavar="", Action action=Action{})
 Returns a typed_value that binds the result to a variable. More...
 
template<typename T >
std::unique_ptr< value_base > constant (const T &t)
 Returns a typed_value that returns a constant value. More...
 
template<typename T , typename Action = store<T>>
std::unique_ptr< value_base > value (std::string metavar="", Action action=Action{})
 Returns a regular typed_value. More...
 
template<typename T , typename Action >
std::unique_ptr< value_base > custom (Action action, std::string metavar="")
 Returns a typed_value with a custom action. More...
 
template<typename Container , typename Action = store<typename Container::value_type>>
std::unique_ptr< value_base > list (size_t arguments, std::string metavar="", Action action=Action{})
 Returns a typed_value that handles a list of values. More...
 

Detailed Description

template<typename T>
struct gears::optparse::typed_value< T >

A class that represents a command line value to be used with command line options that expect one or more arguments to be given. This class is usually not used explicitly as many factory functions are provided for common use cases.

Template Parameters
TThe type of argument expected.

Definition at line 51 of file value.hpp.

Constructor & Destructor Documentation

template<typename T>
gears::optparse::typed_value< T >::typed_value ( T &  object)
inline

Constructs a typed_value with an l-value variable. This allows you to bind the result of the parsed result to a C++ variable easily.

Parameters
objectThe object to bind the result of parsing to.

Definition at line 92 of file value.hpp.

Member Function Documentation

template<typename T>
template<typename Action >
typed_value& gears::optparse::typed_value< T >::action ( Action &&  action)
inline

Specifies the action callback to use for parsing. All parsing of values is done through a callback mechanism called actions. The signature of an action must be T(const std::string&, const std::string&). Many actions are provided with a reasonable default, this should only be called if you have created a custom action. The first argument passed is the key of the command line option, and the second one is the value. For example, passing --foo=test on the command line would result in the first argument being --foo and the second being test.

Parameters
actionThe action callback to use.
Returns
A reference to the typed_value object.

Definition at line 119 of file value.hpp.

template<typename T>
const T& gears::optparse::typed_value< T >::get ( ) const
inline

Retrieves the parsed value. If the typed_value object hasn't gone through parsing yet then this function throws.

Exceptions
std::runtime_errorThrown when is_active is false.
Returns
The parsed value.

Definition at line 133 of file value.hpp.

template<typename T>
const T& gears::optparse::typed_value< T >::get_or ( const T &  def) const
inlinenoexcept

Retrieves the parsed value or a reasonable default if the value hasn't gone through parsing.

Parameters
defThe default value if is_active is false.
Returns
The parsed value or def.

Definition at line 152 of file value.hpp.

template<typename T>
bool gears::optparse::typed_value< T >::is_active ( ) const
inlineoverridenoexcept

Checks if the typed_value has gone through parsing successfully. If the parsing threw then this returns false.

Returns
true if parsing was successful, false otherwise.

Definition at line 101 of file value.hpp.

Friends And Related Function Documentation

template<typename T , typename Action = store<T>>
std::unique_ptr< value_base > bind_to ( T &  t,
std::string  metavar = "",
Action  action = Action{} 
)
related

Returns a typed_value that when parsed, binds the result to an l-value variable. For example, having an option being declared like so:

int x;
opt::option test = { "test", 't', "tests something", opt::bind_to(x) };

When the command line is parsed with 10 as a value, e.g. --test=10 then the variable x will contain the value 10.

Parameters
tThe variable to bind the result to.
metavarThe metavar variable to assign to the optparse::value.
actionThe action to use for parsing the value. Defaults to optparse::store.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 204 of file value.hpp.

template<typename Container , typename Action = store<typename Container::value_type>>
std::unique_ptr< value_base > compose ( std::string  metavar = "",
Action  action = Action{} 
)
related

Returns a typed_value for composed values. Composed values return a list of all the elements concatenated. For example, having a composed optparse::option would allow things like --test=10 --test=20 --test=30 to produce a list of [10, 20, 30].

Parameters
metavarThe metavar variable to assign to the optparse::value.
actionThe internal action to parse the values to.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 177 of file value.hpp.

template<typename T >
std::unique_ptr< value_base > constant ( const T &  t)
related

Returns a typed_value that returns a constant value. This is a wrapper around creating a typed_value and setting the action to optparse::store_const. Note that if this is used, then the option object technically takes no value. So having, for example, --stuff=val in the command line throws an error.

Parameters
tThe constant value to return when the option is active.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 224 of file value.hpp.

template<typename T , typename Action >
std::unique_ptr< value_base > custom ( Action  action,
std::string  metavar = "" 
)
related

Returns a typed_value with a custom action. This is typically used if you want to just specify a custom action parser. Every other factory function provides an action parameter to handle inputting custom actions, so this one is provided to switch the parameter list of optparse::value.

Template Parameters
TThe type of value to store.
Parameters
actionThe action used to parse values.
metavarThe metavar variable to assign to the optparse::value.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 265 of file value.hpp.

template<typename Container , typename Action = store<typename Container::value_type>>
std::unique_ptr< value_base > list ( size_t  arguments,
std::string  metavar = "",
Action  action = Action{} 
)
related

Returns a typed_value that handles a list of values.

Template Parameters
ContainerThe internal container to hold values. Must meet the Container concept.
Parameters
argumentsThe number of elements to store in the list.
metavarThe metavar variable to assign to the optparse::value.
actionThe action to use for parsing. If not provided, defaults to optparse::store.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 283 of file value.hpp.

template<typename T , typename Action = store<T>>
std::unique_ptr< value_base > value ( std::string  metavar = "",
Action  action = Action{} 
)
related

Returns a regular typed_value. If a command line value is required for your interface, this is the recommended way of creating values. If you prefer the parameters to be (action, metavar) then optparse::custom implements this order.

Template Parameters
TThe type of value to store.
Parameters
metavarThe metavar variable to assign to the optparse::value.
actionThe action used to parse values. If not provided, defaults to optparse::store.
Returns
A polymorphic typed_value to use with optparse::option.

Definition at line 244 of file value.hpp.