goptions
Loading...
Searching...
No Matches
goptions.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "goption.h"
9#include "gswitch.h"
10
11// gemc
12#include <gemc/guts/gutsConventions.h>
13
14
15// c++
16#include <string>
17#include <fstream>
18#include <iostream>
19#include <map>
20#include <optional>
21#include <utility>
22#include <vector>
23
47{
48public:
58 };
59
70 explicit GOptions(std::string name) { addOptionTitle(std::move(name)); }
71
87 GOptions(int argc, char* argv[], const GOptions& user_defined_options = GOptions());
88
89
98 if (yamlConf != nullptr) {
99 if (yamlConf->is_open()) { yamlConf->close(); }
100 delete yamlConf;
101 yamlConf = nullptr;
102 }
103 }
104
121 void defineSwitch(const std::string& name, const std::string& description, bool default_status = false);
122
133 void defineOption(const GVariable& gvar, const std::string& help);
134
148 void defineOption(const std::string& name, const std::string& description, const std::vector<GVariable>& gvars,
149 const std::string& help);
150
160 [[nodiscard]] int getRequiredScalarInt(const std::string& tag) const;
161 [[nodiscard]] std::optional<int> getOptionalScalarInt(const std::string& tag) const;
162
163
170 [[nodiscard]] double getRequiredScalarDouble(const std::string& tag) const;
171 [[nodiscard]] std::optional<double> getOptionalScalarDouble(const std::string& tag) const;
172
182 [[nodiscard]] std::string getRequiredScalarString(const std::string& tag) const;
183
194 [[nodiscard]] std::optional<std::string> getOptionalScalarString(const std::string& tag) const;
195
202 [[nodiscard]] bool getSwitch(const std::string& tag) const;
203
221 [[nodiscard]] inline YAML::Node getOptionNode(const std::string& tag) const {
222 if (!doesOptionExist(tag)) {
223 std::cerr << "Option " << tag << " does not exist. Exiting." << std::endl;
225 }
226 const auto& option_value = getOptionIterator(tag)->value;
227 if (!option_value || option_value.size() == 0) { return YAML::Node{}; }
228 return option_value.begin()->second;
229 }
230
245 [[nodiscard]] YAML::Node getOptionMapInNode(const std::string& option_name, const std::string& map_key) const;
246
257 void setOptionValueFromString(const std::string& optionName, const std::string& possibleYamlNode);
258
269 [[nodiscard]] int getVerbosityFor(const std::string& tag) const;
270
282 [[nodiscard]] int getDebugFor(const std::string& tag) const;
283
289 [[nodiscard]] const std::vector<GOption>& getOptions() const { return goptions; }
290
296 [[nodiscard]] const std::map<std::string, GSwitch>& getSwitches() const { return switches; }
297
315 inline void addGOptions(const GOptions& src) {
316 // 1. Options – check by option name
317 for (const auto& opt : src.getOptions()) {
318 auto already = std::find_if(
319 goptions.begin(), goptions.end(),
320 [&opt](const GOption& o) { return o.name == opt.name; });
321
322 if (already == goptions.end())
323 goptions.push_back(opt); // add only if absent
324 }
325
326 // 2. Switches – std::map::insert does the uniqueness check for us
327 for (const auto& sw : src.getSwitches()) {
328 switches.insert(sw); // ignored if key already exists
329 }
330
331 // 3. Verbosity/debug variable names – store each only once
332 for (const auto& v : src.option_verbosity_names) {
333 auto same = std::find_if(
335 [&v](const GVariable& existing) { return existing.name == v.name; });
336
337 if (same == option_verbosity_names.end())
338 option_verbosity_names.push_back(v);
339 }
340 }
341
349 std::vector<GVariable> option_verbosity_names;
350
351 inline void addOptionTitle(const std::string& name) {
352 std::string option_verbosity_name_desc = name + " verbosity level or debug switch";
353 option_verbosity_names.emplace_back(name, 0, option_verbosity_name_desc);
354 }
355
369 template <typename T>
370 T get_variable_in_option(const YAML::Node& node, const std::string& variable_name, const T& default_value);
371
373 template <typename T>
374 T get_required_variable_in_option(const YAML::Node& node, const std::string& variable_name);
375
377 template <typename T>
378 std::optional<T> get_optional_variable_in_option(const YAML::Node& node, const std::string& variable_name);
379
390 [[nodiscard]] std::vector<std::string> getYamlFiles() const { return yaml_files; }
391
398 [[nodiscard]] bool doesOptionExist(const std::string& tag) const;
399
400private:
401 std::vector<GOption> goptions;
402 std::map<std::string, GSwitch> switches;
403 std::ofstream* yamlConf{};
404 std::string executableName;
405 std::string executableCallingDir;
407 std::string installDir;
408 std::vector<std::string> yaml_files;
409
410 std::vector<std::string> findYamls(int argc, char* argv[]);
411 void setOptionsValuesFromYamlFile(const std::string& yaml);
412 void setOptionValuesFromCommandLineArgument(const std::string& optionName, const std::string& possibleYamlNode);
413 std::vector<GOption>::iterator getOptionIterator(const std::string& name);
414 [[nodiscard]] std::vector<GOption>::const_iterator getOptionIterator(const std::string& name) const;
415 void printOptionOrSwitchHelp(const std::string& tag) const;
416 void printSearch(const std::string& tag) const;
417 void printHelp() const;
418 void printWebHelp() const;
419 void saveOptions() const;
420 void print_version();
421};
422
424GOptions& operator+=(GOptions& original, const GOptions& optionsToAdd);
Stores one configuration option (scalar or structured), including schema defaults and current value.
Definition goption.h:120
Parses, stores, and exposes command-line options and YAML configuration values.
Definition goptions.h:47
bool getSwitch(const std::string &tag) const
Retrieves the status of a switch.
Definition goptions.cc:582
const std::vector< GOption > & getOptions() const
Returns the list of defined options.
Definition goptions.h:289
double getRequiredScalarDouble(const std::string &tag) const
Retrieves the required value of a scalar double option.
Definition goptions.cc:340
YAML::Node getOptionNode(const std::string &tag) const
Retrieves the YAML node for the specified option.
Definition goptions.h:221
void addOptionTitle(const std::string &name)
Definition goptions.h:351
GOptions(std::string name)
Constructor for creating verbosity/debug schema helpers.
Definition goptions.h:70
void setOptionValueFromString(const std::string &optionName, const std::string &possibleYamlNode)
Updates an option value from a YAML-formatted string.
Definition goptions.cc:564
~GOptions()
Destructor.
Definition goptions.h:97
GOptions()
Default constructor.
Definition goptions.h:57
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option.
Definition goptions.cc:285
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:595
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:613
bool doesOptionExist(const std::string &tag) const
Checks if an option exists.
Definition goptions.cc:476
std::vector< std::string > getYamlFiles() const
Returns the list of YAML file paths detected on the command line.
Definition goptions.h:390
void addGOptions(const GOptions &src)
Merges options and switches from another GOptions : into this one.
Definition goptions.h:315
std::optional< std::string > getOptionalScalarString(const std::string &tag) const
Retrieves the optional value of a scalar string option.
Definition goptions.cc:385
std::optional< double > getOptionalScalarDouble(const std::string &tag) const
Definition goptions.cc:356
const std::map< std::string, GSwitch > & getSwitches() const
Returns the map of defined switches.
Definition goptions.h:296
std::optional< T > get_optional_variable_in_option(const YAML::Node &node, const std::string &variable_name)
Definition goptions.cc:631
std::string getRequiredScalarString(const std::string &tag) const
Retrieves the required value of a scalar string option.
Definition goptions.cc:368
int getDebugFor(const std::string &tag) const
Retrieves the debug level for the specified tag.
Definition goptions.cc:672
std::optional< int > getOptionalScalarInt(const std::string &tag) const
Definition goptions.cc:327
int getVerbosityFor(const std::string &tag) const
Retrieves the verbosity level for the specified tag.
Definition goptions.cc:658
T get_required_variable_in_option(const YAML::Node &node, const std::string &variable_name)
Definition goptions.cc:621
void defineSwitch(const std::string &name, const std::string &description, bool default_status=false)
Defines and adds a command-line switch.
Definition goptions.cc:273
std::vector< GVariable > option_verbosity_names
Schema entries used to define the verbosity and debug structured options.
Definition goptions.h:349
int getRequiredScalarInt(const std::string &tag) const
Retrieves the required value of a scalar integer option.
Definition goptions.cc:311
Definitions of GVariable : and GOption : used by GOptions : .
GOptions & operator+=(GOptions &original, const GOptions &optionsToAdd)
Overloaded operator to add options and switches from one GOptions : to another.
Definition goptions.cc:791
Definition of GSwitch : the boolean command-line switch type used by GOptions : .
constexpr int EC__NOOPTIONFOUND
Option/switch/key not found, or invalid command-line token.
Describes a schema entry: key name, default value, and user-facing description.
Definition goption.h:35