93#include    <snapdev/join_strings.h> 
   94#include    <snapdev/not_reached.h> 
   99#include    <cppthread/log.h> 
  104#include    <libutf8/iterator.h> 
  114#include    <snapdev/poison.h> 
  204        , 
Help(
"print out the time and date when %p was built and exit.")
 
  207          Name(
"compiler-version")
 
  211        , 
Help(
"print the version of the compiler used to compile the advgetopt library.")
 
  214          Name(
"configuration-filenames")
 
  218        , 
Help(
"print out the list of configuration files checked out by this tool.")
 
  225        , 
Help(
"print out the copyright of %p and exit.")
 
  228          Name(
"environment-variable-name")
 
  232        , 
Help(
"print out the name of the environment variable supported by %p (if any.)")
 
  235          Name(
"has-sanitizer")
 
  239        , 
Help(
"print whether the advgetopt was compiled with the sanitizer extension.")
 
  247        , 
Help(
"print out this help screen and exit.")
 
  254        , 
Help(
"print out the license of %p and exit.")
 
  257          Name(
"path-to-option-definitions")
 
  261        , 
Help(
"print out the path to the option definitions.")
 
  269        , 
Help(
"print the value of the named option after parsing all the options from the command line, environment variables, and configuration files.")
 
  272          Name(
"show-option-sources")
 
  276        , 
Help(
"parse all the options and then print out the source of each value and each override.")
 
  283        , 
Help(
"print out the version of %p and exit.")
 
 
  311        , 
Help(
"add one or more configuration directory paths to search for configuration files.")
 
 
  332    return a[0] == 
'-' && a[1] != 
'\0';
 
 
  349    static bool found = 
false;
 
  351    if(!found && argv != 
nullptr)
 
  353        for(
int idx(1); idx < argc; ++idx)
 
  355            if(strcmp(argv[idx], 
"--show-option-sources") == 0)
 
 
 
  600    check_for_show_sources(argc, argv);
 
 
  665        throw getopt_logic_error(
"argv pointer cannot be nullptr");
 
  668    check_for_show_sources(argc, argv);
 
  673        throw getopt_logic_error(
"an empty list of options is not legal, you must defined at least one (i.e. --version, --help...)");
 
  693    if(cppthread::log.get_errors() != 0)
 
  695        throw getopt_exit(
"errors were found on your command line, environment variable, or configuration file.", 1);
 
 
  717        throw getopt_initialization(
 
  718                "function called too soon, parser is not done yet" 
  719                " (i.e. is_defined(), get_string(), get_long()," 
  720                " get_double() cannot be called until the parser is done)");
 
 
  829        std::string 
const name(opt.second->get_environment_variable_name());
 
  834        if(opt.second->get_environment_variable_value(
 
 
  874          std::string 
const & str
 
  876        , 
bool only_environment_variable)
 
  892    std::vector<char *> sub_argv;
 
  894    sub_argv.resize(args.size() + 2);
 
  902    for(
size_t idx(0); idx < args.size(); ++idx)
 
  904        sub_argv[idx + 1] = 
const_cast<char *
>(args[idx].c_str());
 
  909    sub_argv[args.size() + 1] = 
nullptr;
 
  914          static_cast<int>(sub_argv.size() - 1)
 
  917        , only_environment_variable);
 
 
  943    char const * s(environment.c_str());
 
  964            char const quote(*s++);
 
 
 1041        , 
bool only_environment_variable)
 
 1043    for(
int i(1); i < argc; ++i)
 
 1045        if(argv[i][0] == 
'-')
 
 1047            if(argv[i][1] == 
'-')
 
 1049                if(argv[i][2] == 
'\0')
 
 1056                        cppthread::log << cppthread::log_level_t::error
 
 1057                                       << 
"no default options defined; thus \"--\" is not accepted by this program." 
 1062                    if(only_environment_variable)
 
 1066                            cppthread::log << cppthread::log_level_t::error
 
 1067                                           << 
"option \"--\" is not supported in the environment variable." 
 1076                            cppthread::log << cppthread::log_level_t::error
 
 1077                                           << 
"option \"--\" is not supported on the command line." 
 1097                    std::string option_name(argv[i] + 2);
 
 1098                    std::string option_value;
 
 1099                    std::string::size_type pos(option_name.find(
'='));
 
 1100                    if(pos != std::string::npos)
 
 1104                            cppthread::log << cppthread::log_level_t::error
 
 1105                                           << 
"name missing in \"" 
 1112                        option_value = option_name.substr(pos + 1);
 
 1113                        option_name.resize(pos);
 
 1119                        pos = option_name.find_first_of(
"[:");
 
 1120                        if(pos != std::string::npos)
 
 1123                            option_name.resize(pos);
 
 1128                            cppthread::log << cppthread::log_level_t::error
 
 1131                                           << 
