39void GOption::set_scalar_value(
const string& v) {
40 if (v.empty())
return;
46 auto key = value.begin()->first.as<
string>();
47 value[key] = value_to_set;
57void GOption::set_value(
const YAML::Node& v) {
60 for (
const auto& element : v) {
61 if (!does_the_option_set_all_necessary_values(element)) {
63 <<
" but missing mandatory values." << endl;
64 cerr <<
" Use the option: " <<
YELLOWHHL <<
" help " << name
65 <<
" " <<
RSTHHR <<
" for details." << endl << endl;
75 auto default_value_node = defaultValue.begin()->second;
77 for (
const auto& map_element_in_default_value : default_value_node) {
78 for (
auto default_value_iterator = map_element_in_default_value.begin();
79 default_value_iterator != map_element_in_default_value.end(); ++default_value_iterator) {
80 auto default_key = default_value_iterator->first.as<
string>();
81 auto default_value = default_value_iterator->second;
84 for (
auto map_element_in_value : value[name]) {
85 bool key_found =
false;
87 for (
auto value_iterator = map_element_in_value.begin();
88 value_iterator != map_element_in_value.end(); ++value_iterator) {
89 auto value_key = value_iterator->first.as<
string>();
90 if (default_key == value_key) {
97 map_element_in_value[default_key] = default_value;
106 for (
const auto& map_element_in_desired_value : v) {
107 for (
auto desired_value_iterator = map_element_in_desired_value.begin();
108 desired_value_iterator != map_element_in_desired_value.end(); ++desired_value_iterator) {
109 for (
auto existing_map : value[name]) {
110 for (
auto existing_map_iterator = existing_map.begin();
111 existing_map_iterator != existing_map.end(); ++existing_map_iterator) {
112 auto first_key = existing_map_iterator->first.as<
string>();
113 auto second_key = desired_value_iterator->first.as<
string>();
116 if (first_key == second_key) {
117 existing_map[existing_map_iterator->first] = desired_value_iterator->second;
132bool GOption::does_the_option_set_all_necessary_values(
const YAML::Node& v) {
133 vector<string> this_keys;
134 if (v.Type() == YAML::NodeType::Map) {
135 for (
const auto& it : v) {
136 this_keys.push_back(it.first.as<
string>());
140 for (
const auto& key : mandatory_keys) {
141 if (find(this_keys.begin(), this_keys.end(), key) == this_keys.end()) {
154void GOption::saveOption(std::ofstream* yamlConf)
const {
155 std::vector<std::string> missing;
160 std::function<YAML::Node(YAML::Node, std::string)> clean =
161 [&](YAML::Node n,
const std::string& path) -> YAML::Node {
163 missing.push_back(path.empty() ? name : path);
164 return YAML::Node(
"not provided");
168 YAML::Node res(YAML::NodeType::Map);
170 const std::string key = it.first.as<std::string>();
171 res[it.first] = clean(it.second, path.empty() ? key : path +
"." + key);
176 if (n.IsSequence()) {
177 YAML::Node res(YAML::NodeType::Sequence);
178 for (std::size_t i = 0; i < n.size(); ++i) {
179 res.push_back(clean(n[i], path +
"[" + std::to_string(i) +
"]"));
187 YAML::Node out = clean(value,
"");
192 for (
const auto& p : missing) {
193 *yamlConf <<
"# " << p <<
" not provided\n";
197 out.SetStyle(YAML::EmitterStyle::Block);
198 *yamlConf << out <<
'\n';
208void GOption::printHelp(
bool detailed)
const {
214 string helpString =
"-" + name +
RST;
215 bool is_sequence = defaultValue.begin()->second.IsSequence();
216 helpString += is_sequence ?
"=<sequence>" :
"=<value>";
219 cout <<
KGRN <<
" " << left;
220 cout.width(fill_width);
223 cout << helpString <<
": " << description << endl << endl;
224 cout << detailedHelp() << endl;
227 cout << helpString <<
": " << description << endl;
237string GOption::detailedHelp()
const {
239 YAML::Node yvalues = defaultValue.begin()->second;
241 if (yvalues.IsSequence()) {
244 for (
unsigned i = 0; i < yvalues.size(); i++) {
245 YAML::Node this_node = yvalues[i];
247 for (
auto it = this_node.begin(); it != this_node.end(); ++it) {
249 <<
": " << gvar_descs[i] <<
". Default value: " << it->second.as<
string>() << endl;
256 for (
const auto& line : help_lines) {
257 newHelp +=
GTAB + line +
"\n";
271 YAML::Node option_node = value.begin()->second;
273 if (option_node.IsSequence()) {
274 bool updated =
false;
276 for (
auto it = option_node.begin(); it != option_node.end(); ++it) {
278 if ((*it).IsMap() && (*it)[subkey]) {
279 (*it)[subkey] = YAML::Load(subvalue);
285 cerr <<
"Sub-option key '" << subkey <<
"' not found in option '" << name <<
"'." << endl;
289 else if (option_node.IsMap()) {
290 if (option_node[subkey]) {
291 option_node[subkey] = YAML::Load(subvalue);
294 cerr <<
"Sub-option key '" << subkey <<
"' not found in option '" << name <<
"'." << endl;
299 cerr <<
"Option '" << name <<
"' is not structured to accept sub–options." << endl;
void set_sub_option_value(const std::string &subkey, const std::string &subvalue)
Updates a structured sub-option using dot-notation semantics.
Definitions of GVariable : and GOption : used by GOptions : .
Conventions, constants, and error codes for the GOptions : / GOption : subsystem.
#define EC__NOOPTIONFOUND
Option/switch/key not found, or invalid command-line token.
#define HELPFILLSPACE
Padding used when printing option/switch help.
#define GVERSION_STRING
Reserved option tag used to store version information.
#define EC__MANDATORY_NOT_FILLED
Mandatory structured option key (NODFLT) missing.
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
string replaceCharInStringWithChars(const std::string &input, const std::string &toReplace, const std::string &replacement)