44#include <cppthread/log.h>
49#include <snapdev/glob_to_list.h>
50#include <snapdev/join_strings.h>
51#include <snapdev/tokenize_string.h>
61#include <snapdev/poison.h>
111 if(opts->
f_name ==
nullptr
112 || opts->
f_name[0] ==
'\0')
114 throw getopt_logic_error(
"option long name missing or empty.");
119 throw getopt_logic_error(
"a long name option must be at least 2 characters.");
124 char const *
const n(strrchr(opts->
f_name,
':'));
142 std::string
const basename(n + 1);
151 o->set_help(opts->
f_help);
171 alias->set_help(basename);
191 o->set_help(opts->
f_help);
223 if(
get_option(opt->get_name(),
true) !=
nullptr)
225 if(ignore_duplicates)
229 throw getopt_defined_twice(
230 std::string(
"option named \"")
232 +
"\" found twice.");
238 if(ignore_duplicates)
245 throw getopt_defined_twice(
246 "option with short name \""
248 +
"\" found twice.");
252 if(opt->is_default_option())
256 throw getopt_logic_error(
"two default options found.");
260 throw getopt_logic_error(
"a default option must accept parameters, it can't be a GETOPT_FLAG_FLAG.");
279 char const *
const options_files_directory(getenv(
"ADVGETOPT_OPTIONS_FILES_DIRECTORY"));
280 if(options_files_directory !=
nullptr
281 && *options_files_directory !=
'\0')
285 path = options_files_directory;
298 path =
"/usr/share/advgetopt/options/";
301 if(path.back() !=
'/')
374 result.push_back(path + filename +
".ini");
378 std::string pattern(path + filename +
"-*.ini");
379 snapdev::glob_to_list<std::list<std::string>> list;
380 if(list.read_path<snapdev::glob_to_list_flag_t::GLOB_FLAG_IGNORE_ERRORS>(pattern))
382 for(
auto const & l : list)
419 for(
auto const & l : list)
476 std::string
const & filename
477 ,
int const min_sections
478 ,
int const max_sections
479 ,
bool ignore_duplicates
480 ,
bool keep_all_sections)
489 && max_sections == 1)
527 for(
auto & section_names : sections)
531 std::string option_name;
534 && *names.begin() == section_to_ignore)
536 names.erase(names.begin());
537 option_name = snapdev::join_strings(names,
"::");
541 option_name = section_names;
544 if(names.size() <
static_cast<std::size_t
>(min_sections)
545 || names.size() >
static_cast<std::size_t
>(max_sections))
548 && max_sections == 1)
554 cppthread::log << cppthread::log_level_t::error
556 <<
": the name of a settings definition must include one namespace; \""
558 <<
"\" is not considered valid."
563 cppthread::log << cppthread::log_level_t::error
565 <<
": the name of a settings definition must include between "
571 <<
"\" is not considered valid."
577 std::string
const parameter_name(option_name);
578 std::string
const short_name(
unquote(conf->get_parameter(parameter_name +
"::shortname")));
581 && !short_name.empty())
583 throw getopt_logic_error(
586 +
"\" has an invalid short name \""
590 +
"\", it can't be more than one character.");
596 std::string
const environment_variable_name(parameter_name +
"::environment_variable_name");
597 if(conf->has_parameter(environment_variable_name))
599 opt->set_environment_variable_name(
unquote(conf->get_parameter(environment_variable_name)));
602 std::string
const default_name(parameter_name +
"::default");
603 if(conf->has_parameter(default_name))
605 opt->set_default(
unquote(conf->get_parameter(default_name)));
608 opt->set_help(
unquote(conf->get_parameter(parameter_name +
"::help")));
610 std::string
const validator_name_and_params(conf->get_parameter(parameter_name +
"::validator"));
611 opt->set_validator(validator_name_and_params);
613 std::string
const alias_name(parameter_name +
"::alias");
614 if(conf->has_parameter(alias_name))
616 if(!opt->get_help().empty())
618 throw getopt_logic_error(
621 +
"\" is an alias and as such it can't include a help=... parameter in \""
625 opt->set_help(
unquote(conf->get_parameter(alias_name)));
629 std::string
const allowed_name(parameter_name +
"::allowed");
630 if(conf->has_parameter(allowed_name))
632 std::string
const allowed_list(conf->get_parameter(allowed_name));
635 for(
auto const & a : allowed)
637 if(a ==
"command-line")
641 else if(a ==
"environment-variable")
645 else if(a ==
"configuration-file")
649 else if(a ==
"dynamic-configuration")
656 std::string
const group_name(parameter_name +
"::group");
657 if(conf->has_parameter(group_name))
659 std::string
const group(conf->get_parameter(group_name));
660 if(group ==
"commands")
664 else if(group ==
"options")
668 else if(group ==
"three")
672 else if(group ==
"four")
676 else if(group ==
"five")
680 else if(group ==
"six")
684 else if(group ==
"seven")
690 if(conf->has_parameter(parameter_name +
"::show-usage-on-error"))
695 if(conf->has_parameter(parameter_name +
"::no-arguments"))
700 if(conf->has_parameter(parameter_name +
"::multiple"))
705 if(conf->has_parameter(parameter_name +
"::required"))
729 std::string
const & alias_name(c.second->get_help());
730 if(alias_name.empty())
732 throw getopt_logic_error(
733 "the default value of your alias cannot be an empty string for \""
744 throw getopt_logic_error(
747 +
"\" to satisfy the alias of \""
753 if(alias->get_flags() != expected_flags)
755 std::stringstream ss;
757 <<
"the flags of alias \""
761 <<
") are different than the flags of \""
764 << alias->get_flags()
766 throw getopt_logic_error(ss.str());
769 c.second->set_alias_destination(alias);
811 throw getopt_logic_error(
812 "option with name \""
822 if(it->second == opt->second)
829 throw getopt_logic_error(
830 "found another option (\""
831 + it->second->get_name()
832 +
"\") with short name '"
838 short_name_t const old_short_name(opt->second->get_short_name());
848 opt->second->set_short_name(short_name);
871 out <<
"Option Sources:\n";
874 out <<
" " << idx <<
". option \"" << opt.second->get_name() <<
"\"";
878 out <<
" (undefined)\n";
883 for(
auto const & src : sources)
885 out <<
" " << src <<
"\n";
Definitions of the advanced getopt class.
bool is_valid() const
Check whether the setup is considered valid.
void set_section_to_ignore(std::string const §ion_name)
Set a section name to ignore.
std::shared_ptr< conf_file > pointer_t
static pointer_t get_conf_file(conf_file_setup const &setup)
Create and read a conf_file.
std::string get_group_or_project_name() const
Retrieve the group or project name.
options_environment f_options_environment
option_info::map_by_name_t f_options_by_name
option_info::pointer_t get_option(std::string const &name, bool exact_option=false) const
Retrieve an option by name.
void add_option(option_info::pointer_t opt, bool ignore_duplicates=false)
Add one option to the advgetopt object.
option_info::map_by_short_name_t f_options_by_short_name
std::string get_path_to_option_files() const
void parse_options_info(option const *opts, bool ignore_duplicates=false)
Parse the options to option_info objects.
string_list_t get_filenames_of_option_definitions() const
Get the path and filename to options.
void parse_options_from_file()
Check for a file with option definitions.
void reset()
Reset all the options.
void link_aliases()
Link options marked as a GETOPT_FLAG_ALIAS.
void show_option_sources(std::basic_ostream< char > &out)
Output the source of each option.
variables::pointer_t f_variables
option_info::pointer_t f_default_option
void set_short_name(std::string const &name, short_name_t short_name)
Assign a short name to an option.
std::shared_ptr< option_info > pointer_t
Declaration of the conf_file class used to read a configuration file.
Definitions of the advanced getopt exceptions.
The advgetopt environment to parse command line options.
static constexpr flag_t GETOPT_FLAG_GROUP_SEVEN
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.
constexpr section_operator_t SECTION_OPERATOR_INI_FILE
static constexpr flag_t GETOPT_FLAG_REMOVE_NAMESPACE
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
std::uint_fast16_t section_operator_t
static constexpr flag_t GETOPT_FLAG_SHOW_USAGE_ON_ERROR
static constexpr flag_t GETOPT_FLAG_END
constexpr section_operator_t SECTION_OPERATOR_ONE_SECTION
constexpr comment_t COMMENT_SHELL
static constexpr flag_t GETOPT_FLAG_DYNAMIC_CONFIGURATION
short_name_t string_to_short_name(std::string const &name)
Transform a string to a short name.
static constexpr flag_t GETOPT_FLAG_COMMAND_LINE
static constexpr flag_t GETOPT_FLAG_GROUP_FOUR
static constexpr flag_t GETOPT_FLAG_GROUP_SIX
constexpr assignment_operator_t ASSIGNMENT_OPERATOR_EQUAL
constexpr short_name_t NO_SHORT_NAME
static constexpr flag_t GETOPT_FLAG_CONFIGURATION_FILE
static constexpr flag_t GETOPT_FLAG_FLAG
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
static constexpr flag_t GETOPT_FLAG_GROUP_FIVE
std::string short_name_to_string(short_name_t short_name)
Convert a short name to a UTF-8 string.
constexpr comment_t COMMENT_INI
static constexpr flag_t GETOPT_FLAG_MULTIPLE
static constexpr flag_t GETOPT_FLAG_GROUP_THREE
std::string unquote(std::string const &s, std::string const &pairs)
Remove single (') or double (") quotes from a string.
static constexpr flag_t GETOPT_FLAG_REQUIRED
static constexpr flag_t GETOPT_FLAG_ENVIRONMENT_VARIABLE
std::vector< std::string > string_list_t
static constexpr flag_t GETOPT_FLAG_ALIAS
Structure representing an option.
char const *const * f_multiple_separators
short_name_t f_short_name
char const * f_environment_variable_name
char const * f_options_files_directory