39void GOption::set_scalar_value(
const string& v) {
44 auto key = value.begin()->first.as<
string>();
45 value[key] = value_to_set;
55void GOption::set_value(
const YAML::Node& v) {
58 for (
const auto& element : v) {
59 if (!does_the_option_set_all_necessary_values(element)) {
61 <<
" but missing mandatory values." << endl;
63 <<
" " <<
guts::RSTHHR <<
" for details." << endl << endl;
73 auto default_value_node = defaultValue.begin()->second;
75 for (
const auto& map_element_in_default_value : default_value_node) {
76 for (
auto default_value_iterator = map_element_in_default_value.begin();
77 default_value_iterator != map_element_in_default_value.end(); ++default_value_iterator) {
78 auto default_key = default_value_iterator->first.as<
string>();
79 auto default_value = default_value_iterator->second;
82 for (
auto map_element_in_value : value[name]) {
83 bool key_found =
false;
85 for (
auto value_iterator = map_element_in_value.begin();
86 value_iterator != map_element_in_value.end(); ++value_iterator) {
87 auto value_key = value_iterator->first.as<
string>();
88 if (default_key == value_key) {
95 map_element_in_value[default_key] = default_value;
104 const auto update_existing_value = [
this](
const YAML::Node& desired_key,
const YAML::Node& desired_value) {
105 for (
auto existing_map : value[name]) {
106 for (
auto existing_map_iterator = existing_map.begin();
107 existing_map_iterator != existing_map.end(); ++existing_map_iterator) {
108 auto first_key = existing_map_iterator->first.as<
string>();
109 auto second_key = desired_key.as<
string>();
112 if (first_key == second_key) {
113 existing_map[existing_map_iterator->first] = desired_value;
120 for (
auto desired_value_iterator = v.begin();
121 desired_value_iterator != v.end(); ++desired_value_iterator) {
122 update_existing_value(desired_value_iterator->first, desired_value_iterator->second);
126 for (
const auto& map_element_in_desired_value : v) {
127 for (
auto desired_value_iterator = map_element_in_desired_value.begin();
128 desired_value_iterator != map_element_in_desired_value.end(); ++desired_value_iterator) {
129 update_existing_value(desired_value_iterator->first, desired_value_iterator->second);
142bool GOption::does_the_option_set_all_necessary_values(
const YAML::Node& v) {
143 for (
const auto& key : mandatory_keys) {
144 if (!v[key] || v[key].IsNull())
return false;
155void GOption::saveOption(std::ofstream* yamlConf)
const {
156 std::vector<std::string> missing;
161 std::function<YAML::Node(YAML::Node, std::string)> clean =
162 [&](YAML::Node n,
const std::string& path) -> YAML::Node {
164 missing.push_back(path.empty() ? name : path);
165 return YAML::Node(
"not provided");
169 YAML::Node res(YAML::NodeType::Map);
171 const std::string key = it.first.as<std::string>();
172 res[it.first] = clean(it.second, path.empty() ? key : path +
"." + key);
177 if (n.IsSequence()) {
178 YAML::Node res(YAML::NodeType::Sequence);
179 for (std::size_t i = 0; i < n.size(); ++i) {
180 res.push_back(clean(n[i], path +
"[" + std::to_string(i) +
"]"));
188 YAML::Node out = clean(value,
"");
193 for (
const auto& p : missing) {
194 *yamlConf <<
"# " << p <<
" not provided\n";
198 out.SetStyle(YAML::EmitterStyle::Block);
199 *yamlConf << out <<
'\n';
209void GOption::printHelp(
bool detailed)
const {
215 string helpString =
"-" + name +
guts::RST;
216 bool is_sequence = defaultValue.begin()->second.IsSequence();
217 helpString += is_sequence ?
"=<sequence>" :
"=<value>";
221 cout.width(fill_width);
224 cout << helpString <<
": " << description << endl << endl;
225 cout << detailedHelp() << endl;
228 cout << helpString <<
": " << description << endl;
238string GOption::detailedHelp()
const {
240 YAML::Node yvalues = defaultValue.begin()->second;
242 if (yvalues.IsSequence()) {
245 for (
unsigned i = 0; i < yvalues.size(); i++) {
246 YAML::Node this_node = yvalues[i];
248 for (
auto it = this_node.begin(); it != this_node.end(); ++it) {
249 const std::string defaultDescription = gvar_required[i]
251 : (it->second.IsNull() ?
"not set" : YAML::Dump(it->second));
253 <<
": " << gvar_descs[i] <<
"Default value: " << defaultDescription << endl;
260 for (
const auto& line : help_lines) {
275 YAML::Node option_node = value.begin()->second;
277 if (option_node.IsSequence()) {
278 bool updated =
false;
280 for (
auto it = option_node.begin(); it != option_node.end(); ++it) {
282 if ((*it).IsMap() && (*it)[subkey]) {
283 (*it)[subkey] = YAML::Load(subvalue);
289 cerr <<
"Sub-option key '" << subkey <<
"' not found in option '" << name <<
"'." << endl;
293 else if (option_node.IsMap()) {
294 if (option_node[subkey]) {
295 option_node[subkey] = YAML::Load(subvalue);
298 cerr <<
"Sub-option key '" << subkey <<
"' not found in option '" << name <<
"'." << endl;
303 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.
constexpr int EC__NOOPTIONFOUND
Option/switch/key not found, or invalid command-line token.
constexpr int EC__MANDATORY_NOT_FILLED
Mandatory structured option key missing.
constexpr char GVERSION_STRING[]
Reserved option tag used to store version information.
constexpr char HELPFILLSPACE[]
Padding used when printing option/switch help.
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)
string replaceCharInStringWithChars(const std::string &input, const std::string &toReplace, const std::string &replacement)
constexpr char TGREENPOINTITEM[]
constexpr char YELLOWHHL[]
constexpr char FATALERRORL[]