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>
210 ,
Help(
"print out the time and date when %p was built and exit.")
213 Name(
"compiler-version")
217 ,
Help(
"print the version of the compiler used to compile the advgetopt library.")
220 Name(
"configuration-filenames")
224 ,
Help(
"print out the list of configuration files checked out by this tool.")
231 ,
Help(
"print out the copyright of %p and exit.")
234 Name(
"environment-variable-name")
238 ,
Help(
"print out the name of the environment variable supported by %p (if any.)")
241 Name(
"filenames-of-option-definitions")
245 ,
Help(
"print out the full filename of the main option definitions and if any, the filenames of additional option definitions (often used by plugins).")
248 Name(
"has-sanitizer")
252 ,
Help(
"print whether the advgetopt was compiled with the sanitizer extension.")
260 ,
Help(
"print out this help screen and exit.")
267 ,
Help(
"print out the license of %p and exit.")
270 Name(
"path-to-option-definitions")
274 ,
Help(
"print out the path to the option definitions.")
282 ,
Help(
"print the value of the named option after parsing all the options from the command line, environment variables, and configuration files.")
285 Name(
"show-option-sources")
289 ,
Help(
"parse all the options and then print out the source of each value and each override.")
296 ,
Help(
"print out the version of %p and exit.")
324 ,
Help(
"add one or more configuration directory paths to search for configuration files.")
345 return a[0] ==
'-' && a[1] !=
'\0';
362 static bool found =
false;
364 if(!found && argv !=
nullptr)
366 for(
int idx(1); idx < argc; ++idx)
368 if(strcmp(argv[idx],
"--show-option-sources") == 0)
613 check_for_show_sources(argc, argv);
678 throw getopt_logic_error(
"argv pointer cannot be nullptr");
681 check_for_show_sources(argc, argv);
686 throw getopt_logic_error(
"an empty list of options is not legal, you must defined at least one (i.e. --version, --help...)");
706 if(cppthread::log.get_errors() != 0)
708 throw getopt_exit(
"errors were found on your command line, environment variable, or configuration file.", 1);
730 throw getopt_initialization(
731 "function called too soon, parser is not done yet"
732 " (i.e. is_defined(), get_string(), get_long(),"
733 " get_double() cannot be called until the parser is done)");
842 std::string
const name(opt.second->get_environment_variable_name());
847 if(opt.second->get_environment_variable_value(
887 std::string
const & str
889 ,
bool only_environment_variable)
905 std::vector<char *> sub_argv;
907 sub_argv.resize(args.size() + 2);
915 for(
size_t idx(0); idx < args.size(); ++idx)
917 sub_argv[idx + 1] =
const_cast<char *
>(args[idx].c_str());
922 sub_argv[args.size() + 1] =
nullptr;
927 static_cast<int>(sub_argv.size() - 1)
930 , only_environment_variable);
956 char const * s(environment.c_str());
977 char const quote(*s++);
1054 ,
bool only_environment_variable)
1056 for(
int i(1); i < argc; ++i)
1058 if(argv[i][0] ==
'-')
1060 if(argv[i][1] ==
'-')
1062 if(argv[i][2] ==
'\0')
1069 cppthread::log << cppthread::log_level_t::error
1070 <<
"no default options defined; thus \"--\" is not accepted by this program."
1075 if(only_environment_variable)
1079 cppthread::log << cppthread::log_level_t::error
1080 <<
"option \"--\" is not supported in the environment variable."
1089 cppthread::log << cppthread::log_level_t::error
1090 <<
"option \"--\" is not supported on the command line."
1110 std::string option_name(argv[i] + 2);
1111 std::string option_value;
1112 std::string::size_type pos(option_name.find(
'='));
1113 if(pos != std::string::npos)
1117 cppthread::log << cppthread::log_level_t::error
1118 <<
"name missing in \""
1125 option_value = option_name.substr(pos + 1);
1126 option_name.resize(pos);
1132 pos = option_name.find_first_of(
"[:");
1133 if(pos != std::string::npos)
1136 option_name.resize(pos);
1141 cppthread::log << cppthread::log_level_t::error
1144 <<
"\" is not supported."
1150 cppthread::log << cppthread::log_level_t::error
1153 <<
"\" does not support the array or map syntax."
1157 if(option_keys.empty())
1159 cppthread::log << cppthread::log_level_t::error
1162 <<
"\" has an invalid list of array keys."
1167 if(only_environment_variable)
1171 cppthread::log << cppthread::log_level_t::error
1174 <<
"\" is not supported in the environment variable."
1183 cppthread::log << cppthread::log_level_t::error
1186 <<
"\" is not supported on the command line."
1191 if(pos != std::string::npos)
1204 add_options(opt, i, argc, argv, option_keys, source);
1210 if(argv[i][1] ==
'\0')
1216 cppthread::log << cppthread::log_level_t::error
1217 <<
"no default options defined; thus \"-\" is not accepted by this program."
1221 if(only_environment_variable)
1225 cppthread::log << cppthread::log_level_t::error
1226 <<
"option \"-\" is not supported in the environment variable."
1235 cppthread::log << cppthread::log_level_t::error
1236 <<
"option \"-\" is not supported on the command line."
1253 std::string
const short_args_string(argv[i] + 1);
1254 for(libutf8::utf8_iterator short_args(short_args_string)
1255 ; short_args != short_args_string.end()
1261 cppthread::log << cppthread::log_level_t::error
1264 <<
"\" is not supported."
1268 if(only_environment_variable)
1272 cppthread::log << cppthread::log_level_t::error
1275 <<
"\" is not supported in the environment variable."
1284 cppthread::log << cppthread::log_level_t::error
1287 <<
"\" is not supported on the command line."
1303 cppthread::log << cppthread::log_level_t::error
1304 <<
"no default options defined; we do not know what to do of \""
1306 <<
"\"; standalone parameters are not accepted by this program."
1310 if(only_environment_variable)
1314 cppthread::log << cppthread::log_level_t::error
1315 <<
"default options are not supported in the environment variable."
1324 cppthread::log << cppthread::log_level_t::error
1325 <<
"default options are not supported on the command line."
1407 if(raw_key[0] ==
'[')
1414 std::string::size_type pos(1);
1417 std::string::size_type start(pos);
1418 pos = raw_key.find(
']', pos);
1419 if(pos == std::string::npos)
1425 std::string key(raw_key.substr(start, pos - start));
1428 keys.push_back(key);
1431 if(pos >= raw_key.length())
1435 if(raw_key[pos] !=
'[')
1453 std::sort(keys.begin(), keys.end());
1454 keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
1477 opt = opt->get_alias_destination();
1480 throw getopt_undefined(
"getopt::get_alias_destination(): alias is missing. Did you call link_aliases()?");
1540 throw getopt_invalid_parameter(
"get_option() `name` argument cannot be empty.");
1642 opt->add_value(opt->get_default(), option_keys, source);
1646 if(i + 1 < argc && !is_arg(argv[i + 1]))
1653 opt->add_value(argv[i], option_keys, source);
1655 while(i + 1 < argc && !is_arg(argv[i + 1]));
1660 opt->add_value(argv[i], option_keys, source);
1667 cppthread::log << cppthread::log_level_t::error
1670 <<
" expects an argument."
1679 opt->add_value(std::string(), option_keys, source);
1707 , std::string
const & value
1708 , std::string
const & filename
1722 opt->set_multiple_values(value, option_keys, source);
1726 opt->set_value(0, value, option_keys, source);
1745 opt->set_value(0, std::string(), option_keys, source);
1749#pragma GCC diagnostic push
1750#pragma GCC diagnostic ignored "-Wrestrict"
1751 cppthread::log << cppthread::log_level_t::error
1753 << (filename.empty()
1754 ?
"--" + opt->get_name()
1756 <<
" cannot be given value \""
1759 << (filename.empty()
1761 :
" in configuration file \""
1764 <<
". It only accepts \"true\" or \"false\"."
1766#pragma GCC diagnostic pop
1774#pragma GCC diagnostic push
1775#pragma GCC diagnostic ignored "-Wrestrict"
1776 cppthread::log << cppthread::log_level_t::error
1778 << (filename.empty()
1779 ?
"--" + opt->get_name()
1781 <<
" must be given a value"
1782 << (filename.empty()
1784 :
" in configuration file \""
1789#pragma GCC diagnostic pop
1795 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