b41319a5d79c8bc00d1339106e5f0dc7c08bffe5
[anna.git] / include / anna / core / util / CommandLine.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     *  Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_util_CommandLine_hpp
38 #define anna_core_util_CommandLine_hpp
39
40 #include <stdlib.h>
41
42 #include <vector>
43
44 #include <anna/core/functions.hpp>
45 #include <anna/core/Singleton.hpp>
46
47 namespace anna {
48
49 class RuntimeException;
50
51 namespace xml {
52 class Node;
53 }
54
55
56 /**
57    Command line parser helper for our application. It's close to GNU-style, supporting
58    single letter (single hyphen) and long argument names (double hyphen). No bare hyphen
59    or double-dash end of parsing separator are supported. No positional arguments are supported.
60 */
61 class CommandLine : public Singleton <CommandLine> {
62
63   /* returns first no-leading hyphen position; -1 is error */
64   static int removeLeadingHyphens(std::string &argv) throw();
65
66 public:
67   /**
68      Define los tipos de argumento
69
70   */
71   struct Argument { enum Type { Mandatory = 0, Optional};  };
72
73   // Accesores
74   /**
75      @return la lista de cadenas indicadas en la linea de comandos al ejecutar este programa.
76      Mientras que no invoquemos al metodo #initialize devolvera NULL.
77   */
78   const char** getArgv() const throw() { return a_argv; }
79
80   /**
81      @return El numero de parametros indicados en la linea de comandos al ejecutar este programa.
82   */
83   int getArgc() const throw() { return a_argc; }
84
85   //
86   // Metodos
87   //
88   /**
89      Establece la informacion necesaria para analizar la linea de comandos
90      indicada por el usuario. Debe invocarse antes que cualquier otro metodo
91      relacionado con la obtencion/comprobacion de valor de la linea de comandos.
92
93      Recibe una copia de los parametros que los recibidos por el metodo 'main'.
94
95      @param argv Conjunto de cadenas que se reciben de la linea de comandos.
96      @param argc Numero de cadenas recibidas.
97      @param positionalArguments Enables positional arguments. An offset will be applied to start command-line interpretation.
98      These positional arguments are mandatory, and the user could retrieve their values through #getPositional. By default no
99      positional arguments are specified.
100   */
101   void initialize(const char** argv, const int argc, int positionalArguments = 0) throw(RuntimeException);
102
103   /**
104      Register an argument name in our application
105
106      @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,
107      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>
108      and --<word option> when proceed. If NULL provided, nothing is done.
109      @param type Argument type. See Variable#Type.
110      @param comment Argument explanation.
111      @param needValue If our argument has an additional associated value, this will be true. False in other case (flags).
112    */
113   void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw();
114
115   /**
116      Gets a positional argument. There must be registered or NULL will be returned.
117
118      @param position Argument position from 1 to N
119
120      @return Value of mandatory positional argument with position provided
121   */
122   const char *getPositional(int position) const throw() {
123     const char *result = NULL;
124     if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position];
125     return result;
126   }
127
128   /**
129      Obtiene el valor asociado al argumento recibido como parametro.
130      El valor devuelto puede ser NULL en caso de que el argumento no sea
131      obligatorio y no este en la linea de comandos.
132      Si el argumento es obligatorio y no este en la linea de comandos o
133      no tiene valor asociado la ejecucion del programa TERMINA inmediatamente.
134
135      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
136      @param exitOnFault Indica el funcionamiento del metodo en caso de que el
137      argumento solicitado no halla sido indicado. Si el parametro no existe
138      si vale @em true la aplicacion terminara, si vale @em false devolvera NULL.
139
140      @return Valor asociadoal argumento recibido como parametro. Puede ser NULL.
141   */
142   const char* getValue(const char* argumentExpression, const bool exitOnFault = true) throw();
143
144   /**
145      Obtiene el valor asociado al argumento recibido, pero convirtiendo a
146      numero el valor obtenido por #getValue.
147
148      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
149
150      @return Valor numerico del valor devuelto por #getValue.
151   */
152   int getIntegerValue(const char* argumentExpression) throw() { return atoi(getValue(argumentExpression)); }
153
154   /**
155      Comprueba si el argumento recibido como parametro estña presente en la linea de
156      comandos.
157
158      @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
159
160      @return true si el argumento esta en la linea de comandos y false en otro caso.
161    */
162   bool exists(const char* argumentExpression) throw() { return (getValue(argumentExpression, false) != NULL) ? true : false; }
163
164   /**
165     Comprueba la linea de comandos del programa para verificar que coincide con los argumentos
166     registrados por nuestra aplicacion:
167
168     @li Verifica que los parametros obligatorios estan en la linea de comandos.
169     @li Verifica que los valores de los argumento son correctos de forma que si un parametro
170     debe llevar un valor asociado este esta presente y que si no debe llevarlo no esta.
171     El orden en que aparezcan los argumento en la linea de comandos es indiferente a la hora de
172     hacer las comprobacion.
173
174     Si hay algun fallo en la linea de comandos establecida al ejecutar el programa visualiza un
175     resumen con los parametros soportados y la ejecucion del programa finaliza.
176   */
177   void verify() throw(RuntimeException);
178
179
180   /**
181      Class string representation
182      \return String with relevant information for this instance.
183   */
184   std::string asString() const throw();
185
186   /**
187      Class xml representation
188      \param parent Parent XML node on which hold this instance information.
189      \return XML document with relevant information for this instance.
190   */
191   xml::Node* asXML(xml::Node* parent) const throw();
192
193
194 private:
195   Mutex a_mutex;
196
197   class Variable {
198   public:
199     // Constructors
200     Variable(const std::string &name1, const std::string &name2, const Argument::Type type, const char* comment, const bool needValue = true) :
201       a_name1(name1), a_name2(name2), a_type(type), a_comment(comment), a_needValue(needValue), a_isOn(false), a_value(NULL) {
202     }
203     virtual ~Variable() { if(a_value) free(a_value); }
204
205     // Accesores
206     const std::string& getName1() const throw() { return a_name1; }
207     const std::string& getName2() const throw() { return a_name2; }
208     std::string getHelpExpression() const throw();
209     const char* getValue() const throw() { return a_value; }
210     const char* getComment() const throw() { return a_comment; }
211     bool getNeedValue() const throw() { return a_needValue; }
212     bool getIsOn() const throw() { return a_isOn; }
213     Argument::Type getType() const throw() { return a_type; }
214
215     // Modificadores
216     void setValue(const char* value) throw() { a_value = (value == NULL) ? NULL : strdup(value); }
217     void setIsOn(const bool isOn) throw() { a_isOn = isOn; }
218
219     // Metodos
220     std::string asString() const throw();
221
222   protected:
223     std::string a_name1, a_name2;
224     const char* a_comment;
225     char* a_value;
226     Argument::Type a_type;
227     bool a_needValue;
228     bool a_isOn;
229   };
230
231   const char **a_argv;
232   int a_argc;
233   bool a_wasParsed;
234   std::vector <Variable*> a_arguments;
235   int a_positionalArguments;
236
237   CommandLine() : a_argv(NULL), a_argc(0), a_positionalArguments(0)  {;}
238
239   bool analize() throw();
240   const Variable* search(const char *argumentExpression) const throw();
241   void printUsage() const throw();
242
243   friend class Singleton <CommandLine>;
244 };
245
246 }
247
248 #endif
249