From df766870acbdadb26573b9d3ead550d9e6653b03 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Sun, 3 May 2015 01:26:01 +0200 Subject: [PATCH] Added mechanism to understand passed Argument value. --- include/argument.h | 48 +++++++++++++++++++++++++++++++++++++++++++--- include/command.h | 19 ++++++++---------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/include/argument.h b/include/argument.h index 3ac1d71..159f5eb 100644 --- a/include/argument.h +++ b/include/argument.h @@ -2,6 +2,7 @@ #define __COMMAND_ARGUMENT_H #include +#include #include #include "parameter.h" @@ -10,15 +11,22 @@ namespace command { /** * Class responsible for handling commandline arguments. - * Arguments are required,x non-named parameters of program. + * Arguments are required, non-named parameters of program. + * Accepts * * Example: * ./myprog ARGUMENT */ template class Argument : public Parameter, public Callable { + protected: + /** Variable indicating if current Argument was already used or not */ + bool used = false; + + ArgumentType argument; public: typedef class Argument Type; + /** * Default constructor. * @@ -28,13 +36,47 @@ namespace command { Argument(const std::string & description, void (*function)(ArgumentType)) : Parameter(description), Callable(function) { } + + /** + * + */ virtual ~Argument() { } + /** + * + */ virtual void handle() { - this->call(std::string("A")); + this->call(argument); } - virtual bool understand(const std::string &) { + /** + * Method used for checking if Argument understands user value. + * If so current Argument is flagged as used, and no more checks against + * it will be done in future. + * + * If conversion from passed value to ArgumentType is impossible, it is + * ignored. + * + * @param argv command line value against which test will be made. + * + * @return If passed argv is succesfully converted to ArgumentType, + * returns true, and Argument is set as used one. If there was an error + * during conversion, method returns false and can be used to check + * against next value. + */ + virtual bool understand(const std::string & argv) { + if (!used) { + std::stringstream ss; + + ss << argv; + ss >> argument; + + if (!ss.fail()) { + used = true; + return true; + } + } + return false; } }; diff --git a/include/command.h b/include/command.h index 0c18cdf..ec08681 100644 --- a/include/command.h +++ b/include/command.h @@ -27,7 +27,6 @@ namespace command { : parameters(params) { matchArguments(argc, argv); - invoke(); } /** @@ -42,16 +41,14 @@ namespace command { /** * Matches user passed arguments with available parameter handlers. */ - void matchArguments(unsigned int , char **) { -// param->passUserValue(); - } - - /** - * Invokes passed parameter handlers - */ - void invoke() { - for(Parameter *param : parameters) { - param->handle(); + void matchArguments(unsigned int argc, char *argv[]) { + for (unsigned int i = 1; i < argc; i++) { + for(Parameter *param : parameters) { + if (param->understand(argv[i])) { + param->handle(); + break; + } + } } } }; -- 2.30.2