goptions
Loading...
Searching...
No Matches
goptions.h
Go to the documentation of this file.
1
6#pragma once
7
8#include "goption.h"
9#include "gswitch.h"
10
11// gemc
12#include "gutsConventions.h"
13
14
15// c++
16#include <string>
17#include <fstream>
18#include <iostream>
19#include <map>
20#include <utility>
21#include <vector>
22
46{
47public:
57 };
58
69 explicit GOptions(std::string name) : option_verbosity_name(name) { addOptionTitle(std::move(name)); }
70
86 GOptions(int argc, char* argv[], const GOptions& user_defined_options = GOptions());
87
88
97 if (yamlConf != nullptr) {
98 if (yamlConf->is_open()) { yamlConf->close(); }
99 delete yamlConf;
100 yamlConf = nullptr;
101 }
102 }
103
118 void defineSwitch(const std::string& name, const std::string& description);
119
130 void defineOption(const GVariable& gvar, const std::string& help);
131
145 void defineOption(const std::string& name, const std::string& description, const std::vector<GVariable>& gvars,
146 const std::string& help);
147
157 [[nodiscard]] int getScalarInt(const std::string& tag) const;
158
159
166 [[nodiscard]] double getScalarDouble(const std::string& tag) const;
167
179 [[nodiscard]] std::string getScalarString(const std::string& tag) const;
180
187 [[nodiscard]] bool getSwitch(const std::string& tag) const;
188
206 [[nodiscard]] inline YAML::Node getOptionNode(const std::string& tag) const {
207 if (!doesOptionExist(tag)) {
208 std::cerr << "Option " << tag << " does not exist. Exiting." << std::endl;
210 }
211 return getOptionIterator(tag)->value.begin()->second;
212 }
213
228 [[nodiscard]] YAML::Node getOptionMapInNode(const std::string& option_name, const std::string& map_key) const;
229
240 [[nodiscard]] int getVerbosityFor(const std::string& tag) const;
241
253 [[nodiscard]] int getDebugFor(const std::string& tag) const;
254
260 [[nodiscard]] const std::vector<GOption>& getOptions() const { return goptions; }
261
267 [[nodiscard]] const std::map<std::string, GSwitch>& getSwitches() const { return switches; }
268
286 inline void addGOptions(const GOptions& src) {
287 // 1. Options – check by option name
288 for (const auto& opt : src.getOptions()) {
289 auto already = std::find_if(
290 goptions.begin(), goptions.end(),
291 [&opt](const GOption& o) { return o.name == opt.name; });
292
293 if (already == goptions.end())
294 goptions.push_back(opt); // add only if absent
295 }
296
297 // 2. Switches – std::map::insert does the uniqueness check for us
298 for (const auto& sw : src.getSwitches()) {
299 switches.insert(sw); // ignored if key already exists
300 }
301
302 // 3. Verbosity/debug variable names – store each only once
303 for (const auto& v : src.option_verbosity_names) {
304 auto same = std::find_if(
306 [&v](const GVariable& existing) { return existing.name == v.name; });
307
308 if (same == option_verbosity_names.end())
309 option_verbosity_names.push_back(v);
310 }
311 }
312
321
329 std::vector<GVariable> option_verbosity_names;
330
331 inline void addOptionTitle(const std::string& name) {
332 std::string option_verbosity_name_desc = name + " verbosity level or debug switch";
334 }
335
349 template <typename T>
350 T get_variable_in_option(const YAML::Node& node, const std::string& variable_name, const T& default_value);
351
362 [[nodiscard]] std::vector<std::string> getYamlFiles() const { return yaml_files; }
363
370 [[nodiscard]] bool doesOptionExist(const std::string& tag) const;
371
372private:
373 std::vector<GOption> goptions;
374 std::map<std::string, GSwitch> switches;
375 std::ofstream* yamlConf{};
376 std::string executableName;
377 std::string executableCallingDir;
379 std::string installDir;
380 std::vector<std::string> yaml_files;
381
382 std::vector<std::string> findYamls(int argc, char* argv[]);
383 void setOptionsValuesFromYamlFile(const std::string& yaml);
384 void setOptionValuesFromCommandLineArgument(const std::string& optionName, const std::string& possibleYamlNode);
385 std::vector<GOption>::iterator getOptionIterator(const std::string& name);
386 [[nodiscard]] std::vector<GOption>::const_iterator getOptionIterator(const std::string& name) const;
387 void printOptionOrSwitchHelp(const std::string& tag) const;
388 void printHelp() const;
389 void printWebHelp() const;
390 void saveOptions() const;
391 void print_version();
392};
393
395GOptions& operator+=(GOptions& original, const GOptions& optionsToAdd);
Stores one configuration option (scalar or structured), including schema defaults and current value.
Definition goption.h:105
Parses, stores, and exposes command-line options and YAML configuration values.
Definition goptions.h:46
std::string option_verbosity_name
Name used when constructing the verbosity/debug schema helper.
Definition goptions.h:320
bool getSwitch(const std::string &tag) const
Retrieves the status of a switch.
Definition goptions.cc:382
const std::vector< GOption > & getOptions() const
Returns the list of defined options.
Definition goptions.h:260
YAML::Node getOptionNode(const std::string &tag) const
Retrieves the YAML node for the specified option.
Definition goptions.h:206
void addOptionTitle(const std::string &name)
Definition goptions.h:331
GOptions(std::string name)
Constructor for creating verbosity/debug schema helpers.
Definition goptions.h:69
~GOptions()
Destructor.
Definition goptions.h:96
void defineSwitch(const std::string &name, const std::string &description)
Defines and adds a command-line switch.
Definition goptions.cc:192
std::string getScalarString(const std::string &tag) const
Retrieves the value of a scalar string option.
Definition goptions.cc:252
GOptions()
Default constructor.
Definition goptions.h:56
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option.
Definition goptions.cc:204
YAML::Node getOptionMapInNode(const std::string &option_name, const std::string &map_key) const
Retrieves a map entry value from a structured option stored as a sequence of maps.
Definition goptions.cc:395
T get_variable_in_option(const YAML::Node &node, const std::string &variable_name, const T &default_value)
Retrieves a typed variable from a YAML node within an option.
Definition goptions.cc:413
double getScalarDouble(const std::string &tag) const
Retrieves the value of a scalar double option.
Definition goptions.cc:241
bool doesOptionExist(const std::string &tag) const
Checks if an option exists.
Definition goptions.cc:299
int getScalarInt(const std::string &tag) const
Retrieves the value of a scalar integer option.
Definition goptions.cc:230
std::vector< std::string > getYamlFiles() const
Returns the list of YAML file paths detected on the command line.
Definition goptions.h:362
void addGOptions(const GOptions &src)
Merges options and switches from another GOptions : into this one.
Definition goptions.h:286
const std::map< std::string, GSwitch > & getSwitches() const
Returns the map of defined switches.
Definition goptions.h:267
int getDebugFor(const std::string &tag) const
Retrieves the debug level for the specified tag.
Definition goptions.cc:445
int getVerbosityFor(const std::string &tag) const
Retrieves the verbosity level for the specified tag.
Definition goptions.cc:431
std::vector< GVariable > option_verbosity_names
Schema entries used to define the verbosity and debug structured options.
Definition goptions.h:329
Definitions of GVariable : and GOption : used by GOptions : .
#define EC__NOOPTIONFOUND
Option/switch/key not found, or invalid command-line token.
GOptions & operator+=(GOptions &original, const GOptions &optionsToAdd)
Overloaded operator to add options and switches from one GOptions : to another.
Definition goptions.cc:544
Definition of GSwitch : the boolean command-line switch type used by GOptions : .
#define UNINITIALIZEDSTRINGQUANTITY
Describes a schema entry: key name, default value, and user-facing description.
Definition goption.h:34