"\" is not supported." 
 1137                            cppthread::log << cppthread::log_level_t::error
 
 1140                                           << 
"\" does not support the array or map syntax." 
 1144                        if(option_keys.empty())
 
 1146                            cppthread::log << cppthread::log_level_t::error
 
 1149                                           << 
"\" has an invalid list of array keys." 
 1154                    if(only_environment_variable)
 
 1158                            cppthread::log << cppthread::log_level_t::error
 
 1161                                           << 
"\" is not supported in the environment variable." 
 1170                            cppthread::log << cppthread::log_level_t::error
 
 1173                                           << 
"\" is not supported on the command line." 
 1178                    if(pos != std::string::npos)
 
 1191                        add_options(opt, i, argc, argv, option_keys, source);
 
 1197                if(argv[i][1] == 
'\0')
 
 1203                        cppthread::log << cppthread::log_level_t::error
 
 1204                                       << 
"no default options defined; thus \"-\" is not accepted by this program." 
 1208                    if(only_environment_variable)
 
 1212                            cppthread::log << cppthread::log_level_t::error
 
 1213                                           << 
"option \"-\" is not supported in the environment variable." 
 1222                            cppthread::log << cppthread::log_level_t::error
 
 1223                                           << 
"option \"-\" is not supported on the command line." 
 1240                    std::string 
