/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/ratbase/include/RAT/AnyParse.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/ratbase/include/RAT/AnyParse.hh Source File
Ratpac-two
AnyParse.hh
1 #ifndef __ANYPARSE__
2 #define __ANYPARSE__
3 #include <any>
4 #include <map>
5 #include <string>
6 #include <vector>
7 
8 namespace RAT {
9 
10 enum ConvertType { ParseInt, ParseDouble, ParseString };
11 
12 class AnyParse {
13  public:
14  AnyParse(){};
15  AnyParse(int argc, char **argv);
16 
17  template <typename T>
18  void SetValue(const std::string &key, T value) {
19  Arguments[key] = value;
20  }
21 
22  template <typename T>
23  void AddArgument(const std::string &key, T value, const std::string &shortname, int nargs, const std::string help,
24  ConvertType cv) {
25  this->shortName[shortname] = key;
26  this->reverseShortName[key] = shortname;
27  this->help[key] = help;
28  this->nargs[key] = nargs;
29  this->SetValue(key, value);
30  this->Conversion[key] = cv;
31  }
32 
33  template <typename T>
34  T GetValue(const std::string &key, T defaultValue) const {
35  auto iterator = this->Arguments.find(key);
36  if (iterator == this->Arguments.end()) return defaultValue;
37  return std::any_cast<T>(iterator->second);
38  }
39 
40  void Parse();
41  void Help();
42  void SetHelpLine(std::string helpLine) { this->HelpLine = helpLine; }
43 
44  // Properties of the argument
45  std::string ExecutableName;
46  std::string HelpLine;
47  std::vector<std::string> CommandLine;
48  std::vector<std::string> Positionals;
49  std::map<std::string, ConvertType> Conversion;
50  std::map<std::string, std::any> Arguments;
51  std::map<std::string, std::string> shortName;
52  std::map<std::string, std::string> reverseShortName;
53  std::map<std::string, std::string> help;
54  std::map<std::string, int> nargs;
55 };
56 
57 } // namespace RAT
58 #endif
Definition: AnyParse.hh:12
Definition: CCCrossSecMessenger.hh:29