command_line_arg_parser.h
Go to the documentation of this file.
1 
26 #ifndef COMMAND_LINE_ARG_PARSER_H
27 #define COMMAND_LINE_ARG_PARSER_H
28 
29 namespace argos {
30  class CCommandLineArgParser;
31 }
32 
33 #include <argos3/core/utility/datatypes/datatypes.h>
34 #include <argos3/core/utility/string_utilities.h>
35 #include <argos3/core/utility/logging/argos_log.h>
36 #include <vector>
37 #include <string>
38 #include <iostream>
39 
40 namespace argos {
41 
91 
92  public:
93 
98 
102  virtual ~CCommandLineArgParser();
103 
111  void AddFlag(char ch_short_option,
112  const std::string& str_long_option,
113  const std::string& str_description,
114  bool& b_flag) {
115  m_vecArguments.push_back(
116  new CArgument<bool>(
117  ch_short_option,
118  str_long_option,
119  str_description,
120  true,
121  b_flag));
122  b_flag = false;
123  }
124 
132  template <typename T> void AddArgument(char ch_short_option,
133  const std::string& str_long_option,
134  const std::string& str_description,
135  T& t_buffer) {
136  m_vecArguments.push_back(
137  new CArgument<T>(
138  ch_short_option,
139  str_long_option,
140  str_description,
141  false,
142  t_buffer));
143  }
144 
151  virtual void PrintUsage(CARGoSLog& c_log);
152 
158  virtual void Parse(SInt32 n_argc,
159  char** ppch_argv);
160 
161  private:
162 
163  void ParseLongOption (SInt32 n_argc, char** ppch_argv);
164  void ParseShortOption (SInt32 n_argc, char** ppch_argv);
165  void ParseShortOptions(SInt32 n_argc, char** ppch_argv);
166 
167  private:
168 
169  class CAbstractArgument {
170 
171  public:
172 
173  virtual ~CAbstractArgument() {}
174  virtual void Parse(const std::string& str_value) = 0;
175 
176  public:
177 
178  char ShortOption;
179  std::string LongOption;
180  std::string Description;
181  bool IsFlag;
182 
183  };
184 
185  template <typename T> class CArgument : public CAbstractArgument {
186 
187  public:
188 
189  CArgument(char ch_short_option,
190  const std::string& str_long_option,
191  const std::string& str_description,
192  bool b_is_flag,
193  T& t_buffer) :
194  m_tBuffer(t_buffer) {
195  ShortOption = ch_short_option;
196  LongOption = str_long_option;
197  Description = str_description;
198  IsFlag = b_is_flag;
199  }
200 
201  virtual void Parse(const std::string& str_value) {
202  m_tBuffer = FromString<T>(str_value);
203  }
204 
205  T& m_tBuffer;
206  };
207 
208  private:
209 
210  std::vector<CAbstractArgument*> m_vecArguments;
211  SInt32 m_nCurrentArgument;
212 
213  };
214 
215 }
216 
217 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Easy-to-use command line argument parser.
virtual void PrintUsage(CARGoSLog &c_log)
Prints the arguments on the log.
void AddArgument(char ch_short_option, const std::string &str_long_option, const std::string &str_description, T &t_buffer)
Adds an argument to the parser.
void AddFlag(char ch_short_option, const std::string &str_long_option, const std::string &str_description, bool &b_flag)
Adds a flag to the parser.
virtual void Parse(SInt32 n_argc, char **ppch_argv)
Parses the arguments on the command line.
virtual ~CCommandLineArgParser()
Class destructor.