const short_args_string(argv[i] + 1);
 
 1241                    for(libutf8::utf8_iterator short_args(short_args_string)
 
 1242                      ; short_args != short_args_string.end()
 
 1248                            cppthread::log << cppthread::log_level_t::error
 
 1251                                           << 
"\" is not supported." 
 1255                        if(only_environment_variable)
 
 1259                                cppthread::log << cppthread::log_level_t::error
 
 1262                                               << 
"\" is not supported in the environment variable." 
 1271                                cppthread::log << cppthread::log_level_t::error
 
 1274                                               << 
"\" is not supported on the command line." 
 1290                cppthread::log << cppthread::log_level_t::error
 
 1291                               << 
"no default options defined; we do not know what to do of \"" 
 1293                               << 
"\"; standalone parameters are not accepted by this program." 
 1297            if(only_environment_variable)
 
 1301                    cppthread::log << cppthread::log_level_t::error
 
 1302                                   << 
"default options are not supported in the environment variable." 
 1311                    cppthread::log << cppthread::log_level_t::error
 
 1312                                   << 
"default options are not supported on the command line." 
 
 1394    if(raw_key[0] == 
'[')
 
 1401        std::string::size_type pos(1);
 
 1404            std::string::size_type start(pos);
 
 1405            pos = raw_key.find(
']', pos);
 
 1406            if(pos == std::string::npos)
 
 1412            std::string key(raw_key.substr(start, pos - start));
 
 1415                keys.push_back(key);
 
 1418            if(pos >= raw_key.length())
 
 1422            if(raw_key[pos] != 
'[')
 
 1440    std::sort(keys.begin(), keys.end());
 
 1441    keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
 
 
 1464        opt = opt->get_alias_destination();
 
 1467            throw getopt_undefined(
"getopt::get_alias_destination(): alias is missing. Did you call link_aliases()?");
 
 
 1527        throw getopt_invalid_parameter(
"get_option() `name` argument cannot be empty.");
 
 
 1629        opt->add_value(opt->get_default(), option_keys, source);
 
 1633        if(i + 1 < argc && !is_arg(argv[i + 1]))
 
 1640                    opt->add_value(argv[i], option_keys, source);
 
 1642                while(i + 1 < argc && !is_arg(argv[i + 1]));
 
 1647                opt->add_value(argv[i], option_keys, source);
 
 1654                cppthread::log << cppthread::log_level_t::error
 
 1657                               << 
" expects an argument." 
 1666                opt->add_value(std::string(), option_keys, source);
 
 
 1694        , std::string 
const & value
 
 1695        , std::string 
const & filename
 
 1709                opt->set_multiple_values(value, option_keys, source);
 
 1713                opt->set_value(0, value, option_keys, source);
 
 1732            opt->set_value(0, std::string(), option_keys, source);
 
 1736#pragma GCC diagnostic push 
 1737#pragma GCC diagnostic ignored "-Wrestrict" 
 1738        cppthread::log << cppthread::log_level_t::error
 
 1740                       << (filename.empty()
 
 1741                               ? 
"--" + opt->get_name()
 
 1743                       << 
" cannot be given value \"" 
 1746                       << (filename.empty()
 
 1748                           : 
" in configuration file \"" 
 1751                       << 
". It only accepts \"true\" or \"false\"." 
 1753#pragma GCC diagnostic pop 
 1761#pragma GCC diagnostic push 
 1762#pragma GCC diagnostic ignored "-Wrestrict" 
 1763        cppthread::log << cppthread::log_level_t::error
 
 1765                       << (filename.empty()
 
 1766                               ? 
"--" + opt->get_name()
 
 1768                       << 
" must be given a value" 
 1769                       << (filename.empty()
 
 1771                           : 
" in configuration file \"" 
 1776#pragma GCC diagnostic pop 
 1782    opt->set_value(0, value, option_keys, source);
 
 
Definitions of the advanced getopt class.
options_environment f_options_environment
option_info::pointer_t get_alias_destination(option_info::pointer_t opt) const
Return the alias if there is one.
void initialize_parser(options_environment const &opt_env)
Initialize the parser.
void add_option_from_string(option_info::pointer_t opt, std::string const &value, std::string const &filename, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Add an option with a value defined in a string.
void parse_configuration_files(int argc=0, char *argv[]=nullptr)
This function checks for arguments in configuration files.
void add_options(option_info::pointer_t opt, int &i, int argc, char **argv, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Read parameters of the current option.
option_info::map_by_name_t f_options_by_name
void parse_string(std::string const &str, option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse a string similar to a command line argument.
option_info::pointer_t get_option(std::string const &name, bool exact_option=false) const
Retrieve an option by name.
void parse_environment_variable()
Check for an environment variable.
option_info::map_by_short_name_t f_options_by_short_name
void define_environment_variable_data()
Retrieve the environment variable string.
std::string f_program_fullname
variables::pointer_t get_variables() const
Retrieve a pointer to the variables defined in the getopt object.
void parse_arguments(int argc, char *argv[], option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse an array of arguments.
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 parse_program_name(char *argv[])
Transform the argv[0] parameter in the program name.
getopt(options_environment const &opts)
Initialize the getopt object.
std::string f_environment_variable
void is_parsed() const
Verify that the parser is done.
options_environment const & get_options_environment() const
Return a reference to the options environment.
flag_t process_system_options(std::basic_ostream< char > &out)
Process the system options.
bool has_flag(flag_t flag) const
Check whether an environment flag is set or not.
void link_aliases()
Link options marked as a GETOPT_FLAG_ALIAS.
static string_list_t split_environment(std::string const &environment)
Transform a string in an array of arguments.
option_info::map_by_name_t const & get_options() const
Retrieve the complete list of options.
void finish_parsing(int argc, char *argv[])
Actually parse everything.
void parse_options_from_group_names()
Transform group names in –<name>-help commands.
variables::pointer_t f_variables
option_info::pointer_t f_default_option
static string_list_t parse_option_map(std::string const &raw_key)
Parse a map following an option name.
std::map< std::string, pointer_t > map_by_name_t
std::shared_ptr< option_info > pointer_t
static void set_trace_sources(bool trace)
Whether the sources should be traced.
std::shared_ptr< variables > pointer_t
Definitions of the advanced getopt exceptions.
option const g_if_configuration_filename_system_options[]
Optional list of options.
option const g_system_options[]
Definitions of the system options.
void check_for_show_sources(int argc, char *argv[])
Check for a "--show-option-sources" flag.
bool is_arg(char const *a)
Check whether this parameter is an argument.
The advgetopt environment to parse command line options.
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
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
bool is_false(std::string s)
Check whether a value represents "false".
static constexpr flag_t GETOPT_FLAG_SHOW_USAGE_ON_ERROR
constexpr option define_option(ARGS ...args)
@ SOURCE_ENVIRONMENT_VARIABLE
static constexpr flag_t GETOPT_FLAG_SHOW_SYSTEM
short_name_t string_to_short_name(std::string const &name)
Transform a string to a short name.
constexpr flag_t command_flags()
static constexpr flag_t GETOPT_FLAG_COMMAND_LINE
constexpr flag_t standalone_command_flags()
constexpr option end_options()
constexpr short_name_t NO_SHORT_NAME
bool is_true(std::string s)
Check whether a value represents "true".
std::string option_with_underscores(std::string const &s)
Converts an option back to using underscores.
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS
static constexpr flag_t GETOPT_FLAG_FLAG
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
std::string short_name_to_string(short_name_t short_name)
Convert a short name to a UTF-8 string.
std::string quote(std::string const &s, char open, char close)
The converse of unquote.
constexpr flag_t SYSTEM_OPTION_COMMANDS_MASK
constexpr flag_t any_flags()
static constexpr flag_t GETOPT_FLAG_MULTIPLE
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS
std::string option_with_dashes(std::string const &s)
Convert the _ found in a string to - instead.
static constexpr flag_t GETOPT_FLAG_REQUIRED
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_AUTO_DONE
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.
flag_t f_environment_flags
char const  * f_environment_variable_intro
char const  * f_configuration_filename
char const  * f_environment_variable_name