goptions
Loading...
Searching...
No Matches
goption.h
Go to the documentation of this file.
1
6#pragma once
7
8// goption
10
11// c++
12#include <map>
13#include <fstream>
14#include <vector>
15#include <string>
16#include <algorithm>
17
18// yaml-cpp
19#include "yaml-cpp/yaml.h"
20
21
34{
35 std::string name;
36 std::string value;
37 std::string description;
38
45 GVariable(std::string n, std::string val, std::string d)
46 : name(std::move(n)), value(std::move(val)), description(std::move(d)) {
47 }
48
55 GVariable(std::string n, double val, std::string d)
56 : name(std::move(n)), description(std::move(d)) { value = std::to_string(val); }
57
64 GVariable(std::string n, const char* val, std::string d)
65 : name(std::move(n)), value(val), description(std::move(d)) {
66 }
67
74 GVariable(std::string n, int val, std::string d)
75 : name(std::move(n)), description(std::move(d)) { value = std::to_string(val); }
76
83 GVariable(std::string n, bool val, std::string d)
84 : name(std::move(n)), description(std::move(d)) { value = val ? "true" : "false"; }
85};
86
105{
106public:
123 GOption(GVariable dv, std::string h) : name(dv.name), description(dv.description), help(h) {
124 defaultValue = YAML::Load(name + ": " + dv.value);
125 value = defaultValue;
126 }
127
153 GOption(std::string n, std::string desc, std::vector<GVariable> dv, std::string h)
154 : name(n), description(desc), help(h) {
155 YAML::Node nodes;
156 for (const auto& v : dv) {
157 YAML::Node this_node = YAML::Load(v.name + ": " + v.value);
158 nodes.push_back(this_node);
159 gvar_descs.push_back(v.description);
160 if (v.value == goptions::NODFLT) {
161 isCumulative = true;
162 mandatory_keys.push_back(v.name);
163 }
164 }
165 defaultValue[n] = nodes;
166 if (!isCumulative) { value = defaultValue; }
167 }
168
188 void set_sub_option_value(const std::string& subkey, const std::string& subvalue);
189
190private:
191 bool isCumulative = false;
192 const std::string name;
193 const std::string description;
194 const std::string help;
195
196 YAML::Node value;
197 YAML::Node defaultValue;
198 std::vector<std::string> gvar_descs;
199 std::vector<std::string> mandatory_keys;
200
213 void saveOption(std::ofstream* yamlConf) const;
214
227 void printHelp(bool detailed) const;
228
240 std::string detailedHelp() const;
241
253 void set_scalar_value(const std::string& v);
254
271 void set_value(const YAML::Node& v);
272
283 bool does_the_option_set_all_necessary_values(const YAML::Node& v);
284
285 friend class GOptions;
286};
Stores one configuration option (scalar or structured), including schema defaults and current value.
Definition goption.h:105
GOption(GVariable dv, std::string h)
Constructor for a scalar option with a default value.
Definition goption.h:123
GOption(std::string n, std::string desc, std::vector< GVariable > dv, std::string h)
Constructor for a structured option schema (map/sequence).
Definition goption.h:153
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
Parses, stores, and exposes command-line options and YAML configuration values.
Definition goptions.h:46
Conventions, constants, and error codes for the GOptions : / GOption : subsystem.
const std::string NODFLT
Marker literal indicating "no default value" for a structured option key.
Describes a schema entry: key name, default value, and user-facing description.
Definition goption.h:34
GVariable(std::string n, const char *val, std::string d)
Constructor for initializing a variable with a C-string default.
Definition goption.h:64
GVariable(std::string n, std::string val, std::string d)
Constructor for initializing a variable with a string default.
Definition goption.h:45
std::string name
Variable name (option name for scalar options, schema key name for structured options).
Definition goption.h:35
std::string value
Default value as a string (or goptions::NODFLT : to mark as mandatory).
Definition goption.h:36
std::string description
Human-readable description used in help output.
Definition goption.h:37
GVariable(std::string n, bool val, std::string d)
Constructor for initializing a variable with a boolean default.
Definition goption.h:83
GVariable(std::string n, double val, std::string d)
Constructor for initializing a variable with a double default.
Definition goption.h:55
GVariable(std::string n, int val, std::string d)
Constructor for initializing a variable with an integer default.
Definition goption.h:74