31 executableName = gutilities::getFileFromPath(argv[0]);
32 executableCallingDir = gutilities::getDirFromPath(argv[0]);
33 installDir = gutilities::gemc_root();
43 GVariable(
"conf_yaml",
"saved_configuration",
"the prefix for filename that store the used options"),
44 "The default value appends \"_saved_configuration\" to the executable name.");
48 "Timeout in milliseconds for the code tests that have GUI. ");
54 {
"release", gversion,
"release version number"},
55 {
"release_date", grelease_date,
"release date"},
56 {
"Reference", greference,
"article reference"},
57 {
"Homepage", gweb,
"homepage"},
58 {
"Author", gauthor,
"author"}
62 string help =
"Levels: \n \n";
63 help +=
" - 0: (default) = shush\n";
64 help +=
" - 1: log detailed information\n";
65 help +=
" - 2: log extra detailed information\n \n";
66 help +=
"Example: -verbosity.general=1 \n \n";
67 help +=
"This option can be repeated.\n \n";
71 help =
"Debug information Types: \n \n";
72 help +=
" - false: (default): do not print debug information\n";
73 help +=
" - true: print debug information\n\n";
74 help +=
"Example: -debug.general=true \n \n";
75 help +=
"This option can be repeated.\n \n";
79 for (
int i = 1; i < argc; i++) {
80 if (strcmp(argv[i],
"-h") == 0 || strcmp(argv[i],
"--h") == 0 ||
81 strcmp(argv[i],
"-help") == 0 || strcmp(argv[i],
"--help") == 0) {
83 }
else if (strcmp(argv[i],
"-hweb") == 0) {
85 }
else if (strcmp(argv[i],
"-v") == 0 || strcmp(argv[i],
"--v") == 0 ||
86 strcmp(argv[i],
"-version") == 0 || strcmp(argv[i],
"--version") == 0) {
89 }
else if (strcmp(argv[i],
"help") == 0) {
90 printOptionOrSwitchHelp(argv[i + 1]);
96 yaml_files = findYamls(argc, argv);
97 for (
auto &yaml_file: yaml_files) {
98 cout <<
" Parsing " << yaml_file << endl;
99 setOptionsValuesFromYamlFile(yaml_file);
103 for (
int i = 1; i < argc; i++) {
104 string candidate = argv[i];
105 if (candidate.empty())
continue;
106 if (find(yaml_files.begin(), yaml_files.end(), candidate) != yaml_files.end())
continue;
107 if (candidate[0] ==
'-') {
108 string argStr = candidate.substr(1);
109 size_t eqPos = argStr.find(
'=');
110 if (eqPos != string::npos) {
111 string keyPart = argStr.substr(0, eqPos);
112 string valuePart = argStr.substr(eqPos + 1);
115 if (!valuePart.empty() && valuePart.front() ==
'"' && valuePart.back() ==
'"') {
116 valuePart = valuePart.substr(1, valuePart.length() - 2);
119 size_t dotPos = keyPart.find(
'.');
120 if (dotPos != string::npos) {
122 string mainOption = keyPart.substr(0, dotPos);
123 string subOption = keyPart.substr(dotPos + 1);
125 auto it = getOptionIterator(mainOption);
126 it->set_sub_option_value(subOption, valuePart);
128 cerr <<
"The option " << mainOption <<
" is not known to this system." << endl;
134 setOptionValuesFromCommandLineArgument(keyPart, valuePart);
136 cerr <<
"The option " << keyPart <<
" is not known to this system." << endl;
142 const string& possibleSwitch = argStr;
143 if (switches.find(possibleSwitch) != switches.end()) {
144 switches[possibleSwitch].turnOn();
146 cerr <<
"The switch " << possibleSwitch <<
" is not known to this system." << endl;
151 cerr <<
"The command-line argument \"" << candidate <<
"\" is not valid." << endl;
160 string yamlConf_filename = executableName +
"." +
getScalarString(
"conf_yaml") +
".yaml";
161 cout <<
" Saving options to " << yamlConf_filename << endl << endl;
162 yamlConf =
new std::ofstream(yamlConf_filename);
173 if (switches.find(name) == switches.end()) {
174 switches[name] =
GSwitch(description);
176 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << name << RSTHHR
177 <<
" switch is already present." << std::endl;
190 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << gvar.
name << RSTHHR
191 <<
" option is already present." << std::endl;
206void GOptions::defineOption(
const std::string &name,
const std::string &description,
const std::vector <GVariable> &g_vars,
207 const std::string &help) {
209 std::cerr << FATALERRORL <<
"The " << YELLOWHHL << name << RSTHHR
210 <<
" option is already present." << std::endl;
213 goptions.emplace_back(name, description, g_vars, help);
224 auto it = getOptionIterator(tag);
226 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
227 <<
" was not found." << endl;
230 return it->value.begin()->second.as<
int>();
240 auto it = getOptionIterator(tag);
242 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
243 <<
" was not found." << endl;
246 return it->value.begin()->second.as<
double>();
256 auto it = getOptionIterator(tag);
258 cerr << FATALERRORL <<
"The option " << YELLOWHHL << tag << RSTHHR
259 <<
" was not found." << endl;
262 return it->value.begin()->second.as<
string>();
270void GOptions::printOptionOrSwitchHelp(
const std::string &tag)
const {
271 auto switchIt = switches.find(tag);
272 if (switchIt != switches.end()) {
273 cout << KGRN <<
"-" << tag << RST <<
": " << switchIt->second.getDescription() << endl << endl;
274 cout << TPOINTITEM <<
"Default value is " << (switchIt->second.getStatus() ?
"on" :
"off") << endl << endl;
277 for (
const auto &goption:
goptions) {
278 if (goption.name == tag) {
279 goption.printHelp(
true);
283 cerr << FATALERRORL <<
"The " << YELLOWHHL << tag << RSTHHR
284 <<
" option is not known to this system." << endl;
295vector <string> GOptions::findYamls(
int argc,
char *argv[]) {
296 vector <string> yaml_files;
297 for (
int i = 1; i < argc; i++) {
298 string arg = argv[i];
299 size_t pos = arg.find(
".yaml");
300 if (pos != string::npos) yaml_files.push_back(arg);
301 pos = arg.find(
".yml");
302 if (pos != string::npos) yaml_files.push_back(arg);
312 [&tag](
const auto& option) {
313 return option.name == tag;
322void GOptions::setOptionsValuesFromYamlFile(
const std::string &yaml) {
325 config = YAML::LoadFile(yaml);
326 }
catch (YAML::ParserException &e) {
327 cerr << FATALERRORL <<
"Error parsing " << YELLOWHHL << yaml << RSTHHR
328 <<
" yaml file." << endl;
329 cerr << e.what() << endl;
330 cerr <<
"Try validating the yaml file with an online yaml validator, e.g., https://www.yamllint.com" << endl;
334 for (
auto it = config.begin(); it != config.end(); ++it) {
335 auto option_name = it->first.as<std::string>();
336 auto option_it = getOptionIterator(option_name);
338 if (switches.find(option_name) == switches.end()) {
339 cerr << FATALERRORL <<
"The option or switch " << YELLOWHHL << option_name << RSTHHR
340 <<
" is not known to this system." << endl;
343 switches[option_name].turnOn();
346 YAML::NodeType::value type = it->second.Type();
348 case YAML::NodeType::Scalar:
349 option_it->set_scalar_value(it->second.as<std::string>());
351 case YAML::NodeType::Sequence:
352 option_it->set_value(it->second);
354 case YAML::NodeType::Map:
355 option_it->set_value(it->second);
370void GOptions::setOptionValuesFromCommandLineArgument(
const std::string &optionName,
const std::string &possibleYamlNode) {
371 YAML::Node node = YAML::Load(possibleYamlNode);
372 auto option_it = getOptionIterator(optionName);
373 if (node.Type() == YAML::NodeType::Scalar) {
374 option_it->set_scalar_value(possibleYamlNode);
376 option_it->set_value(node);
386std::vector<GOption>::iterator GOptions::getOptionIterator(
const std::string &name) {
388 [&name](
GOption &option) { return option.name == name; });
398std::vector<GOption>::const_iterator GOptions::getOptionIterator(
const std::string &name)
const {
400 [&name](
const GOption &option) { return option.name == name; });
410 auto it = switches.find(tag);
411 if (it != switches.end()) {
412 return it->second.getStatus();
414 std::cerr << FATALERRORL <<
"The switch " << YELLOWHHL << tag << RSTHHR
415 <<
" was not found." << std::endl;
429 for (
auto seq_item : sequence_node) {
430 for (
auto map_item = seq_item.begin(); map_item != seq_item.end(); ++map_item) {
431 if (map_item->first.as<
string>() == map_key) {
432 return map_item->second;
436 cerr << FATALERRORL <<
"The key " << YELLOWHHL << map_key << RSTHHR
437 <<
" was not found in " << YELLOWHHL << option_name << RSTHHR << endl;
452 if (node[variable_name]) {
453 return node[variable_name].as<T>();
455 return default_value;
459template int GOptions::get_variable_in_option<int>(
const YAML::Node &node,
const std::string &variable_name,
const int &default_value);
460template double GOptions::get_variable_in_option<double>(
const YAML::Node &node,
const std::string &variable_name,
const double &default_value);
461template string GOptions::get_variable_in_option<string>(
const YAML::Node &node,
const std::string &variable_name,
const string &default_value);
462template bool GOptions::get_variable_in_option<bool>(
const YAML::Node &node,
const std::string &variable_name,
const bool &default_value);
472 for (
auto v : verbosity_node) {
473 if (v.begin()->first.as<
string>() == tag) {
474 return v.begin()->second.as<
int>();
479 std::cerr << KRED <<
" Invalid verbosity or debug requested: " << tag << RST << std::endl;
493 for (
auto d : debug_node) {
494 if (d.begin()->first.as<
string>() == tag) {
495 YAML::Node valNode = d.begin()->second;
496 if (valNode.IsScalar()) {
497 auto s = valNode.as<
string>();
498 if (s ==
"true")
return 1;
499 if (s ==
"false")
return 0;
502 return valNode.as<
int>();
503 }
catch (
const YAML::BadConversion &) {
504 std::cerr <<
"Invalid debug value for " << tag << std::endl;
510 std::cerr << KRED <<
" Invalid verbosity or debug requested: " << tag << RST << std::endl;
516void GOptions::printHelp()
const {
519 cout << KGRN << KBOLD <<
" " << executableName << RST <<
" [options] [yaml files]" << endl << endl;
520 cout <<
" Switches: " << endl << endl;
521 for (
auto &s : switches) {
522 string help =
"-" + s.first + RST +
" ";
523 cout << KGRN <<
" " << left;
524 cout.width(fill_width);
526 cout <<
": " << s.second.getDescription() << endl;
529 cout <<
" Options: " << endl << endl;
531 option.printHelp(
false);
534 cout << endl <<
" Help / Search / Introspection: " << endl << endl;
535 vector<string> helps = {
536 string(
"-h, --h, -help, --help") + RST,
537 string(
"print this help and exit"),
538 string(
"-hweb") + RST,
539 string(
"print this help in web format and exit"),
540 string(
"-v, --v, -version, --version") + RST,
541 string(
"print the version and exit\n"),
542 string(
"help <value>") + RST,
543 string(
"print detailed help for option <value> and exit"),
544 string(
"search <value>") + RST,
545 string(
"list all options containing <value> in the description and exit\n")
547 unsigned half_help = helps.size() / 2;
548 for (
unsigned i = 0; i < half_help; i++) {
549 cout << KGRN <<
" " << left;
550 cout.width(fill_width);
551 cout << helps[i * 2] <<
": " << helps[i * 2 + 1] << endl;
554 cout <<
" Note: command line options overwrite YAML file(s)." << endl << endl;
563void GOptions::printWebHelp()
const {
570void GOptions::saveOptions()
const {
571 for (
auto &s : switches) {
572 string status = s.second.getStatus() ?
"true" :
"false";
573 *yamlConf << s.first +
": " + status <<
"," << endl;
575 for (
const auto &option :
goptions) {
576 option.saveOption(yamlConf);
584void GOptions::print_version() {
585 string asterisks =
"*******************************************************************";
586 cout << endl << asterisks << endl;
587 cout <<
" " << KGRN << KBOLD << executableName << RST <<
" version: " << KGRN << gversion << RST << endl;
588 cout <<
" Called from: " << KGRN << executableCallingDir << RST << endl;
589 cout <<
" Install: " << KGRN << installDir <<
"/bin" << RST << endl;
590 cout <<
" Released on: " << KGRN << grelease_date << RST << endl;
591 cout <<
" GEMC Reference: " << KGRN << greference << RST << endl;
592 cout <<
" GEMC Homepage: " << KGRN << gweb << RST << endl;
593 cout <<
" Author: " << KGRN << gauthor << RST << endl << endl;
594 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.
std::string getScalarString(const std::string &tag) const
Retrieves the value of a scalar string option.
bool getSwitch(const std::string &tag) const
Retrieves the status of a switch.
YAML::Node getOptionNode(const std::string &tag) const
Retrieves the YAML node for the specified option.
void defineSwitch(const std::string &name, const std::string &description)
Defines and adds a command–line switch.
GOptions()
Default constructor.
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option.
YAML::Node getOptionMapInNode(const std::string &option_name, const std::string &map_key) const
Retrieves a map option’s value from within a YAML node.
T get_variable_in_option(const YAML::Node &node, const std::string &variable_name, const T &default_value)
Retrieves a variable from a YAML node within an option.
double getScalarDouble(const std::string &tag) const
Retrieves the value of a scalar double option.
bool doesOptionExist(const std::string &tag) const
Checks if the specified option exists.
int getScalarInt(const std::string &tag) const
Retrieves the value of a scalar integer option.
void addGOptions(const GOptions &src)
Adds options from another GOptions object.
int getDebugFor(const std::string &tag) const
Retrieves the debug level for the specified tag.
int getVerbosityFor(const std::string &tag) const
Retrieves the verbosity level for the specified tag.
std::vector< GVariable > option_verbosity_names
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.
std::string name
The name of the variable.