goptions
goptions.h
Go to the documentation of this file.
1 #ifndef GOPTIONS_H
2 #define GOPTIONS_H 1
3 
4 #include "goption.h"
5 #include "gswitch.h"
6 
7 // c++
8 #include <string>
9 #include <fstream>
10 #include <iostream>
11 #include <map>
12 #include <vector>
13 
20 class GOptions {
21 public:
27  GOptions() { return; }
28 
34  GOptions(string name) : option_verbosity_name(name) { addOptionTitle(name); }
35 
46  GOptions(int argc, char *argv[], const GOptions &user_defined_options = GOptions());
47 
53  void defineSwitch(const string &name, const string &description);
54 
60  void defineOption(const GVariable &gvar, const string &help);
61 
69  void defineOption(const string &name, const string &description, const std::vector<GVariable> &gvars,
70  const string &help);
71 
77  int getScalarInt(const string &tag) const;
78 
84  float getScalarFloat(const string &tag) const;
85 
91  double getScalarDouble(const string &tag) const;
92 
98  string getScalarString(const string &tag) const;
99 
105  bool getSwitch(const string &tag) const;
106 
113  inline const YAML::Node getOptionNode(const string &tag) const {
114  if (!doesOptionExist(tag)) {
115  std::cerr << "Option " << tag << " does not exist. Exiting." << std::endl;
116  exit(EC__NOOPTIONFOUND);
117  }
118  return getOptionIterator(tag)->value.begin()->second;
119  }
120 
127  YAML::Node getOptionMapInNode(string option_name, string map_key);
128 
134  int getVerbosityFor(const string &tag) const;
135 
144  int getDebugFor(const string &tag) const;
145 
150  const std::vector<GOption> &getOptions() const { return goptions; }
151 
156  const std::map<string, GSwitch> &getSwitches() const { return switches; }
157 
162  inline void addGOptions(const GOptions &goptions_to_add) {
163  for (auto gopt : goptions_to_add.getOptions()) {
164  goptions.push_back(gopt);
165  }
166  for (auto sw : goptions_to_add.getSwitches()) {
167  switches.insert(sw);
168  }
169  for(auto &gvar : goptions_to_add.option_verbosity_names) {
170  option_verbosity_names.push_back(gvar);
171  }
172  }
173 
174  string option_verbosity_name = UNINITIALIZEDSTRINGQUANTITY;
175  vector <GVariable> option_verbosity_names;
176  inline void addOptionTitle(string name) {
177  string option_verbosity_name_desc = name + " verbosity level or debug switch";
178  option_verbosity_names.push_back( GVariable(name, 0, option_verbosity_name_desc) ) ;
179  }
180 
189  template<typename T>
190  T get_variable_in_option(const YAML::Node &node, const string &variable_name, const T &default_value);
191 
196  std::vector<string> getYamlFiles() const { return yaml_files; }
197 
203  bool doesOptionExist(const string &tag) const;
204 
205 private:
206  std::vector<GOption> goptions;
207  std::map<string, GSwitch> switches;
208  std::ofstream *yamlConf;
209  string executableName;
210  std::vector<string> yaml_files;
211 
218  vector<string> findYamls(int argc, char *argv[]);
219 
224  void setOptionsValuesFromYamlFile(const string &yaml);
225 
231  void setOptionValuesFromCommandLineArgument(const string &optionName, const string &possibleYamlNode);
232 
238  std::vector<GOption>::iterator getOptionIterator(const string &name);
239 
245  std::vector<GOption>::const_iterator getOptionIterator(const string &name) const;
246 
251  void printOptionOrSwitchHelp(const string &tag) const;
252 
256  void printHelp() const;
257 
261  void printWebHelp() const;
262 
266  void saveOptions() const;
267 
271  void print_version();
272 };
273 
275 GOptions &operator+=(GOptions &original, const GOptions &optionsToAdd);
276 
277 #endif
The GOptions class manages command-line options and switches.
Definition: goptions.h:20
void defineOption(const GVariable &gvar, const string &help)
Defines and adds a scalar option.
Definition: goptions.cc:176
bool getSwitch(const string &tag) const
Retrieves the status of a switch.
Definition: goptions.cc:414
int getDebugFor(const string &tag) const
Retrieves the debug level for the specified tag.
Definition: goptions.cc:499
const YAML::Node getOptionNode(const string &tag) const
Retrieves the YAML node for the specified option.
Definition: goptions.h:113
T get_variable_in_option(const YAML::Node &node, const string &variable_name, const T &default_value)
Retrieves a variable from a YAML node within an option.
Definition: goptions.cc:458
int getVerbosityFor(const string &tag) const
Retrieves the verbosity level for the specified tag.
Definition: goptions.cc:478
vector< GVariable > option_verbosity_names
Definition: goptions.h:175
std::vector< string > getYamlFiles() const
Retrieves the list of YAML file paths.
Definition: goptions.h:196
GOptions()
Default constructor.
Definition: goptions.h:27
GOptions(string name)
Constructor for verbosity and debug options.
Definition: goptions.h:34
void addOptionTitle(string name)
Definition: goptions.h:176
const std::vector< GOption > & getOptions() const
Returns the list of defined options.
Definition: goptions.h:150
int getScalarInt(const string &tag) const
Retrieves the value of a scalar integer option.
Definition: goptions.cc:211
void addGOptions(const GOptions &goptions_to_add)
Adds options from another GOptions object.
Definition: goptions.h:162
bool doesOptionExist(const string &tag) const
Checks if the specified option exists.
Definition: goptions.cc:312
float getScalarFloat(const string &tag) const
Retrieves the value of a scalar float option.
Definition: goptions.cc:227
const std::map< string, GSwitch > & getSwitches() const
Returns the map of defined switches.
Definition: goptions.h:156
string option_verbosity_name
Definition: goptions.h:174
void defineSwitch(const string &name, const string &description)
Defines and adds a command–line switch.
Definition: goptions.cc:160
string getScalarString(const string &tag) const
Retrieves the value of a scalar string option.
Definition: goptions.cc:259
YAML::Node getOptionMapInNode(string option_name, string map_key)
Retrieves a map option’s value from within a YAML node.
Definition: goptions.cc:433
double getScalarDouble(const string &tag) const
Retrieves the value of a scalar double option.
Definition: goptions.cc:243
#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:610
Encapsulates a variable with a name, value, and description.
Definition: goption.h:30