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.");
132 o->set_help(opts->
f_help);
163 if(
get_option(opt->get_name(),
true) !=
nullptr)
165 if(ignore_duplicates)
169 throw getopt_defined_twice(
170 std::string(
"option named \"")
172 +
"\" found twice.");
178 if(ignore_duplicates)
185 throw getopt_defined_twice(
186 "option with short name \""
188 +
"\" found twice.");
192 if(opt->is_default_option())
196 throw getopt_logic_error(
"two default options found.");
200 throw getopt_logic_error(
"a default option must accept parameters, it can't be a GETOPT_FLAG_FLAG.");
219 char const *
const options_files_directory(getenv(
"ADVGETOPT_OPTIONS_FILES_DIRECTORY"));
220 if(options_files_directory !=
nullptr
221 && *options_files_directory !=
'\0')
225 path = options_files_directory;
238 path =
"/usr/share/advgetopt/options/";
241 if(path.back() !=
'/')
314 result.push_back(path + filename +
".ini");
318 std::string pattern(path + filename +
"-*.ini");
319 snapdev::glob_to_list<std::list<std::string>> list;
320 if(list.read_path<snapdev::glob_to_list_flag_t::GLOB_FLAG_IGNORE_ERRORS>(pattern))
322 for(
auto const & l : list)
359 for(
auto const & l : list)
415 std::string
const & filename
418 ,
bool ignore_duplicates)
427 && max_sections == 1)
451 for(
auto & section_names : sections)
455 std::string option_name;
457 && *names.begin() == section_to_ignore)
459 names.erase(names.begin());
460 option_name = snapdev::join_strings(names,
"::");
464 option_name = section_names;
467 if(names.size() <
static_cast<std::size_t
>(min_sections)
468 || names.size() >
static_cast<std::size_t
>(max_sections))
471 && max_sections == 1)
477 cppthread::log << cppthread::log_level_t::error
478 <<
"the name of a settings definition must include one namespace; \""
480 <<
"\" is not considered valid."
485 cppthread::log << cppthread::log_level_t::error
486 <<
"the name of a settings definition must include between "
492 <<
"\" is not considered valid."
498 std::string
const parameter_name(option_name);
499 std::string
const short_name(
unquote(conf->get_parameter(parameter_name +
"::shortname")));
500 if(short_name.length() > 1)
502 throw getopt_logic_error(
505 +
"\" has an invalid short name in \""
507 +
"\", it can't be more than one character.");
516 std::string
const environment_variable_name(parameter_name +
"::environment_variable_name");
517 if(conf->has_parameter(environment_variable_name))
519 opt->set_environment_variable_name(
unquote(conf->get_parameter(environment_variable_name)));
522 std::string
const default_name(parameter_name +
"::default");
523 if(conf->has_parameter(default_name))
525 opt->set_default(
unquote(conf->get_parameter(default_name)));
528 opt->set_help(
unquote(conf->get_parameter(parameter_name +
"::help")));
530 std::string
const validator_name_and_params(conf->get_parameter(parameter_name +
"::validator"));
531 opt->set_validator(validator_name_and_params);
533 std::string
const alias_name(parameter_name +
"::alias");
534 if(conf->has_parameter(alias_name))
536 if(!opt->get_help().empty())
538 throw getopt_logic_error(
541 +
"\" is an alias and as such it can't include a help=... parameter in \""
545 opt->set_help(
unquote(conf->get_parameter(alias_name)));
549 std::string
const allowed_name(parameter_name +
"::allowed");
550 if(conf->has_parameter(allowed_name))
552 std::string
const allowed_list(conf->get_parameter(allowed_name));
555 for(
auto const & a : allowed)
557 if(a ==
"command-line")
561 else if(a ==
"environment-variable")
565 else if(a ==
"configuration-file")
569 else if(a ==
"dynamic-configuration")
576 std::string
const group_name(parameter_name +
"::group");
577 if(conf->has_parameter(group_name))
579 std::string
const group(conf->get_parameter(group_name));
580 if(group ==
"commands")
584 else if(group ==
"options")
588 else if(group ==
"three")
592 else if(group ==
"four")
596 else if(group ==
"five")
600 else if(group ==
"six")
604 else if(group ==
"seven")
610 if(conf->has_parameter(parameter_name +
"::show-usage-on-error"))
615 if(conf->has_parameter(parameter_name +
"::no-arguments"))
620 if(conf->has_parameter(parameter_name +
"::multiple"))
625 if(conf->has_parameter(parameter_name +
"::required"))
649 std::string
const & alias_name(c.second->get_help());
650 if(alias_name.empty())
652 throw getopt_logic_error(
653 "the default value of your alias cannot be an empty string for \""
664 throw getopt_logic_error(
667 +
"\" to satisfy the alias of \""
673 if(alias->get_flags() != expected_flags)
675 std::stringstream ss;
677 <<
"the flags of alias \""
681 <<
") are different than the flags of \""
684 << alias->get_flags()
686 throw getopt_logic_error(ss.str());
689 c.second->set_alias_destination(alias);
731 throw getopt_logic_error(
732 "option with name \""
742 if(it->second == opt->second)
749 throw getopt_logic_error(
750 "found another option (\""
751 + it->second->get_name()
752 +
"\") with short name '"
758 short_name_t const old_short_name(opt->second->get_short_name());
768 opt->second->set_short_name(short_name);
791 out <<
"Option Sources:\n";
794 out <<
" " << idx <<
". option \"" << opt.second->get_name() <<
"\"";
798 out <<
" (undefined)\n";
803 for(
auto const & src : sources)
805 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_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