7 #include "gutilities.h"
31 executableName = gutilities::getFileFromPath(argv[0]);
35 addGOptions(user_defined_options);
38 defineSwitch(
"gui",
"use Graphical User Interface");
39 defineSwitch(
"i",
"use interactive batch mode");
41 GVariable(
"conf_yaml",
"saved_configuration",
"the yaml filename prefix where all used options are saved"),
42 "The default value appends \"_saved_configuration\" to the executable name.");
48 {
"release", gversion,
"release version number"},
49 {
"release_date", grelease_date,
"release date"},
50 {
"Reference", greference,
"article reference"},
51 {
"Homepage", gweb,
"homepage"},
52 {
"Author", gauthor,
"author"}
54 defineOption(
GVERSION_STRING,
"version information",
version,
"Version information. Not settable by user.");
56 string help =
"Levels: \n \n";
57 help +=
" - 0: (default) shush\n";
58 help +=
" - 1: normal information\n";
59 help +=
" - 2: detailed information\n \n";
60 help +=
"Example: -verbosity.general=1 \n \n";
61 help +=
"This option can be repeated.\n \n";
62 defineOption(
"verbosity",
"Sets the log verbosity for various classes", option_verbosity_names, help);
65 help =
"Debug information Types: \n \n";
66 help +=
" - false: (default): do not print debug information\n";
67 help +=
" - true: print debug information\n\n";
68 help +=
"Example: -debug.general=true \n \n";
69 help +=
"This option can be repeated.\n \n";
70 defineOption(
"debug",
"Sets the debug level for various classes", option_verbosity_names, help);
73 for (
int i = 1; i < argc; i++) {
74 if (strcmp(argv[i],
"-h") == 0 || strcmp(argv[i],
"--h") == 0 ||
75 strcmp(argv[i],
"-help") == 0 || strcmp(argv[i],
"--help") == 0) {
77 }
else if (strcmp(argv[i],
"-hweb") == 0) {
79 }
else if (strcmp(argv[i],
"-v") == 0 || strcmp(argv[i],
"--v") == 0 ||
80 strcmp(argv[i],
"-version") == 0 || strcmp(argv[i],
"--version") == 0) {
83 }
else if (strcmp(argv[i],
"help") == 0) {
84 printOptionOrSwitchHelp(argv[i + 1]);
90 yaml_files = findYamls(argc, argv);
91 for (
auto &yaml_file: yaml_files) {
92 cout <<
" Parsing " << yaml_file << endl;
93 setOptionsValuesFromYamlFile(yaml_file);
97 for (
int i = 1; i < argc; i++) {
98 string candidate = argv[i];
99 if (candidate.empty())
continue;
100 if (find(yaml_files.begin(), yaml_files.end(), candidate) != yaml_files.end())
continue;
101 if (candidate[0] ==
'-') {
102 string argStr = candidate.substr(1);
103 size_t eqPos = argStr.find(
'=');
104 if (eqPos != string::npos) {
105 string keyPart = argStr.substr(0, eqPos);
106 string valuePart = argStr.substr(eqPos + 1);
107 size_t dotPos = keyPart.find(
'.');
108 if (dotPos != string::npos) {
110 string mainOption = keyPart.substr(0, dotPos);
111 string subOption = keyPart.substr(dotPos + 1);
112 if (doesOptionExist(mainOption)) {
113 auto it = getOptionIterator(mainOption);
114 it->set_sub_option_value(subOption, valuePart);
116 cerr <<
"The option " << mainOption <<
" is not known to this system." << endl;
121 if (doesOptionExist(keyPart)) {
122 setOptionValuesFromCommandLineArgument(keyPart, valuePart);
124 cerr <<
"The option " << keyPart <<
" is not known to this system." << endl;
130 const string& possibleSwitch = argStr;
131 if (switches.find(possibleSwitch) != switches.end()) {
132 switches[possibleSwitch].turnOn();
134 cerr <<
"The switch " << possibleSwitch <<
" is not known to this system." << endl;
139 cerr <<
"The command-line argument \"" << candidate <<
"\" is not valid." << endl;
148 string yamlConf_filename = executableName +
"." + getScalarString(
"conf_yaml") +
".yaml";
149 cout <<
" Saving options to " << yamlConf_filename << endl << endl;
150 yamlConf =
new std::ofstream(yamlConf_filename);
161 if (switches.find(name) == switches.end()) {
162 switches[name] =
GSwitch(description);
164 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << name << RSTHHR
165 <<
" switch is already present." << std::endl;
177 if (doesOptionExist(gvar.
name)) {
178 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << gvar.
name << RSTHHR
179 <<
" option is already present." << std::endl;
194 void GOptions::defineOption(
const std::string &name,
const std::string &description,
const std::vector <GVariable> &g_vars,
195 const std::string &help) {
196 if (doesOptionExist(name)) {
197 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << name << RSTHHR
198 <<
" option is already present." << std::endl;
201 goptions.emplace_back(name, description, g_vars, help);
212 auto it = getOptionIterator(tag);
214 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
215 <<
" was not found." << endl;
218 return it->value.begin()->second.as<
int>();
228 auto it = getOptionIterator(tag);
230 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
231 <<
" was not found." << endl;
234 return it->value.begin()->second.as<
float>();
244 auto it = getOptionIterator(tag);
246 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
247 <<
" was not found." << endl;
250 return it->value.begin()->second.as<
double>();
260 auto it = getOptionIterator(tag);
262 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
263 <<
" was not found." << endl;
266 return it->value.begin()->second.as<
string>();
274 void GOptions::printOptionOrSwitchHelp(
const std::string &tag)
const {
275 auto switchIt = switches.find(tag);
276 if (switchIt != switches.end()) {
277 cout << KGRN <<
"-" << tag << RST <<
": " << switchIt->second.getDescription() << endl << endl;
278 cout << TPOINTITEM <<
"Default value is " << (switchIt->second.getStatus() ?
"on" :
"off") << endl << endl;
281 for (
const auto &goption:
goptions) {
282 if (goption.name == tag) {
283 goption.printHelp(
true);
287 cerr << FATALERRORL <<
"The " << YELLOWHHL << tag << RSTHHR
288 <<
" option is not known to this system." << endl;
299 vector <string> GOptions::findYamls(
int argc,
char *argv[]) {
300 vector <string> yaml_files;
301 for (
int i = 1; i < argc; i++) {
302 string arg = argv[i];
303 size_t pos = arg.find(
".yaml");
304 if (pos != string::npos) yaml_files.push_back(arg);
305 pos = arg.find(
".yml");
306 if (pos != string::npos) yaml_files.push_back(arg);
316 [&tag](
const auto& option) {
317 return option.name == tag;
326 void GOptions::setOptionsValuesFromYamlFile(
const std::string &yaml) {
329 config = YAML::LoadFile(yaml);
330 }
catch (YAML::ParserException &e) {
331 cerr << FATALERRORL <<
"Error parsing " << YELLOWHHL << yaml << RSTHHR
332 <<
" yaml file." << endl;
333 cerr << e.what() << endl;
334 cerr <<
"Try validating the yaml file with an online yaml validator, e.g., https://www.yamllint.com" << endl;
338 for (
auto it = config.begin(); it != config.end(); ++it) {
339 auto option_name = it->first.as<std::string>();
340 auto option_it = getOptionIterator(option_name);
342 if (switches.find(option_name) == switches.end()) {
343 cerr << FATALERRORL <<
"The option or switch " << YELLOWHHL << option_name << RSTHHR
344 <<
" is not known to this system." << endl;
347 switches[option_name].turnOn();
350 YAML::NodeType::value type = it->second.Type();
352 case YAML::NodeType::Scalar:
353 option_it->set_scalar_value(it->second.as<std::string>());
355 case YAML::NodeType::Sequence:
356 option_it->set_value(it->second);
358 case YAML::NodeType::Map:
359 option_it->set_value(it->second);
374 void GOptions::setOptionValuesFromCommandLineArgument(
const std::string &optionName,
const std::string &possibleYamlNode) {
375 YAML::Node node = YAML::Load(possibleYamlNode);
376 auto option_it = getOptionIterator(optionName);
377 if (node.Type() == YAML::NodeType::Scalar) {
378 option_it->set_scalar_value(possibleYamlNode);
380 option_it->set_value(node);
390 std::vector<GOption>::iterator GOptions::getOptionIterator(
const std::string &name) {
392 [&name](
GOption &option) { return option.name == name; });
402 std::vector<GOption>::const_iterator GOptions::getOptionIterator(
const std::string &name)
const {
404 [&name](
const GOption &option) { return option.name == name; });
414 auto it = switches.find(tag);
415 if (it != switches.end()) {
416 return it->second.getStatus();
418 std::cerr << FATALERRORL <<
"The switch " << YELLOWHHL << tag << RSTHHR
419 <<
" was not found." << std::endl;
432 auto sequence_node = getOptionNode(option_name);
433 for (
auto seq_item : sequence_node) {
434 for (
auto map_item = seq_item.begin(); map_item != seq_item.end(); ++map_item) {
435 if (map_item->first.as<
string>() == map_key) {
436 return map_item->second;
440 cerr << FATALERRORL <<
"The key " << YELLOWHHL << map_key << RSTHHR
441 <<
" was not found in " << YELLOWHHL << option_name << RSTHHR << endl;
443 return sequence_node;
457 if (node[variable_name]) {
458 return node[variable_name].as<T>();
460 return default_value;
464 template int GOptions::get_variable_in_option<int>(
const YAML::Node &node,
const std::string &variable_name,
const int &default_value);
465 template float GOptions::get_variable_in_option<float>(
const YAML::Node &node,
const std::string &variable_name,
const float &default_value);
466 template double GOptions::get_variable_in_option<double>(
const YAML::Node &node,
const std::string &variable_name,
const double &default_value);
467 template string GOptions::get_variable_in_option<string>(
const YAML::Node &node,
const std::string &variable_name,
const string &default_value);
468 template bool GOptions::get_variable_in_option<bool>(
const YAML::Node &node,
const std::string &variable_name,
const bool &default_value);
477 YAML::Node verbosity_node = getOptionNode(
"verbosity");
478 for (
auto v : verbosity_node) {
479 if (v.begin()->first.as<
string>() == tag) {
480 return v.begin()->second.as<
int>();
485 std::cerr << KRED <<
" Invalid verbosity or debug requested: " << tag << RST << std::endl;
498 YAML::Node debug_node = getOptionNode(
"debug");
499 for (
auto d : debug_node) {
500 if (d.begin()->first.as<
string>() == tag) {
501 YAML::Node valNode = d.begin()->second;
502 if (valNode.IsScalar()) {
503 string s = valNode.as<
string>();
504 if (s ==
"true")
return 1;
505 if (s ==
"false")
return 0;
508 return valNode.as<
int>();
509 }
catch (
const YAML::BadConversion &) {
510 std::cerr <<
"Invalid debug value for " << tag << std::endl;
516 std::cerr << KRED <<
" Invalid verbosity or debug requested: " << tag << RST << std::endl;
522 void GOptions::printHelp()
const {
525 cout << KGRN << KBOLD <<
" " << executableName << RST <<
" [options] [yaml files]" << endl << endl;
526 cout <<
" Switches: " << endl << endl;
527 for (
auto &s : switches) {
528 string help =
"-" + s.first + RST +
" ";
529 cout << KGRN <<
" " << left;
530 cout.width(fill_width);
532 cout <<
": " << s.second.getDescription() << endl;
535 cout <<
" Options: " << endl << endl;
537 option.printHelp(
false);
540 cout << endl <<
" Help / Search / Introspection: " << endl << endl;
541 vector<string> helps = {
542 string(
"-h, --h, -help, --help") + RST,
543 string(
"print this help and exit"),
544 string(
"-hweb") + RST,
545 string(
"print this help in web format and exit"),
546 string(
"-v, --v, -version, --version") + RST,
547 string(
"print the version and exit\n"),
548 string(
"help <value>") + RST,
549 string(
"print detailed help for option <value> and exit"),
550 string(
"search <value>") + RST,
551 string(
"list all options containing <value> in the description and exit\n")
553 unsigned half_help = helps.size() / 2;
554 for (
unsigned i = 0; i < half_help; i++) {
555 cout << KGRN <<
" " << left;
556 cout.width(fill_width);
557 cout << helps[i * 2] <<
": " << helps[i * 2 + 1] << endl;
560 cout <<
" Note: command line options overwrite YAML file(s)." << endl << endl;
569 void GOptions::printWebHelp()
const {
576 void GOptions::saveOptions()
const {
577 for (
auto &s : switches) {
578 string status = s.second.getStatus() ?
"true" :
"false";
579 *yamlConf << s.first +
": " + status <<
"," << endl;
581 for (
const auto &option :
goptions) {
582 option.saveOption(yamlConf);
590 void GOptions::print_version() {
591 string asterisks =
"**************************************************************";
592 cout << endl << asterisks << endl;
593 cout <<
" " << KGRN << KBOLD << executableName << RST <<
" version: " << KGRN << gversion << RST << endl;
594 cout <<
" Released on: " << KGRN << grelease_date << RST << endl;
595 cout <<
" Reference: " << KGRN << greference << RST << endl;
596 cout <<
" Homepage: " << KGRN << gweb << RST << endl;
597 cout <<
" Author: " << KGRN << gauthor << RST << endl << endl;
598 cout << asterisks << endl << endl;
Represents a configurable option with a name, value(s), description, and help text.
The GOptions class manages command-line options and switches.
void defineOption(const GVariable &gvar, const string &help)
Defines and adds a scalar option.
bool getSwitch(const string &tag) const
Retrieves the status of a switch.
int getDebugFor(const string &tag) const
Retrieves the debug level for the specified tag.
T get_variable_in_option(const YAML::Node &node, const string &variable_name, const T &default_value)
Retrieves a variable from a YAML node within an option.
int getVerbosityFor(const string &tag) const
Retrieves the verbosity level for the specified tag.
YAML::Node getOptionMapInNode(string option_name, string map_key) const
Retrieves a map option’s value from within a YAML node.
GOptions()
Default constructor.
int getScalarInt(const string &tag) const
Retrieves the value of a scalar integer option.
void addGOptions(const GOptions &goptions_to_add)
Adds options from another GOptions object.
bool doesOptionExist(const string &tag) const
Checks if the specified option exists.
float getScalarFloat(const string &tag) const
Retrieves the value of a scalar float option.
void defineSwitch(const string &name, const string &description)
Defines and adds a command–line switch.
string getScalarString(const string &tag) const
Retrieves the value of a scalar string option.
double getScalarDouble(const string &tag) const
Retrieves the value of a scalar double option.
Represents a switch with a description and a status.
#define EC__BAD_CONVERSION
#define EC__NOOPTIONFOUND
#define EC__YAML_PARSING_ERROR
#define EC__DEFINED_SWITCHALREADYPRESENT
#define EC__DEFINED_OPTION_ALREADY_PRESENT
GOptions & operator+=(GOptions &gopts, const GOptions &goptions_to_add)
Overloaded operator to add options and switches from one GOptions object to another.
Encapsulates a variable with a name, value, and description.
string name
The name of the variable.