ANNA Suite  2020b
Multipurpose development suite for Telco applications
CommandLine.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_core_util_CommandLine_hpp
10 #define anna_core_util_CommandLine_hpp
11 
12 #include <stdlib.h>
13 
14 #include <vector>
15 
16 #include <anna/core/functions.hpp>
17 #include <anna/core/Singleton.hpp>
18 
19 namespace anna {
20 
21 class RuntimeException;
22 
23 namespace xml {
24 class Node;
25 }
26 
27 
33 class CommandLine : public Singleton <CommandLine> {
34 
35  /* returns first no-leading hyphen position; -1 is error */
36  static int removeLeadingHyphens(std::string &argv) ;
37 
38 public:
43  struct Argument { enum Type { Mandatory = 0, Optional}; };
44 
45  // Accesores
50  const char** getArgv() const { return a_argv; }
51 
55  int getArgc() const { return a_argc; }
56 
57  //
58  // Metodos
59  //
73  void initialize(const char** argv, const int argc, int positionalArguments = 0) noexcept(false);
74 
85  void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) ;
86 
94  const char *getPositional(int position) const {
95  const char *result = NULL;
96  if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position];
97  return result;
98  }
99 
114  const char* getValue(const char* argumentExpression, const bool exitOnFault = true) ;
115 
124  int getIntegerValue(const char* argumentExpression) { return atoi(getValue(argumentExpression)); }
125 
134  bool exists(const char* argumentExpression) { return (getValue(argumentExpression, false) != NULL) ? true : false; }
135 
149  void verify() noexcept(false);
150 
151 
156  std::string asString() const ;
157 
163  xml::Node* asXML(xml::Node* parent) const ;
164 
165 
166 private:
167  Mutex a_mutex;
168 
169  class Variable {
170  public:
171  // Constructors
172  Variable(const std::string &name1, const std::string &name2, const Argument::Type type, const char* comment, const bool needValue = true) :
173  a_name1(name1), a_name2(name2), a_type(type), a_comment(comment), a_needValue(needValue), a_isOn(false), a_value(NULL) {
174  }
175  virtual ~Variable() { if(a_value) free(a_value); }
176 
177  // Accesores
178  const std::string& getName1() const { return a_name1; }
179  const std::string& getName2() const { return a_name2; }
180  std::string getHelpExpression() const ;
181  const char* getValue() const { return a_value; }
182  const char* getComment() const { return a_comment; }
183  bool getNeedValue() const { return a_needValue; }
184  bool getIsOn() const { return a_isOn; }
185  Argument::Type getType() const { return a_type; }
186 
187  // Modificadores
188  void setValue(const char* value) { a_value = (value == NULL) ? NULL : strdup(value); }
189  void setIsOn(const bool isOn) { a_isOn = isOn; }
190 
191  // Metodos
192  std::string asString() const ;
193 
194  protected:
195  std::string a_name1, a_name2;
196  Argument::Type a_type;
197  const char* a_comment;
198  bool a_needValue;
199  bool a_isOn;
200  char* a_value;
201  };
202 
203  const char **a_argv;
204  int a_argc;
205  bool a_wasParsed;
206  std::vector <Variable*> a_arguments;
207  int a_positionalArguments;
208 
209  CommandLine() : a_argv(NULL), a_argc(0), a_positionalArguments(0) {;}
210 
211  bool analize() ;
212  const Variable* search(const char *argumentExpression) const ;
213  void printUsage() const ;
214 
215  friend class Singleton <CommandLine>;
216 };
217 
218 }
219 
220 #endif
221 
const char * getPositional(int position) const
Definition: CommandLine.hpp:94
Definition: Node.hpp:56
Definition: CommandLine.hpp:33
Definition: Singleton.hpp:76
Type
Definition: CommandLine.hpp:43
int getArgc() const
Definition: CommandLine.hpp:55
xml::Node Node
Definition: Node.hpp:21
Definition: app.hpp:12
bool exists(const char *argumentExpression)
Definition: CommandLine.hpp:134
Definition: Mutex.hpp:41
Definition: CommandLine.hpp:43
const char ** getArgv() const
Definition: CommandLine.hpp:50
int getIntegerValue(const char *argumentExpression)
Definition: CommandLine.hpp:124