goptions
Loading...
Searching...
No Matches
goptions.h
Go to the documentation of this file.
1#pragma once
2
3#include "goption.h"
4#include "gswitch.h"
5
6// gemc
7#include "gutsConventions.h"
8
9
10// c++
11#include <string>
12#include <fstream>
13#include <iostream>
14#include <map>
15#include <utility>
16#include <vector>
17
24class GOptions {
25public:
32 };
33
39 explicit GOptions(std::string name) : option_verbosity_name(name) { addOptionTitle(std::move(name)); }
40
51 GOptions(int argc, char* argv[], const GOptions& user_defined_options = GOptions());
52
53
55 if (yamlConf != nullptr) {
56 if (yamlConf->is_open()) { yamlConf->close(); }
57 delete yamlConf;
58 yamlConf = nullptr;
59 }
60 }
61
67 void defineSwitch(const std::string& name, const std::string& description);
68
74 void defineOption(const GVariable& gvar, const std::string& help);
75
83 void defineOption(const std::string& name, const std::string& description, const std::vector<GVariable>& gvars,
84 const std::string& help);
85
91 [[nodiscard]] int getScalarInt(const std::string& tag) const;
92
93
99 [[nodiscard]] double getScalarDouble(const std::string& tag) const;
100
106 [[nodiscard]] std::string getScalarString(const std::string& tag) const;
107
113 [[nodiscard]] bool getSwitch(const std::string& tag) const;
114
121 [[nodiscard]] inline YAML::Node getOptionNode(const std::string& tag) const {
122 if (!doesOptionExist(tag)) {
123 std::cerr << "Option " << tag << " does not exist. Exiting." << std::endl;
124 exit(EC__NOOPTIONFOUND);
125 }
126 return getOptionIterator(tag)->value.begin()->second;
127 }
128
135 [[nodiscard]] YAML::Node getOptionMapInNode(const std::string& option_name, const std::string& map_key) const;
136
142 [[nodiscard]] int getVerbosityFor(const std::string& tag) const;
143
152 [[nodiscard]] int getDebugFor(const std::string& tag) const;
153
158 [[nodiscard]] const std::vector<GOption>& getOptions() const { return goptions; }
159
164 [[nodiscard]] const std::map<std::string, GSwitch>& getSwitches() const { return switches; }
165
170 inline void addGOptions(const GOptions& src)
171 {
172 // 1. Options – check by option name
173 for (const auto& opt : src.getOptions()) {
174 auto already = std::find_if(
175 goptions.begin(), goptions.end(),
176 [&opt](const GOption& o) { return o.name == opt.name; });
177
178 if (already == goptions.end())
179 goptions.push_back(opt); // add only if absent
180 }
181
182 // 2. Switches – std::map::insert does the uniqueness check for us
183 for (const auto& sw : src.getSwitches()) {
184 switches.insert(sw); // ignored if key already exists
185 }
186
187 // 3. Verbosity/debug variable names – store each only once
188 for (const auto& v : src.option_verbosity_names) {
189 auto same = std::find_if(
191 [&v](const GVariable& existing) { return existing.name == v.name; });
192
193 if (same == option_verbosity_names.end())
194 option_verbosity_names.push_back(v);
195 }
196 }
197
198 std::string option_verbosity_name{UNINITIALIZEDSTRINGQUANTITY};
199 std::vector<GVariable> option_verbosity_names;
200
201 inline void addOptionTitle(const std::string& name) {
202 std::string option_verbosity_name_desc = name + " verbosity level or debug switch";
203 option_verbosity_names.emplace_back(name, 0, option_verbosity_name_desc);
204 }
205
214 template <typename T>
215 T get_variable_in_option(const YAML::Node& node, const std::string& variable_name, const T& default_value);
216
221 [[nodiscard]] std::vector<std::string> getYamlFiles() const { return yaml_files; }
222
228 [[nodiscard]] bool doesOptionExist(const std::string& tag) const;
229
230private:
231 std::vector<GOption> goptions;
232 std::map<std::string, GSwitch> switches;
233 std::ofstream* yamlConf{};
234 std::string executableName;
235 std::string executableCallingDir;
236 std::string installDir;
237 std::vector<std::string> yaml_files;
238
245 std::vector<std::string> findYamls(int argc, char* argv[]);
246
251 void setOptionsValuesFromYamlFile(const std::string& yaml);
252
258 void setOptionValuesFromCommandLineArgument(const std::string& optionName, const std::string& possibleYamlNode);
259
265 std::vector<GOption>::iterator getOptionIterator(const std::string& name);
266
272 [[nodiscard]] std::vector<GOption>::const_iterator getOptionIterator(const std::string& name) const;
273
278 void printOptionOrSwitchHelp(const std::string& tag) const;
279
283 void printHelp() const;
284
288 void printWebHelp() const;
289
293 void saveOptions() const;
294
298 void print_version();
299};
300
302GOptions& operator+=(GOptions& original, const GOptions& optionsToAdd);
Represents a configurable option with a name, value(s), description, and help text.
Definition goption.h:85
The GOptions class manages command-line options and switches.
Definition goptions.h:24
std::string option_verbosity_name
Definition goptions.h:198
std::string getScalarString(const std::string &tag) const
Retrieves the value of a scalar string option.
Definition goptions.cc:255
bool getSwitch(const std::string &tag) const
Retrieves the status of a switch.
Definition goptions.cc:409
const std::vector< GOption > & getOptions() const
Returns the list of defined options.
Definition goptions.h:158
YAML::Node getOptionNode(const std::string &tag) const
Retrieves the YAML node for the specified option.
Definition goptions.h:121
void addOptionTitle(const std::string &name)
Definition goptions.h:201
GOptions(std::string name)
Constructor for verbosity and debug options.
Definition goptions.h:39
~GOptions()
Definition goptions.h:54
void defineSwitch(const std::string &name, const std::string &description)
Defines and adds a command–line switch.
Definition goptions.cc:172
GOptions()
Default constructor.
Definition goptions.h:31
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option.
Definition goptions.cc:188
YAML::Node getOptionMapInNode(const std::string &option_name, const std::string &map_key) const
Retrieves a map option’s value from within a YAML node.
Definition goptions.cc:427
T get_variable_in_option(const YAML::Node &node, const std::string &variable_name, const T &default_value)
Retrieves a variable from a YAML node within an option.
Definition goptions.cc:451
double getScalarDouble(const std::string &tag) const
Retrieves the value of a scalar double option.
Definition goptions.cc:239
bool doesOptionExist(const std::string &tag) const
Checks if the specified option exists.
Definition goptions.cc:308
int getScalarInt(const std::string &tag) const
Retrieves the value of a scalar integer option.
Definition goptions.cc:223
std::vector< std::string > getYamlFiles() const
Retrieves the list of YAML file paths.
Definition goptions.h:221
void addGOptions(const GOptions &src)
Adds options from another GOptions object.
Definition goptions.h:170
const std::map< std::string, GSwitch > & getSwitches() const
Returns the map of defined switches.
Definition goptions.h:164
int getDebugFor(const std::string &tag) const
Retrieves the debug level for the specified tag.
Definition goptions.cc:491
int getVerbosityFor(const std::string &tag) const
Retrieves the verbosity level for the specified tag.
Definition goptions.cc:470
std::vector< GVariable > option_verbosity_names
Definition goptions.h:199
#define EC__NOOPTIONFOUND
GOptions & operator+=(GOptions &original, const GOptions &optionsToAdd)
Overloaded operator to add options and switches from one GOptions object to another.
Definition goptions.cc:604
Encapsulates a variable with a name, value, and description.
Definition goption.h:24