ANNA Suite  2020b
Multipurpose development suite for Telco applications
Classes | Public Member Functions | Friends | List of all members
anna::CommandLine Class Reference

#include <CommandLine.hpp>

Inheritance diagram for anna::CommandLine:
Inheritance graph
[legend]
Collaboration diagram for anna::CommandLine:
Collaboration graph
[legend]

Classes

struct  Argument
 

Public Member Functions

const char ** getArgv () const
 
int getArgc () const
 
void initialize (const char **argv, const int argc, int positionalArguments=0) noexcept(false)
 
void add (const char *argumentExpression, Argument::Type type, const char *comment, const bool needValue=true)
 
const char * getPositional (int position) const
 
const char * getValue (const char *argumentExpression, const bool exitOnFault=true)
 
int getIntegerValue (const char *argumentExpression)
 
bool exists (const char *argumentExpression)
 
void verify () noexcept(false)
 
std::string asString () const
 
xml::NodeasXML (xml::Node *parent) const
 

Friends

class Singleton< CommandLine >
 

Additional Inherited Members

- Static Public Member Functions inherited from anna::Singleton< T >
static T & instantiate ()
 
static void release ()
 

Detailed Description

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.

Member Function Documentation

◆ add()

void anna::CommandLine::add ( const char *  argumentExpression,
Argument::Type  type,
const char *  comment,
const bool  needValue = true 
)

Register an argument name in our application

Parameters
argumentExpressionArgument 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.
typeArgument type. See Variable::Type.
commentArgument explanation.
needValueIf our argument has an additional associated value, this will be true. False in other case (flags).

◆ asString()

std::string anna::CommandLine::asString ( ) const

Class string representation

Returns
String with relevant information for this instance.

◆ asXML()

xml::Node* anna::CommandLine::asXML ( xml::Node parent) const

Class xml representation

Parameters
parentParent XML node on which hold this instance information.
Returns
XML document with relevant information for this instance.

◆ exists()

bool anna::CommandLine::exists ( const char *  argumentExpression)
inline

Comprueba si el argumento recibido como parametro estña presente en la linea de comandos.

Parameters
argumentExpressionYou should look for the registered expression (add), internally tokenized if needed.
Returns
true si el argumento esta en la linea de comandos y false en otro caso.
134 { return (getValue(argumentExpression, false) != NULL) ? true : false; }
const char * getValue(const char *argumentExpression, const bool exitOnFault=true)

◆ getArgc()

int anna::CommandLine::getArgc ( ) const
inline
Returns
El numero de parametros indicados en la linea de comandos al ejecutar este programa.
55 { return a_argc; }

◆ getArgv()

const char** anna::CommandLine::getArgv ( ) const
inline
Returns
la lista de cadenas indicadas en la linea de comandos al ejecutar este programa. Mientras que no invoquemos al metodo initialize devolvera NULL.
50 { return a_argv; }

◆ getIntegerValue()

int anna::CommandLine::getIntegerValue ( const char *  argumentExpression)
inline

Obtiene el valor asociado al argumento recibido, pero convirtiendo a numero el valor obtenido por getValue.

Parameters
argumentExpressionYou should look for the registered expression (add), internally tokenized if needed.
Returns
Valor numerico del valor devuelto por getValue.
124 { return atoi(getValue(argumentExpression)); }
const char * getValue(const char *argumentExpression, const bool exitOnFault=true)

◆ getPositional()

const char* anna::CommandLine::getPositional ( int  position) const
inline

Gets a positional argument. There must be registered or NULL will be returned.

Parameters
positionArgument position from 1 to N
Returns
Value of mandatory positional argument with position provided
94  {
95  const char *result = NULL;
96  if ((position > 0) && (position <= a_positionalArguments)) result = a_argv[position];
97  return result;
98  }

◆ getValue()

const char* anna::CommandLine::getValue ( const char *  argumentExpression,
const bool  exitOnFault = true 
)

Obtiene el valor asociado al argumento recibido como parametro. El valor devuelto puede ser NULL en caso de que el argumento no sea obligatorio y no este en la linea de comandos. Si el argumento es obligatorio y no este en la linea de comandos o no tiene valor asociado la ejecucion del programa TERMINA inmediatamente.

Parameters
argumentExpressionYou should look for the registered expression (add), internally tokenized if needed.
exitOnFaultIndica el funcionamiento del metodo en caso de que el argumento solicitado no halla sido indicado. Si el parametro no existe si vale true la aplicacion terminara, si vale false devolvera NULL.
Returns
Valor asociadoal argumento recibido como parametro. Puede ser NULL.

◆ initialize()

void anna::CommandLine::initialize ( const char **  argv,
const int  argc,
int  positionalArguments = 0 
)
noexcept

Establece la informacion necesaria para analizar la linea de comandos indicada por el usuario. Debe invocarse antes que cualquier otro metodo relacionado con la obtencion/comprobacion de valor de la linea de comandos.

Recibe una copia de los parametros que los recibidos por el metodo 'main'.

Parameters
argvConjunto de cadenas que se reciben de la linea de comandos.
argcNumero de cadenas recibidas.
positionalArgumentsEnables 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.

◆ verify()

void anna::CommandLine::verify ( )
noexcept

Comprueba la linea de comandos del programa para verificar que coincide con los argumentos registrados por nuestra aplicacion:

  • Verifica que los parametros obligatorios estan en la linea de comandos.
  • Verifica que los valores de los argumento son correctos de forma que si un parametro debe llevar un valor asociado este esta presente y que si no debe llevarlo no esta. El orden en que aparezcan los argumento en la linea de comandos es indiferente a la hora de hacer las comprobacion.

Si hay algun fallo en la linea de comandos establecida al ejecutar el programa visualiza un resumen con los parametros soportados y la ejecucion del programa finaliza.

Friends And Related Function Documentation

◆ Singleton< CommandLine >

friend class Singleton< CommandLine >
friend

The documentation for this class was generated from the following file: