39#include <cppthread/guard.h>
40#include <cppthread/log.h>
41#include <cppthread/mutex.h>
46#include <snapdev/not_used.h>
47#include <snapdev/tokenize_string.h>
52#include <libutf8/libutf8.h>
53#include <libutf8/iterator.h>
58#include <snapdev/poison.h>
132 libutf8::utf8_iterator
u8(name);
166 return std::string();
233 "option_info::option_info(): all options must at least have a long name (short name: '"
238 "option_info::option_info(): all options must at least have a long name.");
246 "option_info::option_info(): the default parameter \"--\" cannot include a short name ('"
258 "option_info::option_info(): an option cannot start with a dash (-), \""
260 +
"\" is not valid.");
266 "option_info::option_info(): the short name of an option cannot be the dash (-).");
346 std::string::size_type
const pos(
f_name.rfind(
"::"));
347 if(
pos == std::string::npos)
373 std::string::size_type
const pos(
f_name.rfind(
"::"));
374 if(
pos == std::string::npos)
376 return std::string();
400 std::string::size_type
const pos(
f_name.rfind(
"::"));
401 if(
pos == std::string::npos)
412 , &snapdev::string_predicate<string_list_t>);
514 ,
char const *
intro)
const
926 if(
static_cast<size_t>(
idx) >=
f_value.size())
929 "option_info::validates(): no value at index "
930 + std::to_string(
idx)
932 + std::to_string(
f_value.size())
935 +
" so you can't get this value.");
950 cppthread::log << cppthread::log_level_t::error
953 <<
"\" given to parameter --"
955 <<
" is not considered valid."
1006 "option_info::set_alias(): you can't set an alias as"
1007 " an alias of another option.");
1177 std::string
const & value
1225 , std::string
const & value
1232 "option_info::set_value(): called with SOURCE_UNDEFINED ("
1233 + std::to_string(
static_cast<int>(
source))
1245 cppthread::log << cppthread::log_level_t::error
1248 <<
"\" can't be directly updated."
1256 if(
static_cast<size_t>(
idx) >
f_value.size())
1259 "option_info::set_value(): no value at index "
1260 + std::to_string(
idx)
1261 +
" and it is not the last available index + 1 (idx > "
1262 + std::to_string(
f_value.size())
1263 +
") so you can't set this value (try add_value() maybe?).");
1271 "option_info::set_value(): single value option \"--"
1273 +
"\" does not accepts index "
1274 + std::to_string(
idx)
1275 +
" which is not 0.");
1286 if(
static_cast<size_t>(
idx) ==
f_value.size())
1321 std::string
const v(
k + value);
1405 std::string
const & value
1413 "option_info::set_multiple_value(): parameter --"
1415 +
" does not support array keys.");
1421 "option_info::set_multiple_values(): called with SOURCE_UNDEFINED ("
1422 + std::to_string(
static_cast<int>(
source))
1433 "option_info::set_multiple_value(): parameter --"
1435 +
" expects zero or one parameter. The set_multiple_value() function should not be called with parameters that only accept one value.");
1584 g_trace_sources =
trace;
1612 g_configuration_filename =
filename;
1663 if(
static_cast<size_t>(
idx) >=
f_value.size())
1666 "option_info::get_value(): no value at index "
1667 + std::to_string(
idx)
1669 + std::to_string(
f_value.size())
1672 +
" so you can't get this value.");
1724 "option_info::find_value_index_by_key(): --"
1726 +
" has no values defined.");
1729 if(
key.back() !=
':')
1772 if(
static_cast<size_t>(
idx) >=
f_value.size())
1775 "option_info::get_long(): no value at index "
1776 + std::to_string(
idx)
1778 + std::to_string(
f_value.size())
1781 +
" so you can't get this value.");
1802 cppthread::log << cppthread::log_level_t::error
1803 <<
"invalid number ("
1805 <<
") in parameter --"
1847 if(
static_cast<size_t>(
idx) >=
f_value.size())
1850 "option_info::get_double(): no value at index "
1851 + std::to_string(
idx)
1853 + std::to_string(
f_value.size())
1856 +
" so you can't get this value.");
1877 cppthread::log << cppthread::log_level_t::error
1878 <<
"invalid number ("
1880 <<
") in parameter --"
2008 auto it(std::find_if(
2013 return e.f_id == id;
2051 e.f_callback(*
this);
2066 if(!g_trace_sources)
2079 s =
"configuration=\"" + g_configuration_filename +
"\"";
2091 s =
"environment-variable";
2114 ||
static_cast<std::size_t
>(
idx) >=
f_value.size())
string_list_t get_section_name_list() const
Retrieve a list of section names.
void set_flags(flag_t flags)
Get the flags.
bool validates(int idx=0)
Check a value validity.
void unlock()
Unlock this value.
void add_flag(flag_t flag)
Make sure a given flag is set.
std::string get_environment_variable_name() const
Retrieve the environment variable name of this option.
bool is_default_option() const
Check whether this is the default option.
variables::pointer_t get_variables() const
Retrieve the list of variables held by this option info.
void remove_default()
Remove the default value.
std::vector< long > f_integer
flag_t get_flags() const
Retrieve the flags.
std::string get_value(int idx=0, bool raw=false) const
Retrieve the value.
long get_long(int idx=0) const
Get the value as a long.
bool has_value(std::string const &value) const
Check whether one of the values matches the input.
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.
validator::pointer_t f_validator
std::string f_default_value
callback_vector_t f_callbacks
bool is_defined() const
Check whether a value is defined.
string_list_t const & trace_sources() const
Get the trace of this option.
callback_id_t add_callback(callback_t const &c)
Add a callback to call on a change to this value.
std::vector< callback_entry_t > callback_vector_t
void lock(bool always=true)
Lock this value.
size_t size() const
Retrieve the number of values defined for this option.
pointer_t get_alias_destination() const
Get a link to the destination alias.
void set_environment_variable_name(std::string const &name)
Set the option specific environment variable name.
std::string const & get_default() const
Retrieve the default value.
void value_changed(int idx)
Call whenever the value changed so we can handle callbacks.
std::shared_ptr< option_info > pointer_t
string_list_t f_trace_sources
void set_short_name(short_name_t short_name)
Assign a short name to an option.
void reset()
Reset this value.
void trace_source(int idx)
Remember the source information at of this last change.
pointer_t f_alias_destination
void set_help(std::string const &help)
Set the help string for this option.
bool has_default() const
Check whether this option has a default value.
bool get_environment_variable_value(std::string &value, char const *intro=nullptr) const
Retrieve the environment variable value of this option.
std::string get_section_name() const
Retrieve the name of the sections.
bool validate_all_values()
Validate all the values of this option_info object.
static void set_trace_sources(bool trace)
Whether the sources should be traced.
variables::pointer_t f_variables
void set_variables(variables::pointer_t vars)
Assign variables to this option info.
option_info(std::string const &name, short_name_t short_name=NO_SHORT_NAME)
Create a new option_info object.
std::function< void(option_info const &opt)> callback_t
static void set_configuration_filename(std::string const &filename)
Save the filename of the current configuration file.
string_list_t const & get_multiple_separators() const
Retrieve the list of separators for this argument.
std::string get_basename() const
Retrieve the name of the option without any section names.
bool set_validator(std::string const &name_and_params)
Set the validator for this option.
option_source_t source() const
Return the source of this option info.
std::string const & get_name() const
Get the long name of the 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.
void set_default(std::string const &default_value)
Set the default value.
short_name_t get_short_name() const
Get the short name of the 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.
double get_double(int idx=0) const
Get the value as a double.
std::string f_environment_variable_name
bool has_flag(flag_t flag) const
Check whether a flag is set.
void remove_callback(callback_id_t id)
Remove a callback.
std::string const & get_help() const
Get the help string.
string_list_t f_multiple_separators
short_name_t f_short_name
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.
void set_alias_destination(pointer_t destination)
Set the alias option.
validator::pointer_t get_validator() const
Retrieve a pointer to the validator.
void remove_flag(flag_t flag)
Make sure a given flag is not set.
void set_multiple_separators(string_list_t const &separators)
Set the list of separators.
std::vector< double > f_double
static bool convert_string(std::string const &number, double &result)
Convert a string to a double value.
static bool convert_string(std::string const &number, std::int64_t &result)
Convert a string to an std::int64_t value.
static pointer_t create(std::string const &name, string_list_t const &data)
std::shared_ptr< validator > pointer_t
std::shared_ptr< variables > pointer_t
Definitions of the advanced getopt exceptions.
bool g_trace_sources
The library trace mode.
std::string g_configuration_filename
The filename of the configuration being processed.
The advgetopt environment to parse command line options.
static constexpr flag_t GETOPT_FLAG_LOCK
void split_string(std::string const &str, string_list_t &result, string_list_t const &separators)
Split a string in sub-strings separated by separators.
static constexpr flag_t GETOPT_FLAG_ARRAY
@ SOURCE_ENVIRONMENT_VARIABLE
constexpr flag_t option_flags_merge()
static constexpr flag_t GETOPT_FLAG_DYNAMIC_CONFIGURATION
static constexpr flag_t GETOPT_FLAG_PROCESS_VARIABLES
short_name_t string_to_short_name(std::string const &name)
Transform a string to a short name.
constexpr short_name_t NO_SHORT_NAME
cppthread::mutex & get_global_mutex()
Get a global mutex.
static constexpr flag_t GETOPT_FLAG_DEFAULT_OPTION
std::string short_name_to_string(short_name_t short_name)
Convert a short name to a UTF-8 string.
static constexpr flag_t GETOPT_FLAG_HAS_DEFAULT
static constexpr flag_t GETOPT_FLAG_MULTIPLE
std::string unquote(std::string const &s, std::string const &pairs)
Remove single (') or double (") quotes from a string.
std::string option_with_dashes(std::string const &s)
Convert the _ found in a string to - instead.
std::vector< std::string > string_list_t
static constexpr flag_t GETOPT_FLAG_ALIAS
Declaration of the option_info class used to record available options.
Declaration of validators which can be used to verify the parameters.
Declaration of validators which can be used to verify the parameters.