GNU-style for command line. No positional arguments accepted. Supported single/double...
[anna.git] / include / anna / core / util / CommandLine.hpp
index 6d2569a..a869f4e 100644 (file)
@@ -54,11 +54,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 <CommandLine> {
+
+  /* returns first no-leading hyphen position; -1 is error */
+  static int removeLeadingHyphens(std::string &argv) throw();
+
 public:
   /**
      Define los tipos de argumento
@@ -98,18 +102,16 @@ public:
   }
 
   /**
-     Registra un argumentName que sera recocido por nuestra aplicacion.
-
-     Se pueden indicar tantos argumentNames como sea necesario.
-
-     @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.
+     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 -<single letter option>
+     and --<word option> 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* argumentName, Argument::Type type, const char* comment, const bool needValue = true) throw();
+  void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw();
 
   /**
      Obtiene el valor asociado al argumento recibido como parametro.
@@ -118,34 +120,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 +184,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 +208,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;
@@ -221,7 +224,7 @@ private:
   CommandLine() : a_argv(NULL), a_argc(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 <CommandLine>;