goptions
Loading...
Searching...
No Matches
goption.cc
Go to the documentation of this file.
1
15#include "goption.h"
16#include "goptionsConventions.h"
17#include "gutilities.h"
18
19// gemc
20#include "gutsConventions.h"
21
22#include <iostream>
23#include <algorithm>
24#include <functional>
25
26using std::cerr;
27using std::endl;
28using std::cout;
29using std::left;
30using std::string;
31using std::vector;
32
33// See goption.h for API docs.
34/*
35 * Implementation notes:
36 * - Scalar values are normalized to preserve legacy comma-delimited payloads.
37 * - For structured options, the behavior differs between cumulative and non-cumulative schemas.
38 */
39void GOption::set_scalar_value(const string& v) {
40 if (v.empty()) return;
41
42 // Legacy normalization: remove commas so payloads like "a,b,c" remain shell-friendly.
43 string value_to_set = gutilities::replaceCharInStringWithChars(v, ",", "");
44
45 // Scalar options are stored as a single-entry map: { <name>: <scalar> }.
46 auto key = value.begin()->first.as<string>();
47 value[key] = value_to_set;
48}
49
50// See goption.h for API docs.
51/*
52 * Implementation notes:
53 * - Cumulative options store a user-provided sequence of maps and then back-fill optional keys
54 * from the schema defaults.
55 * - Non-cumulative structured options update existing key/value pairs in-place (by matching keys).
56 */
57void GOption::set_value(const YAML::Node& v) {
58 if (isCumulative) {
59 // Validate that each user-provided entry includes all mandatory keys.
60 for (const auto& element : v) {
61 if (!does_the_option_set_all_necessary_values(element)) {
62 cerr << FATALERRORL << "Trying to set " << YELLOWHHL << name << RSTHHR
63 << " but missing mandatory values." << endl;
64 cerr << " Use the option: " << YELLOWHHL << " help " << name
65 << " " << RSTHHR << " for details." << endl << endl;
67 }
68 }
69
70 // Store the full sequence exactly as provided by the user.
71 value[name] = v;
72
73 // Back-fill optional keys from the schema default sequence.
74 // The default schema is stored as a sequence of single-entry maps.
75 auto default_value_node = defaultValue.begin()->second;
76
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;
82
83 // For each user entry, ensure default_key exists; if not, assign schema default.
84 for (auto map_element_in_value : value[name]) {
85 bool key_found = false;
86
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) {
91 key_found = true;
92 break;
93 }
94 }
95
96 if (!key_found) {
97 map_element_in_value[default_key] = default_value;
98 }
99 }
100 }
101 }
102 }
103 else {
104 // Non-cumulative structured update:
105 // Iterate over desired values and update matching keys in the existing stored structure.
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>();
114
115 // Only update entries whose key matches the requested update key.
116 if (first_key == second_key) {
117 existing_map[existing_map_iterator->first] = desired_value_iterator->second;
118 }
119 }
120 }
121 }
122 }
123 }
124}
125
126// See goption.h for API docs.
127/*
128 * Implementation notes:
129 * - The input is expected to be one element of a cumulative sequence (typically a map).
130 * - We only check keys; type/shape constraints are not enforced here.
131 */
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>());
137 }
138 }
139
140 for (const auto& key : mandatory_keys) {
141 if (find(this_keys.begin(), this_keys.end(), key) == this_keys.end()) {
142 return false;
143 }
144 }
145 return true;
146}
147
148// See goption.h for API docs.
149/*
150 * Implementation notes:
151 * - Writes comment lines for nulls so the saved YAML explains which values were not provided.
152 * - Produces a cleaned copy of the node (nulls replaced) to keep output valid YAML scalars.
153 */
154void GOption::saveOption(std::ofstream* yamlConf) const {
155 std::vector<std::string> missing; // paths of null values
156
157 // --------------------------------------------------------------------
158 // recursive lambda: returns a *new* node with nulls → "not provided"
159 // --------------------------------------------------------------------
160 std::function<YAML::Node(YAML::Node, std::string)> clean =
161 [&](YAML::Node n, const std::string& path) -> YAML::Node {
162 if (n.IsNull()) {
163 missing.push_back(path.empty() ? name : path);
164 return YAML::Node("not provided"); // null replaced
165 }
166
167 if (n.IsMap()) {
168 YAML::Node res(YAML::NodeType::Map);
169 for (auto it : n) {
170 const std::string key = it.first.as<std::string>();
171 res[it.first] = clean(it.second, path.empty() ? key : path + "." + key);
172 }
173 return res;
174 }
175
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) + "]"));
180 }
181 return res;
182 }
183
184 return n; // scalar, already OK
185 };
186
187 YAML::Node out = clean(value, ""); // fully cleaned copy
188
189 // --------------------------------------------------------------------
190 // write one comment line per missing entry
191 // --------------------------------------------------------------------
192 for (const auto& p : missing) {
193 *yamlConf << "# " << p << " not provided\n";
194 }
195
196 // write the YAML itself (block style)
197 out.SetStyle(YAML::EmitterStyle::Block);
198 *yamlConf << out << '\n';
199}
200
201
202// See goption.h for API docs.
203/*
204 * Implementation notes:
205 * - Summary help is a single aligned line.
206 * - Detailed help includes schema defaults + extended multi-line help payload.
207 */
208void GOption::printHelp(bool detailed) const {
209 if (name == GVERSION_STRING) return;
210
211 long int fill_width = string(HELPFILLSPACE).size() + 1;
212 cout.fill('.');
213
214 string helpString = "-" + name + RST;
215 bool is_sequence = defaultValue.begin()->second.IsSequence();
216 helpString += is_sequence ? "=<sequence>" : "=<value>";
217 helpString += " ";
218
219 cout << KGRN << " " << left;
220 cout.width(fill_width);
221
222 if (detailed) {
223 cout << helpString << ": " << description << endl << endl;
224 cout << detailedHelp() << endl;
225 }
226 else {
227 cout << helpString << ": " << description << endl;
228 }
229}
230
231// See goption.h for API docs.
232/*
233 * Implementation notes:
234 * - If the default schema is a sequence, print each key with its per-key description and default value.
235 * - Then append the free-form help text, preserving user formatting.
236 */
237string GOption::detailedHelp() const {
238 string newHelp;
239 YAML::Node yvalues = defaultValue.begin()->second;
240
241 if (yvalues.IsSequence()) {
242 newHelp += "\n";
243
244 for (unsigned i = 0; i < yvalues.size(); i++) {
245 YAML::Node this_node = yvalues[i];
246
247 for (auto it = this_node.begin(); it != this_node.end(); ++it) {
248 cout << TGREENPOINTITEM << " " << KGRN << it->first.as<string>() << RST
249 << ": " << gvar_descs[i] << ". Default value: " << it->second.as<string>() << endl;
250 }
251 }
252 }
253
254 newHelp += "\n";
255 vector<string> help_lines = gutilities::getStringVectorFromStringWithDelimiter(help, "\n");
256 for (const auto& line : help_lines) {
257 newHelp += GTAB + line + "\n";
258 }
259
260 return newHelp;
261}
262
263// See goption.h for API docs.
264/*
265 * Implementation notes:
266 * - Dot-notation updates apply to existing structured storage.
267 * - If the stored node is a sequence, all map elements containing subkey are updated.
268 * - If the stored node is a map, only that entry is updated.
269 */
270void GOption::set_sub_option_value(const string& subkey, const string& subvalue) {
271 YAML::Node option_node = value.begin()->second;
272
273 if (option_node.IsSequence()) {
274 bool updated = false;
275
276 for (auto it = option_node.begin(); it != option_node.end(); ++it) {
277 // Only update entries that are maps and already contain subkey.
278 if ((*it).IsMap() && (*it)[subkey]) {
279 (*it)[subkey] = YAML::Load(subvalue);
280 updated = true;
281 }
282 }
283
284 if (!updated) {
285 cerr << "Sub-option key '" << subkey << "' not found in option '" << name << "'." << endl;
286 exit(EC__NOOPTIONFOUND);
287 }
288 }
289 else if (option_node.IsMap()) {
290 if (option_node[subkey]) {
291 option_node[subkey] = YAML::Load(subvalue);
292 }
293 else {
294 cerr << "Sub-option key '" << subkey << "' not found in option '" << name << "'." << endl;
295 exit(EC__NOOPTIONFOUND);
296 }
297 }
298 else {
299 cerr << "Option '" << name << "' is not structured to accept sub–options." << endl;
300 exit(EC__NOOPTIONFOUND);
301 }
302}
void set_sub_option_value(const std::string &subkey, const std::string &subvalue)
Updates a structured sub-option using dot-notation semantics.
Definition goption.cc:270
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.
#define RSTHHR
#define YELLOWHHL
#define TGREENPOINTITEM
#define GTAB
#define KGRN
#define FATALERRORL
#define RST
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
string replaceCharInStringWithChars(const std::string &input, const std::string &toReplace, const std::string &replacement)