41#include <libutf8/libutf8.h>
46#include <libexcept/file_inheritance.h>
51#include <snapdev/not_reached.h>
52#include <snapdev/stringize.h>
53#include <snapdev/trim_string.h>
65#include <snapdev/poison.h>
104 "/etc/advgetopt/build-file-of-options.conf",
112#pragma GCC diagnostic ignored "-Wpedantic"
116 .f_group_name =
nullptr,
118 .f_options_files_directory =
nullptr,
120 .f_environment_variable_intro =
nullptr,
121 .f_section_variables_name =
nullptr,
123 .f_configuration_filename =
nullptr,
124 .f_configuration_directories =
nullptr,
126 .f_help_header =
"Usage: %p [-<opt>] <configuration file>\n"
127 "where -<opt> is one or more of:",
128 .f_help_footer =
"%c",
130 .f_license =
nullptr,
131 .f_copyright =
"Copyright (c) 2019-" SNAPDEV_STRINGIZE(UTC_BUILD_YEAR)
" Made to Order Software Corporation",
132 .f_build_date = UTC_BUILD_DATE,
133 .f_build_time = UTC_BUILD_TIME
148 int read_conf(std::string
const & filename);
149 void append_flag(std::string & flags, std::string
const & name);
152 std::ifstream f_in = std::ifstream();
180 for(std::size_t idx(0); idx < max; ++idx)
199 <<
"error: could not open file \""
210 for(++line;
f_in; ++line)
215 l = snapdev::trim_string(l);
226 l = snapdev::trim_string(l);
239 std::string::size_type
const pos(l.find(
'='));
242 if(pos != std::string::npos)
247 name = snapdev::trim_string(l.substr(1, pos - 1));
248 params = snapdev::trim_string(l.substr(pos + 1));
250 std::string::size_type
const space(name.find(
' '));
251 std::string::size_type
const colon(name.find(
':'));
253 && space == std::string::npos
254 && colon == std::string::npos)
257 std::string::size_type
const has_alias(name.find(
'|'));
258 if(has_alias != std::string::npos)
260 alias = name.substr(has_alias + 1);
261 name = name.substr(0, has_alias);
264 std::string short_name;
265 if(params.back() ==
')')
267 std::string::size_type
const start_short_name(params.rfind(
'('));
268 short_name = params.substr(start_short_name + 1, params.length() - start_short_name - 2);
269 params = snapdev::trim_string(params.substr(0, start_short_name));
270 if(libutf8::u8length(short_name) != 1)
272 std::cerr <<
"error:"
276 <<
": a short name specification must be exactly one character; \""
278 <<
"\" is not valid.\n";
288 std::string default_value;
289 for(++line;
f_in; ++line)
292 l = snapdev::trim_string(l);
296 l = snapdev::trim_string(l.substr(1));
297 if(l.length() >=
sizeof(
"Default:")
307 default_value = snapdev::trim_string(l.substr(8));
315 std::string::size_type
const dot(help.find(
'.'));
316 if(dot != std::string::npos)
318 help = snapdev::trim_string(help.substr(0, dot + 1),
true,
true,
true);
323 <<
" advgetopt::define_option(\n"
324 <<
" advgetopt::Name(\"" << name <<
"\")\n";
326 if(!short_name.empty())
329 <<
" , advgetopt::ShortName(U'" << short_name <<
"')\n";
334 if(params.front() !=
'[' || params.back() !=
']')
338 if((params.front() ==
'{' && params.back() ==
'}')
339 || params.find(
"...") != std::string::npos)
344 <<
" , advgetopt::Args(\"" << params <<
"\")\n";
350 <<
" , advgetopt::Help(\"" << help <<
"\")\n";
356 <<
" , advgetopt::Flags("
360 std::cout <<
" ),\n";
368 <<
" advgetopt::define_option(\n"
369 <<
" advgetopt::Name(\"" << alias <<
"\")\n";
378 <<
" , advgetopt::Help(\"" << name <<
"\")\n";
383 <<
" , advgetopt::Flags("
387 std::cout <<
" ),\n";
407 flags +=
"advgetopt::GETOPT_FLAG_";
419int main(
int argc,
char * argv[])
421 libexcept::verify_inherited_files();
425 build_file session(argc, argv);
426 return session.run();
432 std::cerr <<
"error: " << e.what() << std::endl;
436 catch(std::exception
const & e)
438 std::cerr <<
"error: exception caught: " << e.what() << std::endl;
441 snapdev::NOT_REACHED();
Definitions of the advanced getopt class.
int main(int argc, char *argv[])
Class used to parse command line options.
std::string get_string(std::string const &name, int idx=0, bool raw=false) const
Get the content of an option as a string.
std::size_t size(std::string const &name) const
Retrieve the number of arguments.
bool is_defined(std::string const &name) const
Check whether a parameter is defined.
void append_flag(std::string &flags, std::string const &name)
int read_conf(std::string const &filename)
build_file(int argc, char *argv[])
Definitions of the advanced getopt exceptions.
constexpr option define_option(ARGS ...args)
constexpr flag_t command_flags()
constexpr option end_options()
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS
static constexpr flag_t GETOPT_FLAG_DEFAULT_OPTION
static constexpr flag_t GETOPT_FLAG_MULTIPLE
static constexpr flag_t GETOPT_FLAG_REQUIRED
advgetopt::options_environment const g_options_environment
constexpr char const *const g_configuration_files[]
The tool looks for this configuration file.
advgetopt::option const g_options[]
Command line options.
Structure representing an option.
char const * f_environment_variable_name
char const * f_project_name
Definitions of the advanced getopt class version.
#define LIBADVGETOPT_VERSION_STRING