X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=include%2Fanna%2Fcore%2Futil%2FCommandLine.hpp;h=35fbb34b77e1f35c63c094f9f9473f59c5a4a290;hb=93366a0bda79e6fd6e7dad6316bfcf8cc82f5731;hp=c10a36e664b916bf13538a39bce2baf731d353d9;hpb=4e12ac57e93c052f716a6305ad8fc099c45899d1;p=anna.git diff --git a/include/anna/core/util/CommandLine.hpp b/include/anna/core/util/CommandLine.hpp index c10a36e..35fbb34 100644 --- a/include/anna/core/util/CommandLine.hpp +++ b/include/anna/core/util/CommandLine.hpp @@ -1,37 +1,9 @@ -// ANNA - Anna is Not 'N' Anymore -// -// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo -// -// https://bitbucket.org/testillano/anna -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions -// are met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Authors: eduardo.ramos.testillano@gmail.com -// cisco.tierra@gmail.com +// ANNA - Anna is Not Nothingness Anymore // +// // +// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo // +// // +// See project site at http://redmine.teslayout.com/projects/anna-suite // +// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE // #ifndef anna_core_util_CommandLine_hpp @@ -54,11 +26,15 @@ class Node; /** - Facilita la recogida de parametros desde la linea de comandos. Tambien - verifica que todos los parametros necesarios han sido indicados por el - usuario. + Command line parser helper for our application. It's close to GNU-style, supporting + single letter (single hyphen) and long argument names (double hyphen). No bare hyphen + or double-dash end of parsing separator are supported. No positional arguments are supported. */ class CommandLine : public Singleton { + + /* returns first no-leading hyphen position; -1 is error */ + static int removeLeadingHyphens(std::string &argv) throw(); + public: /** Define los tipos de argumento @@ -90,26 +66,36 @@ public: @param argv Conjunto de cadenas que se reciben de la linea de comandos. @param argc Numero de cadenas recibidas. + @param positionalArguments Enables positional arguments. An offset will be applied to start command-line interpretation. + These positional arguments are mandatory, and the user could retrieve their values through #getPositional. By default no + positional arguments are specified. */ - void initialize(const char** argv, const int argc) throw() { - a_argv = argv; - a_argc = argc; - a_wasParsed = false; - } + void initialize(const char** argv, const int argc, int positionalArguments = 0) throw(RuntimeException); /** - Registra un argumentName que sera recocido por nuestra aplicacion. + Register an argument name in our application + + @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, + 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 - + and -- when proceed. If NULL provided, nothing is done. + @param type Argument type. See Variable#Type. + @param comment Argument explanation. + @param needValue If our argument has an additional associated value, this will be true. False in other case (flags). + */ + void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw(); - Se pueden indicar tantos argumentNames como sea necesario. + /** + Gets a positional argument. There must be registered or NULL will be returned. - @param argumentName Nombre del argumento. - @param type Tipo de argumento. Ver Variable#Type. - @param comment Explicacion acerca de cometido de este argumento. - @param needValue Indica si el parametro que estamos definido debe tener un - valor adicional. Por ejemplo un parametro "usuario" deberia tener un valor adicional - que sea el valor que toma. - */ - void add(const char* argumentName, Argument::Type type, const char* comment, const bool needValue = true) throw(); + @param position Argument position from 1 to N + + @return Value of mandatory positional argument with position provided + */ + const char *getPositional(int position) const throw() { + const char *result = NULL; + if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position]; + return result; + } /** Obtiene el valor asociado al argumento recibido como parametro. @@ -118,34 +104,34 @@ public: Si el argumento es obligatorio y no este en la linea de comandos o no tiene valor asociado la ejecucion del programa TERMINA inmediatamente. - @param argumentName Nombre del argumento del que deseamos obtener el valor. + @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed. @param exitOnFault Indica el funcionamiento del metodo en caso de que el argumento solicitado no halla sido indicado. Si el parametro no existe si vale @em true la aplicacion terminara, si vale @em false devolvera NULL. @return Valor asociadoal argumento recibido como parametro. Puede ser NULL. */ - const char* getValue(const char* argumentName, const bool exitOnFault = true) throw(); + const char* getValue(const char* argumentExpression, const bool exitOnFault = true) throw(); /** Obtiene el valor asociado al argumento recibido, pero convirtiendo a numero el valor obtenido por #getValue. - @param argumentName Nombre del argumento del que deseamos obtener el valor. + @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed. @return Valor numerico del valor devuelto por #getValue. */ - int getIntegerValue(const char* argumentName) throw() { return atoi(getValue(argumentName)); } + int getIntegerValue(const char* argumentExpression) throw() { return atoi(getValue(argumentExpression)); } /** Comprueba si el argumento recibido como parametro estña presente en la linea de comandos. - @param argumentName Nombre del argumento del que deseamos obtener el valor. + @param argumentExpression You should look for the registered expression (#add), internally tokenized if needed. @return true si el argumento esta en la linea de comandos y false en otro caso. */ - bool exists(const char* argumentName) throw() { return (getValue(argumentName, false) != NULL) ? true : false; } + bool exists(const char* argumentExpression) throw() { return (getValue(argumentExpression, false) != NULL) ? true : false; } /** Comprueba la linea de comandos del programa para verificar que coincide con los argumentos @@ -182,15 +168,16 @@ private: class Variable { public: - // Constructores - Variable(const char* name, const Argument::Type type, const char* comment, const bool needValue = true) : - a_name(name), a_type(type), a_comment(comment), a_needValue(needValue), - a_isOn(false), a_value(NULL) { + // Constructors + Variable(const std::string &name1, const std::string &name2, const Argument::Type type, const char* comment, const bool needValue = true) : + a_name1(name1), a_name2(name2), a_type(type), a_comment(comment), a_needValue(needValue), a_isOn(false), a_value(NULL) { } virtual ~Variable() { if(a_value) free(a_value); } // Accesores - const std::string& getName() const throw() { return a_name; } + const std::string& getName1() const throw() { return a_name1; } + const std::string& getName2() const throw() { return a_name2; } + std::string getHelpExpression() const throw(); const char* getValue() const throw() { return a_value; } const char* getComment() const throw() { return a_comment; } bool getNeedValue() const throw() { return a_needValue; } @@ -205,7 +192,7 @@ private: std::string asString() const throw(); protected: - std::string a_name; + std::string a_name1, a_name2; const char* a_comment; char* a_value; Argument::Type a_type; @@ -217,11 +204,12 @@ private: int a_argc; bool a_wasParsed; std::vector a_arguments; + int a_positionalArguments; - CommandLine() : a_argv(NULL), a_argc(0) {;} + CommandLine() : a_argv(NULL), a_argc(0), a_positionalArguments(0) {;} bool analize() throw(); - const Variable* search(const char *argumentName) const throw(); + const Variable* search(const char *argumentExpression) const throw(); void printUsage() const throw(); friend class Singleton ;