![]() |
advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
|
#include <option_info.h>
Classes | |
struct | callback_entry_t |
Public Types | |
typedef int | callback_id_t |
typedef std::function< void(option_info const &opt)> | callback_t |
typedef std::map< std::string, pointer_t > | map_by_name_t |
typedef std::map< short_name_t, pointer_t > | map_by_short_name_t |
typedef std::shared_ptr< option_info > | pointer_t |
typedef std::vector< pointer_t > | vector_t |
Public Member Functions | |
option_info (std::string const &name, short_name_t short_name=NO_SHORT_NAME) | |
Create a new option_info object. | |
callback_id_t | add_callback (callback_t const &c) |
Add a callback to call on a change to this value. | |
void | add_flag (flag_t flag) |
Make sure a given flag is set. | |
bool | add_value (std::string const &value, string_list_t const &option_keys=string_list_t(), option_source_t source=option_source_t::SOURCE_DIRECT) |
Add a value to this option. | |
int | find_value_index_by_key (std::string key, int idx=0) const |
Get the index at which a value with the given key is defined. | |
pointer_t | get_alias_destination () const |
Get a link to the destination alias. | |
std::string | get_basename () const |
Retrieve the name of the option without any section names. | |
std::string const & | get_default () const |
Retrieve the default value. | |
double | get_double (int idx=0) const |
Get the value as a double. | |
std::string | get_environment_variable_name () const |
Retrieve the environment variable name of this option. | |
bool | get_environment_variable_value (std::string &value, char const *intro=nullptr) const |
Retrieve the environment variable value of this option. | |
flag_t | get_flags () const |
Retrieve the flags. | |
std::string const & | get_help () const |
Get the help string. | |
long | get_long (int idx=0) const |
Get the value as a long. | |
string_list_t const & | get_multiple_separators () const |
Retrieve the list of separators for this argument. | |
std::string const & | get_name () const |
Get the long name of the option. | |
std::string | get_section_name () const |
Retrieve the name of the sections. | |
string_list_t | get_section_name_list () const |
Retrieve a list of section names. | |
short_name_t | get_short_name () const |
Get the short name of the option. | |
validator::pointer_t | get_validator () const |
Retrieve a pointer to the validator. | |
std::string | get_value (int idx=0, bool raw=false) const |
Retrieve the value. | |
variables::pointer_t | get_variables () const |
Retrieve the list of variables held by this option info. | |
bool | has_default () const |
Check whether this option has a default value. | |
bool | has_flag (flag_t flag) const |
Check whether a flag is set. | |
bool | has_value (std::string const &value) const |
Check whether one of the values matches the input. | |
bool | is_default_option () const |
Check whether this is the default option. | |
bool | is_defined () const |
Check whether a value is defined. | |
void | lock (bool always=true) |
Lock this value. | |
void | remove_callback (callback_id_t id) |
Remove a callback. | |
void | remove_default () |
Remove the default value. | |
void | remove_flag (flag_t flag) |
Make sure a given flag is not set. | |
void | reset () |
Reset this value. | |
void | set_alias_destination (pointer_t destination) |
Set the alias option. | |
void | set_default (char const *default_value) |
Set the default value of this option. | |
void | set_default (std::string const &default_value) |
Set the default value. | |
void | set_environment_variable_name (char const *name) |
Set the default value of this option. | |
void | set_environment_variable_name (std::string const &name) |
Set the option specific environment variable name. | |
void | set_flags (flag_t flags) |
Get the flags. | |
void | set_help (char const *help) |
Set the help string for this option. | |
void | set_help (std::string const &help) |
Set the help string for this option. | |
void | set_multiple_separators (char const *const *separators) |
Set the list of separators. | |
void | set_multiple_separators (string_list_t const &separators) |
Set the list of separators. | |
bool | set_multiple_values (std::string const &value, string_list_t const &option_keys=string_list_t(), option_source_t source=option_source_t::SOURCE_DIRECT) |
Set a multi-value at once. | |
void | set_short_name (short_name_t short_name) |
Assign a short name to an option. | |
bool | set_validator (std::nullptr_t) |
Clear the validator. | |
bool | set_validator (std::string const &name_and_params) |
Set the validator for this option. | |
bool | set_validator (validator::pointer_t validator) |
Set the validator for this option. | |
bool | set_value (int idx, std::string const &value, string_list_t const &option_keys=string_list_t(), option_source_t source=option_source_t::SOURCE_DIRECT) |
Replace a value. | |
void | set_variables (variables::pointer_t vars) |
Assign variables to this option info. | |
size_t | size () const |
Retrieve the number of values defined for this option. | |
option_source_t | source () const |
Return the source of this option info. | |
string_list_t const & | trace_sources () const |
Get the trace of this option. | |
void | unlock () |
Unlock this value. | |
Static Public Member Functions | |
static void | set_configuration_filename (std::string const &filename) |
Save the filename of the current configuration file. | |
static void | set_trace_sources (bool trace) |
Whether the sources should be traced. | |
Private Types | |
typedef std::vector< callback_entry_t > | callback_vector_t |
Private Member Functions | |
void | trace_source (int idx) |
Remember the source information at of this last change. | |
bool | validate_all_values () |
Validate all the values of this option_info object. | |
bool | validates (int idx=0) |
Check a value validity. | |
void | value_changed (int idx) |
Call whenever the value changed so we can handle callbacks. | |
Private Attributes | |
pointer_t | f_alias_destination = pointer_t() |
callback_vector_t | f_callbacks = callback_vector_t() |
std::string | f_default_value = std::string() |
std::vector< double > | f_double = std::vector<double>() |
std::string | f_environment_variable_name = std::string() |
flag_t | f_flags = GETOPT_FLAG_NONE |
std::string | f_help = std::string() |
std::vector< long > | f_integer = std::vector<long>() |
string_list_t | f_multiple_separators = string_list_t() |
std::string | f_name = std::string() |
id_t | f_next_callback_id = 0 |
short_name_t | f_short_name = NO_SHORT_NAME |
option_source_t | f_source = option_source_t::SOURCE_UNDEFINED |
string_list_t | f_trace_sources = string_list_t() |
validator::pointer_t | f_validator = validator::pointer_t() |
string_list_t | f_value = string_list_t() |
variables::pointer_t | f_variables = variables::pointer_t() |
Definition at line 75 of file option_info.h.
Definition at line 83 of file option_info.h.
typedef std::function<void (option_info const & opt)> advgetopt::option_info::callback_t |
Definition at line 82 of file option_info.h.
|
private |
Definition at line 168 of file option_info.h.
typedef std::map<std::string, pointer_t> advgetopt::option_info::map_by_name_t |
Definition at line 80 of file option_info.h.
Definition at line 81 of file option_info.h.
typedef std::shared_ptr<option_info> advgetopt::option_info::pointer_t |
Definition at line 78 of file option_info.h.
typedef std::vector<pointer_t> advgetopt::option_info::vector_t |
Definition at line 79 of file option_info.h.
advgetopt::option_info::option_info | ( | std::string const & | name, |
short_name_t | short_name = NO_SHORT_NAME |
||
) |
This function creates a new option_info object with the specified name
and short_name
. The short_name
is optional.
When adding options to a map of options, all the long and short names must be unique. See the add_child() function for details.
The short_name
parameter is a UTF-32 character. To not offer a short name for an option, use NO_SHORT_NAME as the value (which is the default if not specified to the constructor.)
The "--" long name is viewed as the separator between options and filenames. When "--" is found by itself on the command line, then it is viewed as a switch to go from having options to only having filenames. Of course, these options may be used as any type of values, not just filenames (it could be URLs, email addresses, numbers, etc.)
The "--" separator cannot be assigned a short name.
The "*" long name is viewed as the accept all option. This means all the options may not be defined in the list of options but we still want to accept them. This is to allow dynamically defined (supported) command options and especially to not have to declare all the valid options found in a configuration file.
It is customary to support dashes between words in options appearing on the command line (--help-me
), however, it is unusual in configuration files where underscores are used instead (under_score
.) When we compare option names, ‘’-' == '_'` is always considered true so either dashes or underscore can be used in both cases.
For this reason, the long name is saved with only dashes. That means all the maps are indexed using the long name with dashes.
getopt_exception_logic | The constructor raises the invalid exception if the long name is an empty string since this is not allowed. It will also raise that exception if the name is the default option ("--") and a short name is also defined. (i.e. no short name is allowed along the default option.) |
[in] | name | The (long) name of this option. |
[in] | short_name | The short name of this option (one character.) |
Definition at line 224 of file option_info.cpp.
References add_flag(), f_name, advgetopt::GETOPT_FLAG_DEFAULT_OPTION, advgetopt::NO_SHORT_NAME, and advgetopt::option_flags_merge().
option_info::callback_id_t advgetopt::option_info::add_callback | ( | callback_t const & | c | ) |
Since we now officially support dynamically setting option values, we decided to add a callback mechanism that lets you know that an option changed. That way you can react to the change as soon as possible instead of having to poll for the value once in a while.
[in] | c | The callback. Usually an std::bind() call. |
Definition at line 1985 of file option_info.cpp.
References f_callbacks, f_next_callback_id, advgetopt::get_global_mutex(), lock(), and advgetopt::option_flags_merge().
This function adds the given flag from the set of flags being set.
flag
.[in] | flag | The flag(s) to set. |
Definition at line 578 of file option_info.cpp.
References f_flags, advgetopt::option_flags_merge(), and set_flags().
Referenced by option_info(), lock(), and set_default().
bool advgetopt::option_info::add_value | ( | std::string const & | value, |
string_list_t const & | option_keys = string_list_t() , |
||
option_source_t | source = option_source_t::SOURCE_DIRECT |
||
) |
Whenever an option is found it may be followed by one or more values. This function is used to add these values to this option.
Later you can use the size() function to know how many values were added and the get_value() to retrieve any one of these values.
[in] | value | The value to add to this option. |
[in] | option_keys | The set of keys found at the end of the option name. |
[in] | source | Where the value comes from. |
Definition at line 1176 of file option_info.cpp.
References f_value, advgetopt::GETOPT_FLAG_MULTIPLE, has_flag(), advgetopt::option_flags_merge(), set_value(), and source().
This function searches a value with the specified key
and return the index where it was found.
If the function does not find a value starting with key
, then it returns -1.
The function can be called with the start idx
set to a value other than zero in which case the search starts at that index. It is valid to call the function with idx
larger or equal to the number of values defined.
getopt_logic_error | If the idx parameter is negative, this exception is raised. |
getopt_undefined | If no values were defined, this exception is raised. To avoid receiving the exception, make sure to first test with is_defined(). Note that if the requested key is not found, the function simply returns an empty string opposed to raising an exception. |
[in] | key | The key to search for. |
[in] | idx | Start from this index. |
Definition at line 1715 of file option_info.cpp.
References f_name, f_value, and advgetopt::option_flags_merge().
Referenced by set_value().
option_info::pointer_t advgetopt::option_info::get_alias_destination | ( | ) | const |
This function returns a pointer to the aliased option.
Definition at line 1020 of file option_info.cpp.
References f_alias_destination.
std::string advgetopt::option_info::get_basename | ( | ) | const |
The name of an option can include section names. These are rarely used on the command line, but they are useful for configuration files if you want to create multiple layers of options (a.k.a. sections.)
This function removes all the section names from the option name and returns what's left.
Definition at line 344 of file option_info.cpp.
References f_name, and advgetopt::option_flags_merge().
std::string const & advgetopt::option_info::get_default | ( | ) | const |
This function returns the default value.
Definition at line 722 of file option_info.cpp.
References f_default_value.
This function returns the value converted to a double
.
If the value does not represent a valid double value, an error is emitted through the logger.
The value will be parsed through the variables if defined and this parameter allows it. This means the value may be a variable reference instead of an actually value (i.e. ${pi}
)
getopt_exception_undefined | If the value was not defined, the function raises this exception. |
[in] | idx | The index of the value to retrieve as a double. |
idx
converted to a double or -1.0 on error. Definition at line 1845 of file option_info.cpp.
References advgetopt::validator_double::convert_string(), f_double, f_name, f_value, advgetopt::get_global_mutex(), get_value(), lock(), and advgetopt::option_flags_merge().
std::string advgetopt::option_info::get_environment_variable_name | ( | ) | const |
Each command line option can be assigned an environment variable name. When checking for the global environment variable name, the advgetopt library also checks each option's environment variable name which has priority over the global variable (i.e. it will overwrite a value found in the global environment variable).
The value returned is an empty string by default, which means the option does not have a value defined in an environment variable.
Definition at line 492 of file option_info.cpp.
References f_environment_variable_name.
bool advgetopt::option_info::get_environment_variable_value | ( | std::string & | value, |
char const * | intro = nullptr |
||
) | const |
Each command line option can be assigned an environment variable name. Using this name, this function attempts to retrieve the corresponding value defined in that variable.
[out] | value | Save the resulting value in this variable. |
[in] | intro | The introducer to prepend. May be nullptr. |
Definition at line 512 of file option_info.cpp.
References f_environment_variable_name, and advgetopt::option_flags_merge().
flag_t advgetopt::option_info::get_flags | ( | ) | const |
This function retrieves all the flags defined in this option.
To just check whether a flag is set or not, use the has_flag() function instead.
Definition at line 608 of file option_info.cpp.
References f_flags.
std::string const & advgetopt::option_info::get_help | ( | ) | const |
This function returns the help string for this command line option.
Definition at line 795 of file option_info.cpp.
References f_help.
This function returns the value converted to a long
.
If the value does not represent a valid long value, an error is emitted through the logger.
The value will be parsed through the variables if defined and this parameter allows it. This means the value may be a variable reference instead of an actually value (i.e. ${one}
)
getopt_exception_undefined | If the value was not defined, the function raises this exception. |
[in] | idx | The index of the value to retrieve as a long. |
idx
converted to a long or -1 on error. Definition at line 1770 of file option_info.cpp.
References advgetopt::validator_integer::convert_string(), f_integer, f_name, f_value, advgetopt::get_global_mutex(), get_value(), lock(), and advgetopt::option_flags_merge().
string_list_t const & advgetopt::option_info::get_multiple_separators | ( | ) | const |
This function returns a reference to the list of separators of this option. It is expected to be used when a value is found in a configuration file or a command line in an environment variable. Parameters on the command line are already broken down by the shell and we do not do any further manipulation with those.
--tags=a,b,c,d
). It also works in environment variables. I am thinking that the --tags a,b,c,d
should probably work the same way though because otherwise many people will have a surprise.Definition at line 1095 of file option_info.cpp.
References f_multiple_separators.
std::string const & advgetopt::option_info::get_name | ( | ) | const |
This option retrieves the long name of the option.
Definition at line 287 of file option_info.cpp.
References f_name.
std::string advgetopt::option_info::get_section_name | ( | ) | const |
The name of an option can include section names. These are rarely used on the command line, but they are useful for configuration files if you want to create multiple layers of options (a.k.a. sections.)
This function returns all the section names found in this option name. The last scope operator gets removed too.
If the name does not include any sections, then this function returns an empty string.
Definition at line 371 of file option_info.cpp.
References f_name, and advgetopt::option_flags_merge().
string_list_t advgetopt::option_info::get_section_name_list | ( | ) | const |
The name of an option can include section names. These are rarely used on the command line, but they are useful for configuration files if you want to create multiple layers of options (a.k.a. sections.)
This function returns a string_list_t of the section names found in this option name.
If the name does not include any sections, then this function returns an empty list.
Definition at line 398 of file option_info.cpp.
References f_name, and advgetopt::option_flags_merge().
short_name_t advgetopt::option_info::get_short_name | ( | ) | const |
This function returns the short_name
of this option.
The short name is a Unicode character (UTF-32).
Definition at line 326 of file option_info.cpp.
References f_short_name.
validator::pointer_t advgetopt::option_info::get_validator | ( | ) | const |
The validator of an option may be used for additional tasks such as converting the value to a specific type (i.e. a string to an integer, for example.)
This function allows you to retrieve the validator to be able to make use of those functions. You will have to use std::dynamic_cast_pointer<>() to change the type of validator to the specialized validator of this option. If that returns a null pointer, then the option is not using that type of validator.
Definition at line 987 of file option_info.cpp.
References f_validator.
This function returns the value for this option. By default, set the idx
parameter to zero.
The number of values is defined by the size() function.
The is_defined() function returns true if at least one value is defined. It is a good idea to check first otherwise you will get an exception.
If the parameter is marked as one that can be processed through the variables::process_value() function and the variables were defined with set_variables(), then the value will be processed for variables unless you set the raw
parameter to true.
getopt_exception_undefined | If the idx parameter is too large or no value was found for this option, then this function raises an invalid error. |
[in] | idx | The index of the parameter to retrieve. |
[in] | raw | Whether to allow the variable processing or not. |
idx
. Definition at line 1661 of file option_info.cpp.
References f_name, f_value, f_variables, advgetopt::GETOPT_FLAG_PROCESS_VARIABLES, has_flag(), and advgetopt::option_flags_merge().
Referenced by get_double(), and get_long().
variables::pointer_t advgetopt::option_info::get_variables | ( | ) | const |
This option info object may replace variables in values (see get_value() for details) using this list of variables. Option info objects created by the getopt class always have this pointer set, although the list of variables may be empty.
Definition at line 1125 of file option_info.cpp.
References f_variables.
bool advgetopt::option_info::has_default | ( | ) | const |
Whenever an option is given a default value, the GETOPT_FLAG_HAS_DEFAULT flag gets set. This allows us to distinguish between an option with a default which is the empty string and an option without a default.
The set_default() forces the flag to be set.
The remove_default() clears the flag.
Definition at line 646 of file option_info.cpp.
References advgetopt::GETOPT_FLAG_HAS_DEFAULT, and has_flag().
This function is used to check whether a flag is set or not.
flag
parameter can be set to more than one flag in which case the function returns true if any one of those flags is set.Definition at line 624 of file option_info.cpp.
References f_flags, and advgetopt::option_flags_merge().
Referenced by add_value(), get_value(), has_default(), is_default_option(), set_multiple_values(), set_value(), and trace_source().
This function searches the set of existing values in this option_info and if found returns true.
[in] | value | The value to search in this option. |
Definition at line 1147 of file option_info.cpp.
References f_value, and advgetopt::option_flags_merge().
bool advgetopt::option_info::is_default_option | ( | ) | const |
This function checks whether this option represents the default option. The default option is where non-options, generally filenames, are added when not following an argument.
The name of the default option is always "--". However, it is not required. When no default option is defined, filenames can't be specified and when such are found on the command line, an error ensues.
Definition at line 430 of file option_info.cpp.
References f_name, advgetopt::GETOPT_FLAG_DEFAULT_OPTION, and has_flag().
bool advgetopt::option_info::is_defined | ( | ) | const |
When parsing the options on the command line or a configuration file, values get added to the various existing option_info. If a special "*" option is also defined, then any value found on the command line or the configuration file are returned.
To know whether this or that option was defined with a value, use this function. Even an option which doesn't come with a parameter will get an is_defined() returning true once it was found on the command line. The value will be the empty string.
Definition at line 1531 of file option_info.cpp.
References f_value.
This function allows for locking a value so further reading of data from different sources will not overwrite it.
When parsing the data we have multiple levels. Here are these levels in priority order (first option found is the one we keep):
./\<name>.conf
) ~/.config/\<proc>/\<name>.conf
) /etc/\<proc>/\<proc>.d/\<ohter-name>.conf
) /etc/\<proc>/\<other-name>.conf
) /etc/\<proc>/\<name>.conf
) /etc/\<proc>/\<name>.conf
)We use this lock because we want to support multiple values so just detecting that a value is set to not add more options is not a good test. Instead we lock the values that are set before moving to the next level.
[in] | always | Always lock that option, whether it is defined or not. |
Definition at line 1924 of file option_info.cpp.
References add_flag(), advgetopt::GETOPT_FLAG_LOCK, is_defined(), and advgetopt::option_flags_merge().
Referenced by add_callback(), get_double(), get_long(), remove_callback(), and value_changed().
void advgetopt::option_info::remove_callback | ( | callback_id_t | id | ) |
This function is the opposite of the add_callback(). It removes a callback that you previously added. This is useful if you are interested in hearing about the value when set but are not interested at all about future changes.
[in] | id | The id returned by the add_callback() function. |
Definition at line 2004 of file option_info.cpp.
References f_callbacks, advgetopt::get_global_mutex(), lock(), and advgetopt::option_flags_merge().
void advgetopt::option_info::remove_default | ( | ) |
Call this function remove the default value. The default string gets cleared and the GETOPT_FLAG_NO_DEFAULT flag gets set.
Definition at line 709 of file option_info.cpp.
References f_default_value, advgetopt::GETOPT_FLAG_HAS_DEFAULT, and remove_flag().
This function removes the given flag from the set of flags currently set.
flag
.[in] | flag | The flag(s) to clear. |
Definition at line 593 of file option_info.cpp.
References f_flags, advgetopt::option_flags_merge(), and set_flags().
Referenced by remove_default(), and unlock().
void advgetopt::option_info::reset | ( | ) |
This function clears the value so it is marked as undefined again.
To reuse the same getopt object multiple times, you can use the reset() function which clears the values. Then you can parse a new set of argc/argv parameters.
Definition at line 1960 of file option_info.cpp.
References f_double, f_integer, f_source, f_value, is_defined(), advgetopt::SOURCE_UNDEFINED, and value_changed().
void advgetopt::option_info::set_alias_destination | ( | option_info::pointer_t | destination | ) |
After loading all the options, we run the link_aliases() function which makes sure that all the options that are marked as an alias are properly linked.
[in] | alias | The final destination of this option. |
Definition at line 1001 of file option_info.cpp.
References f_alias_destination, advgetopt::GETOPT_FLAG_ALIAS, and advgetopt::option_flags_merge().
While parsing a configuration file, this function gets called to set the name which is used to generate the trace of the source of all the configuration data.
Definition at line 1610 of file option_info.cpp.
References advgetopt::option_flags_merge().
Referenced by advgetopt::getopt::process_configuration_file().
This function is an overload which allows us to call set_default() with a nullptr.
[in] | default_value | The new default value for this option. |
Definition at line 691 of file option_info.cpp.
References advgetopt::option_flags_merge(), and set_default().
This function sets the default value for this option.
The default value is always defined as a string, but it can later be converted to a different type using the option validator.
Often, though, the default value is not compatible with the validator. For example, you may have a parameter which is set to a percentage from -100% to +100% and the default may be the string "off".
[in] | default_value | The new default value for this option. |
Definition at line 673 of file option_info.cpp.
References add_flag(), f_default_value, advgetopt::GETOPT_FLAG_HAS_DEFAULT, and advgetopt::option_flags_merge().
Referenced by set_default().
This function is an overload which allows us to call set_default() with a nullptr.
[in] | default_value | The new default value for this option. |
Definition at line 467 of file option_info.cpp.
References set_environment_variable_name().
Each option can be given a specific environment variable name. That parameter is used to retrieve the option value if not defined on the command line.
By default this is an empty string.
[in] | name | The name of the environment variable attached to this option. |
Definition at line 451 of file option_info.cpp.
References f_environment_variable_name.
Referenced by set_environment_variable_name().
The options have flags determining various sub-options available to them. Right now we have flags to tell how each option can be used (on the command line, in an environment variable, or in a configuration file.)
[in] | flags | The new flags. |
Definition at line 563 of file option_info.cpp.
References f_flags, and advgetopt::option_flags_merge().
Referenced by add_flag(), and remove_flag().
This function is an overload which allows us to call set_help() with a nullptr and not crash. We just ignore the call when that happens.
[in] | help | The help string for this option or nullptr. |
Definition at line 776 of file option_info.cpp.
References advgetopt::option_flags_merge(), and set_help().
The usage() function prints this string whenever the command line includes the help command line option (such as -h
or --help
.)
The string can include various flags such as p
to include dynamically defined parameters. See the process_help_string() function for additional details about these parameters.
It represents a string to print out by the usage() function. The option purpose is solaly for adding a string of help in the output.
It represents the filename to read additional advgetopt options. In some cases, your static array of option structures is to define this special flag.
It represents the program license.
It represents the program copyright notice.
[in] | help | The help string for this option. |
Definition at line 763 of file option_info.cpp.
References f_help, and advgetopt::option_flags_merge().
Referenced by set_help().
Options marked with the GETOPT_FLAG_MULTIPLE flag get their value cut by separators when such is found in an environment variable or a configuration file.
This function saves the list of separators in a vector.
--tags=a,b,c,d
. I thinks that it should also work without the equal sign.[in] | separators | The list of separators to be used for this argument. |
Definition at line 1041 of file option_info.cpp.
References f_multiple_separators, and advgetopt::option_flags_merge().
void advgetopt::option_info::set_multiple_separators | ( | string_list_t const & | separators | ) |
Options marked with the GETOPT_FLAG_MULTIPLE flag get their value cut by separators when such is found in an environment variable or a configuration file.
This function saves the specified list of separators.
[in] | separators | The list of separators to be used for this argument. |
Definition at line 1070 of file option_info.cpp.
References f_multiple_separators, and advgetopt::option_flags_merge().
bool advgetopt::option_info::set_multiple_values | ( | std::string const & | value, |
string_list_t const & | option_keys = string_list_t() , |
||
option_source_t | source = option_source_t::SOURCE_DIRECT |
||
) |
This function views the value
parameter as a multi-value parameter which it breaks down in multiple parameters and add the results to this option_info object as the current value(s).
To separate the values, the function makes use of the separators as set by one the set_multiple_separators() functions.
The resulting values must not be the empty string. Empty strings are ignored. So if the separator is a comma and you write:
The result includes "foo" and "bar" and no empty strings.
The value can be quoted in which case it can include any of the separators.
The result includes "foo, bar" and "bar, foo" (without the quotes which get removed in the process).
value
will be set in the option once the function returns.[in] | value | The multi-value to save in this option. |
[in] | option_keys | The keys to which the values are applied. |
[in] | source | Where the value comes from. |
value
were considered valid.Definition at line 1404 of file option_info.cpp.
References f_double, f_integer, f_multiple_separators, f_name, f_source, f_value, advgetopt::GETOPT_FLAG_ARRAY, advgetopt::GETOPT_FLAG_MULTIPLE, has_flag(), advgetopt::option_flags_merge(), source(), advgetopt::SOURCE_UNDEFINED, advgetopt::split_string(), advgetopt::unquote(), validate_all_values(), and value_changed().
void advgetopt::option_info::set_short_name | ( | short_name_t | short_name | ) |
This function is used to assign a short name to an option.
It can be changed to anything, including the NO_SHORT_NAME special value.
[in] | short_name | The short name to assign to this option. |
Definition at line 312 of file option_info.cpp.
References f_short_name, and advgetopt::option_flags_merge().
This is a global flag that you can set before calling any getopt functions so that way you can make sure that you get a full trace of all the sources for all your options. Then you can use the –show-sources command line options to see the resulting data.
[in] | trace | Whether the sources should be traced. |
Definition at line 1582 of file option_info.cpp.
References advgetopt::option_flags_merge().
Referenced by advgetopt::anonymous_namespace{advgetopt.cpp}::check_for_show_sources().
bool advgetopt::option_info::set_validator | ( | std::nullptr_t | null_ptr | ) |
This function removes the existing validator by resetting the pointer back to nullptr.
[in] | null_ptr | Ignored. |
Definition at line 893 of file option_info.cpp.
References f_validator, and advgetopt::option_flags_merge().
This function parses the specified name and optional parameters and create a corresponding validator for this option.
The name_and_params
string can be defined as one of:
The list of parameters is optional. There may be no, just one, or any number of parameters. How the parameters are parsed is left to the validator to decide.
If the input string is empty, the current validator, if one is installed, gets removed.
[in] | name_and_params | The validator name and parameters. |
Definition at line 835 of file option_info.cpp.
References advgetopt::validator::create(), advgetopt::option_flags_merge(), and set_validator().
Referenced by set_validator().
bool advgetopt::option_info::set_validator | ( | validator::pointer_t | validator | ) |
Options may be assigned a validator. Without a validator, any value is considered valid.
A value is checked when you call the validates() function. The function returns true if the value is considered valid. False in all other cases.
You can define your own validators and add them to the library list of available validators before using the library in order to get your options to use said validators.
[in] | validator | A pointer to a validator. |
Definition at line 866 of file option_info.cpp.
References f_validator, f_value, advgetopt::option_flags_merge(), size(), validate_all_values(), and value_changed().
bool advgetopt::option_info::set_value | ( | int | idx, |
std::string const & | value, | ||
string_list_t const & | option_keys = string_list_t() , |
||
option_source_t | source = option_source_t::SOURCE_DIRECT |
||
) |
This function is generally used to replace an existing value. If the index is set to the size of the existing set of values, then a new value is saved in the vector.
This is particularly useful if you want to edit a configuration file.
If the option comes with a validator, then the value gets checked against that validator. If that results in an error, the value is not added to the vector so an invalid value will never be returned by the option_info class.
The value does not get added when it currently is locked or when it does not validate as per the validator of this option_info.
getopt_exception_undefined | If the index is out of range (too large or negative), then this exception is raised. |
[in] | idx | The position of the value to update. |
[in] | value | The new value. |
[in] | option_keys | An array of keys to prepend to each value. |
[in] | source | Where the value comes from. |
Definition at line 1223 of file option_info.cpp.
References f_double, f_integer, f_name, f_source, f_value, find_value_index_by_key(), advgetopt::GETOPT_FLAG_DYNAMIC_CONFIGURATION, advgetopt::GETOPT_FLAG_LOCK, advgetopt::GETOPT_FLAG_MULTIPLE, has_flag(), advgetopt::option_flags_merge(), source(), advgetopt::SOURCE_DIRECT, advgetopt::SOURCE_UNDEFINED, validates(), and value_changed().
Referenced by add_value().
void advgetopt::option_info::set_variables | ( | variables::pointer_t | vars | ) |
The getopt object holds a set of variables which is can pass down to the option info. If defined, then the get_value() function returns a processed value (a.k.a. the ${...}
references in that value are replaced by their corresponding value).
[in] | vars | A pointer to a list of variables. |
Definition at line 1110 of file option_info.cpp.
References f_variables, and advgetopt::option_flags_merge().
size_t advgetopt::option_info::size | ( | ) | const |
This function returns the number of values that were found for this option.
If the option is marked as GETOPT_FLAG_MULTIPLE, then this function may return 0 or more. Without that flag, this function only returns 0 or 1.
You must use the size() parameter to know how many items are defined and call the get_value() with a correct idx
parameter (i.e. a value between 0 and size() - 1
.)
Definition at line 1631 of file option_info.cpp.
References f_value.
Referenced by set_validator().
option_source_t advgetopt::option_info::source | ( | ) | const |
This function returns the source of this option, i.e. whether it came from the command line, the environment variable, a configuration file, or some other source that you can define.
The source is similar to a priority in the sense that a source with a higher number cannot overwrite the value of a smaller source. The source is set at the same time as you set the option. The mechanism may not be working exactly as expected when trying to add options from different sources.
Definition at line 1560 of file option_info.cpp.
References f_source.
Referenced by add_value(), set_multiple_values(), and set_value().
The getopt class supports a flag which turns on the trace mode. This allows it to memorize where the values came fram. This includes the source and if the source is a configuration file, the path to that configuration file.
Definition at line 2064 of file option_info.cpp.
References f_name, f_source, f_trace_sources, f_value, advgetopt::GETOPT_FLAG_MULTIPLE, has_flag(), advgetopt::option_flags_merge(), advgetopt::SOURCE_COMMAND_LINE, advgetopt::SOURCE_CONFIGURATION, advgetopt::SOURCE_DIRECT, advgetopt::SOURCE_DYNAMIC, advgetopt::SOURCE_ENVIRONMENT_VARIABLE, and advgetopt::SOURCE_UNDEFINED.
Referenced by value_changed().
string_list_t const & advgetopt::option_info::trace_sources | ( | ) | const |
An option can be marked for tracing. This allows you to see exactly which value came from which source. We currently support multiple sources such as the command line, environment variable, direct, dynamic, configuration files.
Definition at line 1598 of file option_info.cpp.
References f_trace_sources.
void advgetopt::option_info::unlock | ( | ) |
This function does the opposite of the lock() function. It allows for the value to be updated again.
Once the getpot object is done parsing all the input, it unlocks all the values using this function. The unlock is always unconditional.
Definition at line 1946 of file option_info.cpp.
References advgetopt::GETOPT_FLAG_LOCK, and remove_flag().
|
private |
Whenever you change the validator of an option_info, or change all the values with set_multiple_value(), all the values get verified using this function. The function removes any value which does not validate according to the current validator.
--order
instead of --order asc
) the value is set to the empty string unless there is a default. This allows you to know that the option was used without a value, which is useful for some options.Definition at line 1492 of file option_info.cpp.
References f_validator, f_value, advgetopt::option_flags_merge(), and validates().
Referenced by set_multiple_values(), and set_validator().
This function us used internally to verify values that get added at the time they get added. It runs the validator::validate() function and returns true if the value is considered valid. When the value does not validate, it returns false and removes the value from the f_value vector. This means no invalid values are ever kept in an option_info object.
An option without a validator has values that are always valid. Also, an empty value is always considered valid.
[in] | idx | The value to check. |
Definition at line 924 of file option_info.cpp.
References f_name, f_source, f_validator, f_value, advgetopt::option_flags_merge(), and advgetopt::SOURCE_UNDEFINED.
Referenced by set_value(), and validate_all_values().
This function is called on a change of the internal values.
The function is used to call the callbacks that were added to this option_info object. The function first copies the existing list of callbacks so you can safely update the list from within a callback.
[in] | idx | This represents the index of the value that last changed (currently poor attempt to fix this issue). |
Definition at line 2037 of file option_info.cpp.
References f_callbacks, advgetopt::get_global_mutex(), lock(), advgetopt::option_flags_merge(), and trace_source().
Referenced by reset(), set_multiple_values(), set_validator(), and set_value().
Definition at line 184 of file option_info.h.
Referenced by get_alias_destination(), and set_alias_destination().
|
private |
Definition at line 186 of file option_info.h.
Referenced by add_callback(), remove_callback(), and value_changed().
|
private |
Definition at line 181 of file option_info.h.
Referenced by get_default(), remove_default(), and set_default().
Definition at line 196 of file option_info.h.
Referenced by get_double(), reset(), set_multiple_values(), and set_value().
|
private |
Definition at line 179 of file option_info.h.
Referenced by get_environment_variable_name(), get_environment_variable_value(), and set_environment_variable_name().
|
private |
Definition at line 180 of file option_info.h.
Referenced by add_flag(), get_flags(), has_flag(), remove_flag(), and set_flags().
|
private |
Definition at line 182 of file option_info.h.
Referenced by get_help(), and set_help().
Definition at line 195 of file option_info.h.
Referenced by get_long(), reset(), set_multiple_values(), and set_value().
|
private |
Definition at line 185 of file option_info.h.
Referenced by get_multiple_separators(), set_multiple_separators(), set_multiple_separators(), and set_multiple_values().
|
private |
Definition at line 177 of file option_info.h.
Referenced by option_info(), find_value_index_by_key(), get_basename(), get_double(), get_long(), get_name(), get_section_name(), get_section_name_list(), get_value(), is_default_option(), set_multiple_values(), set_value(), trace_source(), and validates().
|
private |
Definition at line 187 of file option_info.h.
Referenced by add_callback().
|
private |
Definition at line 178 of file option_info.h.
Referenced by get_short_name(), and set_short_name().
|
private |
Definition at line 193 of file option_info.h.
Referenced by reset(), set_multiple_values(), set_value(), source(), trace_source(), and validates().
|
private |
Definition at line 188 of file option_info.h.
Referenced by trace_source(), and trace_sources().
|
private |
Definition at line 183 of file option_info.h.
Referenced by get_validator(), set_validator(), set_validator(), validate_all_values(), and validates().
|
private |
Definition at line 194 of file option_info.h.
Referenced by add_value(), find_value_index_by_key(), get_double(), get_long(), get_value(), has_value(), is_defined(), reset(), set_multiple_values(), set_validator(), set_value(), size(), trace_source(), validate_all_values(), and validates().
|
private |
Definition at line 189 of file option_info.h.
Referenced by get_value(), get_variables(), and set_variables().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.