Provides access to command line options and a configuration file. More...
#include <Options.h>
Classes | |
class | Error |
Exception for class Options. More... | |
Public Member Functions | |
Options (int argc, char **argv, std::initializer_list< Group > const &optionGroups) | |
Constructor from command line arguments. | |
bool | Exists (std::string const &label) const |
Checks if an option with the given label exists. | |
template<typename T > | |
T | GetAs (std::string const &label) const |
Returns the value of the option with the given label. | |
template<typename T , typename Checker > | |
T | GetAsChecked (std::string const &label, Checker const &checker) const |
Checks and returns the value of the option with the given label. | |
YAML::Node const & | GetConfig () const |
Returns parsed YAML configuration. | |
template<typename T > | |
T | NodeAs (YAML::Node const &node, std::initializer_list< std::string > const keys) |
template<typename T , typename Checker > | |
T | NodeAsChecked (YAML::Node const &node, Checker const &checker, std::initializer_list< std::string > const keys) |
Static Public Member Functions | |
template<typename T > | |
static T | NodeAs (YAML::Node const &node, std::initializer_list< std::string > const keys={}) |
Returns the content of a node converted into requested type. | |
template<typename T , typename Checker > | |
static T | NodeAsChecked (YAML::Node const &node, Checker const &checker, std::initializer_list< std::string > const keys={}) |
Checks and returns the content of a node. | |
Private Member Functions | |
void | PrintUsage () const |
Prints usage instructions. | |
Private Attributes | |
std::string | programName_ |
Name of the program extracted from the first command line argument. | |
boost::program_options::options_description | allOptions_ |
All registered options. | |
boost::program_options::variables_map | optionMap_ |
Map with parsed options. | |
YAML::Node | config_ |
Parsed YAML configuration. |
Provides access to command line options and a configuration file.
The parsing of command line options is implemented with Boost.Program_options. Values of options can be accessed by their labels using methods GetAs and GetAsChecked. All supported options must be registered beforehand and provided to the constructor. However, several options are added automatically, see Options().
The configuration file must be of YAML format. It is parsed with yaml-cpp and exposed via method GetConfig. The content of individual nodes in the configuration can be extracted using native methods of YAML::Node, but it is recommended to use NodeAs and NodeAsChecked as they provide a better error reporting.
Options::Options | ( | int | argc, | |
char ** | argv, | |||
std::initializer_list< Group > const & | optionGroups | |||
) |
Constructor from command line arguments.
[in] | argc,argv | Number of command line arguments and an array with their values, as given to main . |
[in] | optionGroups | Zero or more instances of boost::program_options::options_description that describe possible command line options. |
If an unregistered option is encountered, terminates the program. Several options are added automatically:
-h
,--help Prints usage information and exists the program.--config
Sets location of the configuration file. The path is resolved using FileInPath.--version
Prints version and exits the program.-v
,--verbosity Sets the verbosity level for the log. T Options::GetAs | ( | std::string const & | label | ) | const [inline] |
Returns the value of the option with the given label.
The value is represented with the given type. If the requested option has not been specified in the call to the program or if the option does not have a value (i.e. this is a pure flag), throws an exception of type Options::Error.
T Options::GetAsChecked | ( | std::string const & | label, | |
Checker const & | checker | |||
) | const [inline] |
Checks and returns the value of the option with the given label.
[in] | label | Label that identifies the option. |
[in] | checker | Object of type Checker that defines bool operator()(T const &) and checks the validity of the returned value for the option. |
Behaves in the same way as GetAs but additionally checks the value of the option using the provided functor. If the value fails the check, throws an exception of type Options::Error. In a typical use case the checker will be a lambda function.
YAML::Node const& Options::GetConfig | ( | ) | const |
Returns parsed YAML configuration.
A brief introduction to the parsing library is available here. If no configuration file has been given, this method throws an exception of type Options::Error.
static T Options::NodeAs | ( | YAML::Node const & | node | ) | [inline, static] |
Returns the content of a node converted into requested type.
Compared to the native YAML::Node::as, this function provides a more meaningful error message if the node is not defined (meaning that it does not exist in the configuration).
static T Options::NodeAsChecked | ( | YAML::Node const & | node, | |
Checker const & | checker | |||
) | [inline, static] |
Checks and returns the content of a node.
[in] | node | YAML node whose content is to be read. |
[in] | checker | Object of type Checker that defines bool operator()(T const &) and checks the validity of the content. |
T | Type into which to convert the content of the node. |
Behaves in the same way as NodeAs but additionally checks the extracted value using the provided functor. If the value fails the check, throws an exception of type Options::Error. In a typical use case the checker will be a lambda function.
YAML::Node Options::config_ [private] |
Parsed YAML configuration.
If no configuration file has been given in command line arguments, the type of this node is Null.