44#include <cppthread/log.h>
49#include <snapdev/join_strings.h>
50#include <snapdev/tokenize_string.h>
60#include <snapdev/poison.h>
110 if(opts->
f_name ==
nullptr
111 || opts->
f_name[0] ==
'\0')
113 throw getopt_logic_error(
"option long name missing or empty.");
118 throw getopt_logic_error(
"a long name option must be at least 2 characters.");
131 o->set_help(opts->
f_help);
162 if(
get_option(opt->get_name(),
true) !=
nullptr)
164 if(ignore_duplicates)
168 throw getopt_defined_twice(
169 std::string(
"option named \"")
171 +
"\" found twice.");
177 if(ignore_duplicates)
184 throw getopt_defined_twice(
185 "option with short name \""
187 +
"\" found twice.");
191 if(opt->is_default_option())
195 throw getopt_logic_error(
"two default options found.");
199 throw getopt_logic_error(
"a default option must accept parameters, it can't be a GETOPT_FLAG_FLAG.");
237 return std::string();
241 char const *
const options_files_directory(getenv(
"ADVGETOPT_OPTIONS_FILES_DIRECTORY"));
242 if(options_files_directory !=
nullptr
243 && *options_files_directory !=
'\0')
245 path = options_files_directory;
254 path =
"/usr/share/advgetopt/options/";
256 if(path.back() !=
'/')
261 return path + filename +
".ini";
333 std::string
const & filename
336 ,
bool ignore_duplicates)
345 && max_sections == 1)
369 for(
auto & section_names : sections)
373 std::string option_name;
375 && *names.begin() == section_to_ignore)
377 names.erase(names.begin());
378 option_name = snapdev::join_strings(names,
"::");
382 option_name = section_names;
385 if(names.size() <
static_cast<std::size_t
>(min_sections)
386 || names.size() >
static_cast<std::size_t
>(max_sections))
389 && max_sections == 1)
395 cppthread::log << cppthread::log_level_t::error
396 <<
"the name of a settings definition must include one namespace; \""
398 <<
"\" is not considered valid."
403 cppthread::log << cppthread::log_level_t::error
404 <<
"the name of a settings definition must include between "
410 <<
"\" is not considered valid."
416 std::string
const parameter_name(option_name);
417 std::string
const short_name(
unquote(conf->get_parameter(parameter_name +
"::shortname")));
418 if(short_name.length() > 1)
420 throw getopt_logic_error(
423 +
"\" has an invalid short name in \""
425 +
"\", it can't be more than one character.");
434 std::string
const environment_variable_name(parameter_name +
"::environment_variable_name");
435 if(conf->has_parameter(environment_variable_name))
437 opt->set_environment_variable_name(
unquote(conf->get_parameter(environment_variable_name)));
440 std::string
const default_name(parameter_name +
"::default");
441 if(conf->has_parameter(default_name))
443 opt->set_default(
unquote(conf->get_parameter(default_name)));
446 opt->set_help(
unquote(conf->get_parameter(parameter_name +
"::help")));
448 std::string
const validator_name_and_params(conf->get_parameter(parameter_name +
"::validator"));
449 opt->set_validator(validator_name_and_params);
451 std::string
const alias_name(parameter_name +
"::alias");
452 if(conf->has_parameter(alias_name))
454 if(!opt->get_help().empty())
456 throw getopt_logic_error(
459 +
"\" is an alias and as such it can't include a help=... parameter in \""
463 opt->set_help(
unquote(conf->get_parameter(alias_name)));
467 std::string
const allowed_name(parameter_name +
"::allowed");
468 if(conf->has_parameter(allowed_name))
470 std::string
const allowed_list(conf->get_parameter(allowed_name));
473 for(
auto const & a : allowed)
475 if(a ==
"command-line")
479 else if(a ==
"environment-variable")
483 else if(a ==
"configuration-file")
487 else if(a ==
"dynamic-configuration")
494 std::string
const group_name(parameter_name +
"::group");
495 if(conf->has_parameter(group_name))
497 std::string
const group(conf->get_parameter(group_name));
498 if(group ==
"commands")
502 else if(group ==
"options")
506 else if(group ==
"three")
510 else if(group ==
"four")
514 else if(group ==
"five")
518 else if(group ==
"six")
522 else if(group ==
"seven")
528 if(conf->has_parameter(parameter_name +
"::show-usage-on-error"))
533 if(conf->has_parameter(parameter_name +
"::no-arguments"))
538 if(conf->has_parameter(parameter_name +
"::multiple"))
543 if(conf->has_parameter(parameter_name +
"::required"))
567 std::string
const & alias_name(c.second->get_help());
568 if(alias_name.empty())
570 throw getopt_logic_error(
571 "the default value of your alias cannot be an empty string for \""
582 throw getopt_logic_error(
585 +
"\" to satisfy the alias of \""
591 if(alias->get_flags() != expected_flags)
593 std::stringstream ss;
595 <<
"the flags of alias \""
599 <<
") are different than the flags of \""
602 << alias->get_flags()
604 throw getopt_logic_error(ss.str());
607 c.second->set_alias_destination(alias);
649 throw getopt_logic_error(
650 "option with name \""
660 if(it->second == opt->second)
667 throw getopt_logic_error(
668 "found another option (\""
669 + it->second->get_name()
670 +
"\") with short name '"
676 short_name_t const old_short_name(opt->second->get_short_name());
686 opt->second->set_short_name(short_name);
709 out <<
"Option Sources:\n";
712 out <<
" " << idx <<
". option \"" << opt.second->get_name() <<
"\"";
716 out <<
" (undefined)\n";
721 for(
auto const & src : sources)
723 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_options_filename() const
Get the path and filename to options.
void parse_options_info(option const *opts, bool ignore_duplicates=false)
Parse the options to option_info objects.
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