try {
CommandLine& commandLine(anna::CommandLine::instantiate());
// General
- commandLine.add(NULL, anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("juan,pepe,maria", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("dos,palabras", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("x,y", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("-x", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("-ooox", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("--ooox", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("--x", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("x,-y", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("x,-lly", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("bueno,a-medias", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("bueno,en-te-ro", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
- commandLine.add("b,a-ho-ra-si", anna::CommandLine::Argument::Optional, "XXXXXXXXXXXXXXXXXXXXX");
-
-
-
commandLine.add("trace", anna::CommandLine::Argument::Optional, "Trace level (emergency, alert, critical, error, warning, notice, information, debug, local0..local7)");
commandLine.add("log", anna::CommandLine::Argument::Optional, "Process log file (operations result, traffic log, etc.). By default 'launcher.log'. Empty string or \"null\" name, to disable. Warning: there is no rotation for log files (use logrotate or whatever)");
commandLine.add("splitLog", anna::CommandLine::Argument::Optional, "Splits log file (appends to log filename, extensions with the type of event: see help on startup information-level traces). No log files for code/decode and load operations are created", false);
@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);
/**
Register an argument name in our application
*/
void add(const char* argumentExpression, Argument::Type type, const char* comment, const bool needValue = true) throw();
+ /**
+ Gets a positional argument. There must be registered or NULL will be returned.
+
+ @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.
El valor devuelto puede ser NULL en caso de que el argumento no sea
int a_argc;
bool a_wasParsed;
std::vector <Variable*> 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 *argumentExpression) const throw();
a_arguments.push_back(new Variable(arg1, arg2, type, comment, needValue));
}
+void CommandLine::initialize(const char** argv, const int argc, int positionalArguments)
+throw(RuntimeException) {
+ if (argc < 1) throw RuntimeException("Provided argc < 1 as command-line argv size !", ANNA_FILE_LOCATION);
+ if (positionalArguments < 0) throw RuntimeException("Provided negative number of positional arguments as command-line initializer", ANNA_FILE_LOCATION);
+ if (positionalArguments > (argc-1)) throw RuntimeException("Provided positional arguments > (argc - 1) as command-line initializer", ANNA_FILE_LOCATION);
+ a_positionalArguments = positionalArguments;
+ a_argv = argv;
+ a_argc = argc;
+ a_wasParsed = false;
+}
+
//--------------------------------------------------------------------------------
// Verifica que todos los argumentos declarados como obligatorios estan en la
// linea de comandos.
throw() {
Variable* variable;
bool result = true;
- int i = 1;
+ int i = a_positionalArguments + 1;
string aux;
if(a_wasParsed == true) // already analyzed
vector <Variable*>::const_iterator ii, maxii;
const char *value;
+ for(int pos = 1; pos <= a_positionalArguments; pos++)
+ result += anna::functions::asString("Positional argument [%d]: %s\n", pos, getPositional(pos));
+
for(ii = a_arguments.begin(), maxii = a_arguments.end(); ii != maxii; ii ++) {
value = (*ii)->getValue();
vector <Variable*>::const_iterator ii, maxii;
const char *value;
+ for(int pos = 1; pos <= a_positionalArguments; pos++)
+ result->createAttribute(anna::functions::asString("PositionalArgument_%d", pos).c_str(), getPositional(pos));
+
for(ii = a_arguments.begin(), maxii = a_arguments.end(); ii != maxii; ii ++) {
value = (*ii)->getValue();