goptions
Loading...
Searching...
No Matches
goption.h
Go to the documentation of this file.
1
5
6#pragma once
7
8// goption
10
11// c++
12#include <map>
13#include <fstream>
14#include <optional>
15#include <vector>
16#include <string>
17#include <algorithm>
18
19// yaml-cpp
20#include "yaml-cpp/yaml.h"
21
22
35{
36 std::string name;
37 YAML::Node defaultValue;
38 bool required{};
39 std::string description;
40
47 GVariable(std::string n, std::string val, std::string d)
48 : name(std::move(n)), defaultValue(std::move(val)), description(std::move(d)) {
49 }
50
57 GVariable(std::string n, double val, std::string d)
58 : name(std::move(n)), defaultValue(val), description(std::move(d)) {
59 }
60
67 GVariable(std::string n, const char* val, std::string d)
68 : name(std::move(n)), defaultValue(val), description(std::move(d)) {
69 }
70
77 GVariable(std::string n, int val, std::string d)
78 : name(std::move(n)), defaultValue(val), description(std::move(d)) {
79 }
80
87 GVariable(std::string n, bool val, std::string d)
88 : name(std::move(n)), defaultValue(val), description(std::move(d)) {
89 }
90
92 GVariable(std::string n, std::nullopt_t, std::string d)
93 : name(std::move(n)), defaultValue(YAML::NodeType::Null), description(std::move(d)) {
94 }
95
97 GVariable(std::string n, goptions::RequiredValue, std::string d)
98 : name(std::move(n)), defaultValue(YAML::NodeType::Null), required(true), description(std::move(d)) {
99 }
100};
101
120{
121public:
138 GOption(GVariable dv, std::string h) : name(dv.name), description(dv.description), help(h) {
139 defaultValue[name] = dv.defaultValue;
140 value = defaultValue;
141 }
142
168 GOption(std::string n, std::string desc, std::vector<GVariable> dv, std::string h)
169 : name(n), description(desc), help(h) {
170 YAML::Node nodes;
171 for (const auto& v : dv) {
172 YAML::Node this_node;
173 this_node[v.name] = v.defaultValue;
174 nodes.push_back(this_node);
175 gvar_descs.push_back(v.description);
176 gvar_required.push_back(v.required);
177 if (v.required) {
178 isCumulative = true;
179 mandatory_keys.push_back(v.name);
180 }
181 }
182 defaultValue[n] = nodes;
183 if (!isCumulative) { value = defaultValue; }
184 }
185
206 void set_sub_option_value(const std::string& subkey, const std::string& subvalue);
207
208private:
209 bool isCumulative = false;
210 const std::string name;
211 const std::string description;
212 const std::string help;
213
214 YAML::Node value;
215 YAML::Node defaultValue;
216 std::vector<std::string> gvar_descs;
217 std::vector<bool> gvar_required;
218 std::vector<std::string> mandatory_keys;
219
232 void saveOption(std::ofstream* yamlConf) const;
233
246 void printHelp(bool detailed) const;
247
259 std::string detailedHelp() const;
260
272 void set_scalar_value(const std::string& v);
273
290 void set_value(const YAML::Node& v);
291
302 bool does_the_option_set_all_necessary_values(const YAML::Node& v);
303
304 friend class GOptions;
305};
GOption(GVariable dv, std::string h)
Constructor for a scalar option with a default value.
Definition goption.h:138
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:168
friend class GOptions
Grants GOptions : access to private members for parsing and serialization.
Definition goption.h:304
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:274
Conventions, constants, and error codes for the GOptions : / GOption : subsystem.
Describes a schema entry: key name, default value, and user-facing description.
Definition goption.h:35
GVariable(std::string n, goptions::RequiredValue, std::string d)
Definition goption.h:97
GVariable(std::string n, const char *val, std::string d)
Constructor for initializing a variable with a C-string default.
Definition goption.h:67
GVariable(std::string n, std::string val, std::string d)
Constructor for initializing a variable with a string default.
Definition goption.h:47
std::string name
Variable name (option name for scalar options, schema key name for structured options).
Definition goption.h:36
YAML::Node defaultValue
Typed default; null for optional-null and required fields.
Definition goption.h:37
bool required
Whether a structured field must be supplied by the user.
Definition goption.h:38
std::string description
Human-readable description used in help output.
Definition goption.h:39
GVariable(std::string n, bool val, std::string d)
Constructor for initializing a variable with a boolean default.
Definition goption.h:87
GVariable(std::string n, std::nullopt_t, std::string d)
Definition goption.h:92
GVariable(std::string n, double val, std::string d)
Constructor for initializing a variable with a double default.
Definition goption.h:57
GVariable(std::string n, int val, std::string d)
Constructor for initializing a variable with an integer default.
Definition goption.h:77