1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_CommandLine_hpp
38 #define anna_core_util_CommandLine_hpp
44 #include <anna/core/functions.hpp>
45 #include <anna/core/Singleton.hpp>
49 class RuntimeException;
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.
61 class CommandLine : public Singleton <CommandLine> {
63 /* returns first no-leading hyphen position; -1 is error */
64 static int removeLeadingHyphens(std::string &argv) throw();
68 Define los tipos de argumento
71 struct Argument { enum Type { Mandatory = 0, Optional}; };
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.
78 const char** getArgv() const throw() { return a_argv; }
81 @return El numero de parametros indicados en la linea de comandos al ejecutar este programa.
83 int getArgc() const throw() { return a_argc; }
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.
93 Recibe una copia de los parametros que los recibidos por el metodo 'main'.
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.
101 void initialize(const char** argv, const int argc, int positionalArguments = 0) throw(RuntimeException);
104 Register an argument name in our application
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).
113 void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw();
116 Gets a positional argument. There must be registered or NULL will be returned.
118 @param position Argument position from 1 to N
120 @return Value of mandatory positional argument with position provided
122 const char *getPositional(int position) const throw() {
123 const char *result = NULL;
124 if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position];
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.
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.
140 @return Valor asociadoal argumento recibido como parametro. Puede ser NULL.
142 const char* getValue(const char* argumentExpression, const bool exitOnFault = true) throw();
145 Obtiene el valor asociado al argumento recibido, pero convirtiendo a
146 numero el valor obtenido por #getValue.
148 @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
150 @return Valor numerico del valor devuelto por #getValue.
152 int getIntegerValue(const char* argumentExpression) throw() { return atoi(getValue(argumentExpression)); }
155 Comprueba si el argumento recibido como parametro estña presente en la linea de
158 @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed.
160 @return true si el argumento esta en la linea de comandos y false en otro caso.
162 bool exists(const char* argumentExpression) throw() { return (getValue(argumentExpression, false) != NULL) ? true : false; }
165 Comprueba la linea de comandos del programa para verificar que coincide con los argumentos
166 registrados por nuestra aplicacion:
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.
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.
177 void verify() throw(RuntimeException);
181 Class string representation
182 \return String with relevant information for this instance.
184 std::string asString() const throw();
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.
191 xml::Node* asXML(xml::Node* parent) const throw();
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) {
203 virtual ~Variable() { if(a_value) free(a_value); }
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; }
216 void setValue(const char* value) throw() { a_value = (value == NULL) ? NULL : strdup(value); }
217 void setIsOn(const bool isOn) throw() { a_isOn = isOn; }
220 std::string asString() const throw();
223 std::string a_name1, a_name2;
224 const char* a_comment;
226 Argument::Type a_type;
234 std::vector <Variable*> a_arguments;
235 int a_positionalArguments;
237 CommandLine() : a_argv(NULL), a_argc(0), a_positionalArguments(0) {;}
239 bool analize() throw();
240 const Variable* search(const char *argumentExpression) const throw();
241 void printUsage() const throw();
243 friend class Singleton <CommandLine>;