Hermes
arg_parser.h
Go to the documentation of this file.
1 
31 #ifndef HERMES_COMMON_ARG_PARSER_H
32 #define HERMES_COMMON_ARG_PARSER_H
33 
34 #include <hermes/common/defs.h>
35 #include <hermes/common/str.h>
36 #include <string>
37 #include <vector>
38 #include <map>
39 #include <sstream>
40 
41 namespace hermes {
42 
43 // *********************************************************************************************************************
44 // ArgParser
45 // *********************************************************************************************************************
64 class ArgParser {
65 public:
66  // *******************************************************************************************************************
67  // CONSTRUCTORS
68  // *******************************************************************************************************************
72  explicit ArgParser(std::string bin = "", std::string description = "");
73  // *******************************************************************************************************************
74  // METHODS
75  // *******************************************************************************************************************
76  // parsing
81  bool parse(int argc, const char **argv, bool verbose_parsing = false);
82  // arguments
87  void addArgument(const std::string &name,
88  const std::string &description = "",
89  bool is_required = false);
95  template<typename T>
96  T get(const std::string &name, const T &default_value = {}) const {
97  const auto &it = m_.find(name);
98  if (it == m_.end() || !arguments_[it->second].given || arguments_[it->second].values.empty())
99  return default_value;
100  std::istringstream in(arguments_[it->second].values[0]);
101  T t{};
102  in >> t;
103  return t;
104  }
110  template<typename T>
111  std::vector<T> getList(const std::string &name, const std::vector<T> &default_value = {}) const {
112  const auto &it = m_.find(name);
113  if (it == m_.end() || !arguments_[it->second].given || arguments_[it->second].values.empty())
114  return default_value;
115  // expect a list separated by ',' or ' '
116  // expect string: "v1 ... vn" or "v1,...,vn" with no enclosing characters
117  auto s_values = hermes::Str::split(arguments_[it->second].values[0], " ,");
118  std::vector<T> values;
119  for (auto &value : s_values) {
120  std::istringstream in(value);
121  T t{};
122  in >> t;
123  values.template emplace_back(t);
124  }
125  return values;
126  }
130  [[nodiscard]] bool check(const std::string &name) const;
132  void printHelp() const;
133 
134 private:
135  struct Argument {
136  std::string description{};
137  std::vector<std::string> names{};
138  std::vector<std::string> values{};
139  bool required{false};
140  bool given{false};
141  };
142  std::map<std::string, u64> m_;
143  std::vector<Argument> arguments_;
144  std::string bin_;
145  std::string description_;
146 };
147 
148 }
149 
150 #endif //HERMES_COMMON_ARG_PARSER_H
151 
Command line argument parser.
Definition: arg_parser.h:64
void addArgument(const std::string &name, const std::string &description="", bool is_required=false)
Define program argument.
Definition: arg_parser.cpp:92
bool parse(int argc, const char **argv, bool verbose_parsing=false)
Parse program arguments.
Definition: arg_parser.cpp:41
ArgParser(std::string bin="", std::string description="")
Constructor.
Definition: arg_parser.cpp:38
T get(const std::string &name, const T &default_value={}) const
Get argument value.
Definition: arg_parser.h:96
std::vector< T > getList(const std::string &name, const std::vector< T > &default_value={}) const
Get argument value as list.
Definition: arg_parser.h:111
bool check(const std::string &name) const
Checks if an argument was given.
Definition: arg_parser.cpp:116
void printHelp() const
Output help into stdout.
Definition: arg_parser.cpp:101
static std::vector< std::string > split(const std::string &s, const std::string &delimiters=" ")
Splits a string into tokens separated by delimiters.
Definition: str.cpp:170
Data type definitions.
String utils.