![]() |
advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
|
#include <variables.h>
Public Types | |
typedef std::shared_ptr< variables > | pointer_t |
typedef std::map< std::string, std::string > | variable_t |
Public Member Functions | |
std::string | get_variable (std::string const &name) const |
Return the value of the named variable. | |
variable_t const & | get_variables () const |
Return a reference to the map of variables. | |
bool | has_variable (std::string const &name) const |
Check whether a variable is defined. | |
std::string | process_value (std::string const &value) const |
Process variables against a parameter. | |
void | set_variable (std::string const &name, std::string const &value, assignment_t assignment=assignment_t::ASSIGNMENT_SET) |
Set a variable. | |
Static Public Member Functions | |
static std::string | canonicalize_variable_name (std::string const &name) |
Canonicalize the variable name. | |
Private Types | |
typedef string_set_t | variable_names_t |
Private Member Functions | |
std::string | recursive_process_value (std::string const &value, variable_names_t &names) const |
Internal function processing variables recursively. | |
Private Attributes | |
variable_t | f_variables = variable_t() |
Definition at line 58 of file variables.h.
typedef std::shared_ptr<variables> advgetopt::variables::pointer_t |
Definition at line 61 of file variables.h.
Definition at line 77 of file variables.h.
typedef std::map<std::string, std::string> advgetopt::variables::variable_t |
Definition at line 62 of file variables.h.
|
static |
This function canonicalizes the name of a variable.
This means:
getopt_invalid | If the variable or one of the section names start with a digit, this exception is raised. The exception is also raised if we detect an empty section name (as in "::test" or "double::::scope" or "not..allowed"). |
[in] | name | The name of the variable. |
Definition at line 79 of file variables.cpp.
References advgetopt::option_flags_merge().
Referenced by get_variable(), has_variable(), and set_variable().
std::string advgetopt::variables::get_variable | ( | std::string const & | name | ) | const |
This function searches for the named variable and returns its value if defined.
[in] | name | The name of the variable to retrieve. |
Definition at line 154 of file variables.cpp.
References canonicalize_variable_name(), f_variables, and advgetopt::option_flags_merge().
Referenced by recursive_process_value().
variables::variable_t const & advgetopt::variables::get_variables | ( | ) | const |
This function returns a reference to the whole map of variables.
The map is composed of named values. The first string is the name of variables and the second string is the value.
Definition at line 179 of file variables.cpp.
References f_variables.
If you want to verify that a variable is defined before retrieving it, you can use this function. A variable can be set to the empty string so checking the returned value of the get_variable() is not sufficient to know whether the variable is defined or log.
[in] | name | The name of the variable to check. |
Definition at line 138 of file variables.cpp.
References canonicalize_variable_name(), f_variables, and advgetopt::option_flags_merge().
std::string advgetopt::variables::process_value | ( | std::string const & | value | ) | const |
Whenever a parameter is retrieved, its value is passed through this function and if the variable processing is allowed, it searches for ${...}
sequances and when such are found, it replaces them with the corresponding variable content.
The process is recursive meaning that if a variable includes the ${...}
sequence, that variable will itself also be replaced.
The variables can be defined in a [variables]
section and by the programmer by calling the set_variable() function.
[in] | value | The parameter value to be processed. |
Definition at line 291 of file variables.cpp.
References advgetopt::option_flags_merge(), and recursive_process_value().
|
private |
This function goes through value and replaces the ${...}
with the corresponding variable data. The content of a variable is itself passed through this process so it is recursive.
The function records which variables it has worked on so far to prevent the function from re-adding the same variable (avoid infinite loop).
[in] | value | The value to parse. |
[in] | names | A set of variable names that have already been processed. |
Definition at line 316 of file variables.cpp.
References get_variable(), advgetopt::option_flags_merge(), and recursive_process_value().
Referenced by process_value(), and recursive_process_value().
void advgetopt::variables::set_variable | ( | std::string const & | name, |
std::string const & | value, | ||
assignment_t | assignment = assignment_t::ASSIGNMENT_SET |
||
) |
This function sets a variable in the getopt object.
The value of variables can be used to replace ${...}
entries in parameters found on the command line or in configuration files.
By default, if that variable already existed, then its value gets replaced (assignment_t::ASSIGNMENT_SET).
You can use this function to define a default after loading data with:
${...}
references. When parsing a parameter for variables, such are replaced recursively. See process_value() for details.[in] | name | The name of the variable. |
[in] | value | The value of the variable. |
[in] | assignment | The operator to use to set this variable. |
Definition at line 212 of file variables.cpp.
References advgetopt::ASSIGNMENT_APPEND, advgetopt::ASSIGNMENT_NEW, advgetopt::ASSIGNMENT_OPTIONAL, canonicalize_variable_name(), f_variables, and advgetopt::option_flags_merge().
|
private |
Definition at line 83 of file variables.h.
Referenced by get_variable(), get_variables(), has_variable(), and set_variable().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.