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 
22 class GOptions {
23 
24 public:
25 
31  GOptions() { return; }
32 
39  GOptions(int argc, char *argv[], const GOptions &user_defined_options);
40 
47  void defineSwitch(const std::string &name, const std::string &description);
48 
55  void defineOption(const GVariable &gvar, const std::string &help);
56 
65  void defineOption(const std::string &name, const std::string &description, const std::vector <GVariable> &gvars, const std::string &help);
66 
73  int getScalarInt(const std::string &tag) const;
74 
81  float getScalarFloat(const std::string &tag) const;
82 
89  double getScalarDouble(const std::string &tag) const;
90 
97  std::string getScalarString(const std::string &tag) const;
98 
105  bool getSwitch(const std::string &tag) const;
106 
113  inline const YAML::Node getOptionNode(const std::string &tag) const {
114  // If the option does not exist, exit with error
115  if (!doesOptionExist(tag)) {
116  std::cerr << "Option " << tag << " does not exist. Exiting." << std::endl;
117  gexit(EC__NOOPTIONFOUND);
118  }
119  return getOptionIterator(tag)->value.begin()->second;
120  }
121 
129  YAML::Node getOptionMapInNode(string option_name, string map_key);
130 
137  int getVerbosityFor(const std::string &tag) const;
138 
144  const std::vector <GOption> &getOptions() const { return goptions; }
145 
151  const std::map <std::string, GSwitch> &getSwitches() const { return switches; }
152 
158  inline void addGOptions(const GOptions &goptions_to_add) {
159  for (auto gopt: goptions_to_add.getOptions()) {
160  goptions.push_back(gopt);
161  }
162  for (auto sw: goptions_to_add.getSwitches()) {
163  switches.insert(sw);
164  }
165  }
166 
175  template<typename T>
176  T get_variable_in_option(const YAML::Node &node, const string &variable_name, const T &default_value);
177 
183  std::vector <std::string> getYamlFiles() const { return yaml_files; }
184 
185 private:
186 
187  std::vector <GOption> goptions;
188  std::map <std::string, GSwitch> switches;
189  std::ofstream *yamlConf;
190  std::string executableName;
191  std::vector <std::string> yaml_files;
192 
193 
201  vector <string> findYamls(int argc, char *argv[]); // finds the yaml specified by command line. Returns "na' if not found.
202 
208  void setOptionsValuesFromYamlFile(const std::string &yaml);
209 
216  void setOptionValuesFromCommandLineArgument(const std::string &optionName, const std::string &possibleYamlNode);
217 
224  bool doesOptionExist(const std::string &tag) const;
225 
232  std::vector<GOption>::iterator getOptionIterator(const std::string &name);
233 
234  std::vector<GOption>::const_iterator getOptionIterator(const std::string &name) const;
235 
241  void printOptionOrSwitchHelp(const std::string &tag) const;
242 
246  void printHelp() const;
247 
251  void printWebHelp() const;
252 
256  void saveOptions() const;
257 
261  void print_version();
262 
263 
264 };
265 
266 // overloaded operator to add option vectors and switch maps
267 GOptions &operator+=(GOptions &original, const GOptions &optionsToAdd);
268 
269 #endif
The GOptions class manages command-line options and switches.
Definition: goptions.h:22
const YAML::Node getOptionNode(const std::string &tag) const
Retrieves the YAML::Node of the specified option.
Definition: goptions.h:113
std::string getScalarString(const std::string &tag) const
Retrieves the value of a scalar string option.
Definition: goptions.cc:239
bool getSwitch(const std::string &tag) const
Retrieves the status of a switch.
Definition: goptions.cc:374
T get_variable_in_option(const YAML::Node &node, const string &variable_name, const T &default_value)
Retrieves a variable from a YAML::Node.
Definition: goptions.cc:408
void defineSwitch(const std::string &name, const std::string &description)
Defines and adds a command-line switch to the map of switches.
Definition: goptions.cc:172
float getScalarFloat(const std::string &tag) const
Retrieves the value of a scalar float option.
Definition: goptions.cc:215
GOptions()
Default constructor.
Definition: goptions.h:31
void defineOption(const GVariable &gvar, const std::string &help)
Defines and adds a scalar option to the map of options.
Definition: goptions.cc:182
const std::vector< GOption > & getOptions() const
Returns the list of all defined options.
Definition: goptions.h:144
const std::map< std::string, GSwitch > & getSwitches() const
Returns the map of all defined switches.
Definition: goptions.h:151
double getScalarDouble(const std::string &tag) const
Retrieves the value of a scalar double option.
Definition: goptions.cc:227
void addGOptions(const GOptions &goptions_to_add)
Adds a set of GOptions to the current options.
Definition: goptions.h:158
int getScalarInt(const std::string &tag) const
Retrieves the value of a scalar integer option.
Definition: goptions.cc:203
std::vector< std::string > getYamlFiles() const
Retrieves the list of YAML files.
Definition: goptions.h:183
int getVerbosityFor(const std::string &tag) const
Retrieves the verbosity level for a given tag.
Definition: goptions.cc:426
YAML::Node getOptionMapInNode(string option_name, string map_key)
Retrieves a map option within a YAML::Node.
Definition: goptions.cc:388
#define EC__NOOPTIONFOUND
GOptions & operator+=(GOptions &original, const GOptions &optionsToAdd)
Definition: goptions.cc:531
Encapsulates a variable with a name, value, and description.
Definition: goption.h:32