6 #include "gutilities.h"
21 void GOption::set_scalar_value(
const string &v) {
23 if (v.empty())
return;
25 string value_to_set = gutilities::replaceCharInStringWithChars(v,
",",
"");
27 string key = value.begin()->first.as<
string>();
28 value[key] = value_to_set;
31 void GOption::set_value(
const YAML::Node &v) {
45 for (
const auto &element: v) {
46 if (!does_the_option_set_all_necessary_values(element)) {
47 cerr << FATALERRORL <<
"Trying to set " << YELLOWHHL << name << RSTHHR <<
" but missing mandatory values." << endl;
48 cerr <<
" Use the option: " << YELLOWHHL <<
" help " << name <<
" " << RSTHHR <<
" for details." << endl << endl;
56 auto default_value_node = defaultValue.begin()->second;
58 for (
const auto &map_element_in_default_value: default_value_node) {
59 for (YAML::const_iterator default_value_iterator = map_element_in_default_value.begin();
60 default_value_iterator != map_element_in_default_value.end(); ++default_value_iterator) {
62 string default_key = default_value_iterator->first.as<
string>();
63 auto default_value = default_value_iterator->second;
65 for (
auto map_element_in_value: value[name]) {
66 bool key_found =
false;
68 for (YAML::const_iterator value_iterator = map_element_in_value.begin();
69 value_iterator != map_element_in_value.end(); ++value_iterator) {
70 string value_key = value_iterator->first.as<
string>();
72 if (default_key == value_key) {
78 map_element_in_value[default_key] = default_value;
87 for (
const auto &map_element_in_desired_value: v) {
89 for (YAML::const_iterator desired_value_iterator = map_element_in_desired_value.begin();
90 desired_value_iterator != map_element_in_desired_value.end(); ++desired_value_iterator) {
92 for (
auto existing_map: value[name]) {
93 for (YAML::const_iterator existing_map_iterator = existing_map.begin();
94 existing_map_iterator != existing_map.end(); ++existing_map_iterator) {
96 string first_key = existing_map_iterator->first.as<
string>();
97 string second_key = desired_value_iterator->first.as<
string>();
99 if (first_key == second_key) {
100 existing_map[existing_map_iterator->first] = desired_value_iterator->second;
117 bool GOption::does_the_option_set_all_necessary_values(YAML::Node v) {
118 vector <string> this_keys;
121 case YAML::NodeType::Map:
122 for (
const auto &it : v) {
123 this_keys.push_back(it.first.as<
string>());
130 for (
const auto &key : mandatory_keys) {
131 if (find(this_keys.begin(), this_keys.end(), key) == this_keys.end()) {
145 void GOption::saveOption(std::ofstream *yamlConf)
const {
149 YAML::Node mutableValue = value;
150 mutableValue.SetStyle(YAML::EmitterStyle::Block);
152 *yamlConf << mutableValue << std::endl;
164 void GOption::printHelp(
bool detailed)
const {
171 string helpString =
"-" + name + RST;
173 bool is_sequence = defaultValue.begin()->second.IsSequence();
176 helpString +=
"=<sequence>";
178 helpString +=
"=<value>";
182 cout << KGRN <<
" " << left;
183 cout.width(fill_width);
186 cout << helpString <<
": " << description << endl;
188 cout << detailedHelp() << endl;
190 cout << helpString <<
": " << description << endl;
202 string GOption::detailedHelp()
const {
205 YAML::Node yvalues = defaultValue.begin()->second;
206 if (yvalues.IsSequence()) {
209 for (
unsigned i = 0; i < (unsigned) yvalues.size(); i++) {
210 YAML::Node this_node = yvalues[i];
212 for (YAML::const_iterator it = this_node.begin(); it != this_node.end(); ++it) {
213 cout << TGREENPOINTITEM << KGRN << it->first.as<
string>() << RST
214 <<
": " << gvar_descs[i] <<
". Default value: " << it->second.as<
string>() << endl;
220 vector <string> help_lines = gutilities::getStringVectorFromStringWithDelimiter(help,
"\n");
222 for (
auto line: help_lines) {
223 newHelp += GTAB + line +
"\n";
#define EC__MANDATORY_NOT_FILLED