From: Eduardo Ramos Testillano Date: Sun, 19 Apr 2015 17:51:02 +0000 (+0200) Subject: Support positional arguments X-Git-Tag: REFACTORING_TESTING_LIBRARY~154 X-Git-Url: https://git.teslayout.com/public/public/public/?a=commitdiff_plain;h=c377f6f51a72b35d5d0b341c624e8bad55baeea4;p=anna.git Support positional arguments --- diff --git a/example/diameter/launcher/main.cpp b/example/diameter/launcher/main.cpp index 49612e9..e761b9f 100644 --- a/example/diameter/launcher/main.cpp +++ b/example/diameter/launcher/main.cpp @@ -1067,22 +1067,6 @@ int main(int argc, const char** argv) { 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); diff --git a/include/anna/core/util/CommandLine.hpp b/include/anna/core/util/CommandLine.hpp index a869f4e..b41319a 100644 --- a/include/anna/core/util/CommandLine.hpp +++ b/include/anna/core/util/CommandLine.hpp @@ -94,12 +94,11 @@ 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); /** Register an argument name in our application @@ -113,6 +112,19 @@ public: */ 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 @@ -220,8 +232,9 @@ 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 *argumentExpression) const throw(); diff --git a/source/core/util/CommandLine.cpp b/source/core/util/CommandLine.cpp index 74c34e1..39f2cdb 100644 --- a/source/core/util/CommandLine.cpp +++ b/source/core/util/CommandLine.cpp @@ -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 ::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 ::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();