Support positional arguments
[anna.git] / source / core / util / CommandLine.cpp
index 74c34e1..39f2cdb 100644 (file)
@@ -89,6 +89,17 @@ 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.
@@ -189,7 +200,7 @@ bool CommandLine::analize()
 throw() {
   Variable* variable;
   bool result = true;
-  int i = 1;
+  int i = a_positionalArguments + 1;
   string aux;
 
   if(a_wasParsed == true) // already analyzed
@@ -315,6 +326,9 @@ throw() {
   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();
 
@@ -334,6 +348,9 @@ xml::Node* CommandLine::asXML(xml::Node* parent) const throw() {
   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();