goptions
Loading...
Searching...
No Matches
goption.h
Go to the documentation of this file.
1#pragma once
2
3// goption
5
6// c++
7#include <map>
8#include <fstream>
9#include <vector>
10#include <string>
11#include <algorithm>
12
13// yaml-cpp
14#include "yaml-cpp/yaml.h"
15
16
24struct GVariable {
25 std::string name;
26 std::string value;
27 std::string description;
28
35 GVariable(std::string n, std::string val, std::string d)
36 : name(std::move(n)), value(std::move(val)), description(std::move(d)) {
37 }
38
45 GVariable(std::string n, double val, std::string d)
46 : name(std::move(n)), description(std::move(d)) { value = std::to_string(val); }
47
54 GVariable(std::string n, const char* val, std::string d)
55 : name(std::move(n)), value(val), description(std::move(d)) {
56 }
57
64 GVariable(std::string n, int val, std::string d)
65 : name(std::move(n)), description(std::move(d)) { value = std::to_string(val); }
66
73 GVariable(std::string n, bool val, std::string d)
74 : name(std::move(n)), description(std::move(d)) { value = val ? "true" : "false"; }
75};
76
85class GOption {
86public:
92 GOption(GVariable dv, std::string h) : name(dv.name), description(dv.description), help(h) {
93 defaultValue = YAML::Load(name + ": " + dv.value);
94 value = defaultValue;
95 }
96
104 GOption(std::string n, std::string desc, std::vector<GVariable> dv, std::string h)
105 : name(n), description(desc), help(h) {
106 YAML::Node nodes;
107 for (const auto& v : dv) {
108 YAML::Node this_node = YAML::Load(v.name + ": " + v.value);
109 nodes.push_back(this_node);
110 gvar_descs.push_back(v.description);
111 if (v.value == goptions::NODFLT) {
112 isCumulative = true;
113 mandatory_keys.push_back(v.name);
114 }
115 }
116 defaultValue[n] = nodes;
117 if (!isCumulative) { value = defaultValue; }
118 }
119
129 void set_sub_option_value(const std::string& subkey, const std::string& subvalue);
130
131private:
132 bool isCumulative = false;
133 const std::string name;
134 const std::string description;
135 const std::string help;
136
137 YAML::Node value;
138 YAML::Node defaultValue;
139 std::vector<std::string> gvar_descs;
140 std::vector<std::string> mandatory_keys;
141
146 void saveOption(std::ofstream* yamlConf) const;
147
152 void printHelp(bool detailed) const;
153
158 std::string detailedHelp() const;
159
164 void set_scalar_value(const std::string& v);
165
170 void set_value(const YAML::Node& v);
171
177 bool does_the_option_set_all_necessary_values(const YAML::Node& v);
178
179 friend class GOptions;
180};
Represents a configurable option with a name, value(s), description, and help text.
Definition goption.h:85
GOption(GVariable dv, std::string h)
Constructor for a scalar option with a default value.
Definition goption.h:92
GOption(std::string n, std::string desc, std::vector< GVariable > dv, std::string h)
Constructor for a sequence option.
Definition goption.h:104
void set_sub_option_value(const std::string &subkey, const std::string &subvalue)
Sets the value of a sub–option using dot–notation.
Definition goption.cc:229
The GOptions class manages command-line options and switches.
Definition goptions.h:24
const std::string NODFLT
Encapsulates a variable with a name, value, and description.
Definition goption.h:24
GVariable(std::string n, const char *val, std::string d)
Constructor for initializing a variable with a const char* value.
Definition goption.h:54
GVariable(std::string n, std::string val, std::string d)
Constructor for initializing a variable with a string value.
Definition goption.h:35
std::string name
The name of the variable.
Definition goption.h:25
std::string value
The value of the variable, stored as a string.
Definition goption.h:26
std::string description
A brief description of the variable.
Definition goption.h:27
GVariable(std::string n, bool val, std::string d)
Constructor for initializing a variable with a boolean value.
Definition goption.h:73
GVariable(std::string n, double val, std::string d)
Constructor for initializing a variable with a double value.
Definition goption.h:45
GVariable(std::string n, int val, std::string d)
Constructor for initializing a variable with an integer value.
Definition goption.h:64