goptions
goption.h
Go to the documentation of this file.
1 #ifndef GOPTION_H
2 #define GOPTION_H 1
3 
4 // goption
5 #include "goptionsConventions.h"
6 
7 // gemc
8 #include "gutsConventions.h"
9 
10 // c++
11 #include <map>
12 #include <fstream>
13 #include <vector>
14 #include <string>
15 
16 using std::string;
17 using std::map;
18 using std::vector;
19 
20 // yaml-cpp
21 #include "yaml-cpp/yaml.h"
22 
32 struct GVariable {
33 
34  string name;
35  string value;
36  string description;
37 
44  GVariable(string n, string val, string d) : name(n), value(val), description(d) {}
45 
52  GVariable(string n, double val, string d) : name(n), description(d) { value = std::to_string(val); }
53 
60  GVariable(string n, const char *val, string d) : name(n), value(val), description(d) {}
61 
68  GVariable(string n, int val, string d) : name(n), description(d) { value = std::to_string(val); }
69 
76  GVariable(string n, bool val, string d) : name(n), description(d) { value = val ? "true" : "false"; }
77 };
78 
86 class GOption {
87 public:
88 
98  GOption(GVariable dv, string h) : name(dv.name), description(dv.description), help(h) {
99  defaultValue = YAML::Load(name + ": " + dv.value);
100  value = defaultValue;
101  }
102 
103 
114  GOption(string n, string desc, vector <GVariable> dv, string h) : name(n), description(desc), help(h) {
115 
116  YAML::Node nodes;
117  for (const auto &v: dv) {
118  YAML::Node this_node = YAML::Load(v.name + ": " + v.value);
119  nodes.push_back(this_node);
120  gvar_descs.push_back(v.description);
121  if (v.value == goptions::NODFLT) {
122  isCumulative = true;
123  mandatory_keys.push_back(v.name);
124  }
125  }
126  defaultValue[n] = nodes;
127 
128  // if an option is not cumulative, the default value is copied to the first element of the sequence
129  if (!isCumulative) {
130  value = defaultValue;
131  }
132  }
133 
134 private:
135 
136  bool isCumulative = false;
137  const string name;
138  const string description;
139  const string help;
140 
141  // values is a map, containing either:
142  // - a single scalar
143  // Example:
144  // runno: 14
145  //
146  // - a sequence of maps
147  // Example 1:
148  // gparticle:
149  // - pname: e-
150  // multiplicity: 1
151  // Example 2:
152  // verbosity:
153  // - fields: 1
154  // - particles: 2
155 
156 
157  YAML::Node value;
158 
159 
160  YAML::Node defaultValue;
161  vector <string> gvar_descs;
162  vector <string> mandatory_keys;
163 
164 
169  void saveOption(std::ofstream *yamlConf) const;
170 
175  void printHelp(bool detailed) const;
176 
181  string detailedHelp() const;
182 
187  void set_scalar_value(const string &v);
188 
193  void set_value(const YAML::Node &v);
194 
200  bool does_the_option_set_all_necessary_values(YAML::Node v);
201 
202  friend class GOptions;
203 
204 
205 };
206 
207 
208 #endif
Represents a configurable option with a name, value, description, and help text.
Definition: goption.h:86
GOption(string n, string desc, vector< GVariable > dv, string h)
Constructor for a sequence option.
Definition: goption.h:114
GOption(GVariable dv, string h)
Constructor for a scalar option with a default value.
Definition: goption.h:98
The GOptions class manages command-line options and switches.
Definition: goptions.h:22
const std::string NODFLT
Encapsulates a variable with a name, value, and description.
Definition: goption.h:32
string value
The value of the variable, stored as a string.
Definition: goption.h:35
string description
A brief description of the variable.
Definition: goption.h:36
GVariable(string n, const char *val, string d)
Constructor for initializing a variable with an integer value.
Definition: goption.h:60
GVariable(string n, string val, string d)
Constructor for initializing a variable with a string value.
Definition: goption.h:44
string name
The name of the variable.
Definition: goption.h:34
GVariable(string n, int val, string d)
Constructor for initializing a variable with a double value.
Definition: goption.h:68
GVariable(string n, double val, string d)
Constructor for initializing a variable with a string value.
Definition: goption.h:52
GVariable(string n, bool val, string d)
Constructor for initializing a variable with a boolean value.
Definition: goption.h:76