Remove warnings
[anna.git] / include / anna / core / util / CommandLine.hpp
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
28 /**
29    Command line parser helper for our application. It's close to GNU-style, supporting
30    single letter (single hyphen) and long argument names (double hyphen). No bare hyphen
31    or double-dash end of parsing separator are supported. No positional arguments are supported.
32 */
33 class CommandLine : public Singleton <CommandLine> {
34
35   /* returns first no-leading hyphen position; -1 is error */
36   static int removeLeadingHyphens(std::string &argv) throw();
37
38 public:
39   /**
40      Define los tipos de argumento
41
42   */
43   struct Argument { enum Type { Mandatory = 0, Optional};  };
44
45   // Accesores
46   /**
47      @return la lista de cadenas indicadas en la linea de comandos al ejecutar este programa.
48      Mientras que no invoquemos al metodo #initialize devolvera NULL.
49   */
50   const char** getArgv() const throw() { return a_argv; }
51
52   /**
53      @return El numero de parametros indicados en la linea de comandos al ejecutar este programa.
54   */
55   int getArgc() const throw() { return a_argc; }
56
57   //
58   // Metodos
59   //
60   /**
61      Establece la informacion necesaria para analizar la linea de comandos
62      indicada por el usuario. Debe invocarse antes que cualquier otro metodo
63      relacionado con la obtencion/comprobacion de valor de la linea de comandos.
64
65      Recibe una copia de los parametros que los recibidos por el metodo 'main'.
66
67      @param argv Conjunto de cadenas que se reciben de la linea de comandos.
68      @param argc Numero de cadenas recibidas.
69      @param positionalArguments Enables positional arguments. An offset will be applied to start command-line interpretation.
70      These positional arguments are mandatory, and the user could retrieve their values through #getPositional. By default no
71      positional arguments are specified.
72   */
73   void initialize(const char** argv, const int argc, int positionalArguments = 0) throw(RuntimeException);
74
75   /**
76      Register an argument name in our application
77
78      @param argumentExpression Argument name, or comma-separated set with both short and long argument names. For example 'v,version', 'h,help', or simply 'f' or 'file'. If both,
79      provided, one of them shall be a single letter and the other will be a word. In other case, nothing will be registered. Command line arguments stands for -<single letter option>
80      and --<word option> when proceed. If NULL provided, nothing is done.
81      @param type Argument type. See Variable#Type.
82      @param comment Argument explanation.
83      @param needValue If our argument has an additional associated value, this will be true. False in other case (flags).
84    */
85   void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw();
86
87   /**
88      Gets a positional argument. There must be registered or NULL will be returned.
89
90      @param position Argument position from 1 to N
91
92      @return Value of mandatory positional argument with position provided
93   */
94   const char *getPositional(int position) const throw() {
95     const char *result = NULL;
96     if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position];
97     return result;
98   }
99
100   /**
101      Obtiene el valor asociado al argumento recibido como parametro.
102      El valor devuelto puede ser NULL en caso de que el argumento no sea
103      obligatorio y no este en la linea de comandos.
104      Si el argumento es obligatorio y no este en la linea de comandos o
105      no tiene valor asociado la ejecucion del programa TERMINA inmediatamente.
106
107      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
108      @param exitOnFault Indica el funcionamiento del metodo en caso de que el
109      argumento solicitado no halla sido indicado. Si el parametro no existe
110      si vale @em true la aplicacion terminara, si vale @em false devolvera NULL.
111
112      @return Valor asociadoal argumento recibido como parametro. Puede ser NULL.
113   */
114   const char* getValue(const char* argumentExpression, const bool exitOnFault = true) throw();
115
116   /**
117      Obtiene el valor asociado al argumento recibido, pero convirtiendo a
118      numero el valor obtenido por #getValue.
119
120      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
121
122      @return Valor numerico del valor devuelto por #getValue.
123   */
124   int getIntegerValue(const char* argumentExpression) throw() { return atoi(getValue(argumentExpression)); }
125
126   /**
127      Comprueba si el argumento recibido como parametro estña presente en la linea de
128      comandos.
129
130      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
131
132      @return true si el argumento esta en la linea de comandos y false en otro caso.
133    */
134   bool exists(const char* argumentExpression) throw() { return (getValue(argumentExpression, false) != NULL) ? true : false; }
135
136   /**
137     Comprueba la linea de comandos del programa para verificar que coincide con los argumentos
138     registrados por nuestra aplicacion:
139
140     @li Verifica que los parametros obligatorios estan en la linea de comandos.
141     @li Verifica que los valores de los argumento son correctos de forma que si un parametro
142     debe llevar un valor asociado este esta presente y que si no debe llevarlo no esta.
143     El orden en que aparezcan los argumento en la linea de comandos es indiferente a la hora de
144     hacer las comprobacion.
145
146     Si hay algun fallo en la linea de comandos establecida al ejecutar el programa visualiza un
147     resumen con los parametros soportados y la ejecucion del programa finaliza.
148   */
149   void verify() throw(RuntimeException);
150
151
152   /**
153      Class string representation
154      \return String with relevant information for this instance.
155   */
156   std::string asString() const throw();
157
158   /**
159      Class xml representation
160      \param parent Parent XML node on which hold this instance information.
161      \return XML document with relevant information for this instance.
162   */
163   xml::Node* asXML(xml::Node* parent) const throw();
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 throw() { return a_name1; }
179     const std::string& getName2() const throw() { return a_name2; }
180     std::string getHelpExpression() const throw();
181     const char* getValue() const throw() { return a_value; }
182     const char* getComment() const throw() { return a_comment; }
183     bool getNeedValue() const throw() { return a_needValue; }
184     bool getIsOn() const throw() { return a_isOn; }
185     Argument::Type getType() const throw() { return a_type; }
186
187     // Modificadores
188     void setValue(const char* value) throw() { a_value = (value == NULL) ? NULL : strdup(value); }
189     void setIsOn(const bool isOn) throw() { a_isOn = isOn; }
190
191     // Metodos
192     std::string asString() const throw();
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() throw();
212   const Variable* search(const char *argumentExpression) const throw();
213   void printUsage() const throw();
214
215   friend class Singleton <CommandLine>;
216 };
217
218 }
219
220 #endif
221