X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=blobdiff_plain;f=include%2Fcommand.h;h=7d01cb800cb9464a505fab1a957a9a5ef8698c00;hp=15d9a6be73ebfc0f0d1bda0239120a593400cc6c;hb=f2d683504e2dc76868ec84d7c1fd858edc0df5a4;hpb=f4f7e7543fb56070a408da837cf4e42333504348 diff --git a/include/command.h b/include/command.h index 15d9a6b..7d01cb8 100644 --- a/include/command.h +++ b/include/command.h @@ -4,31 +4,32 @@ #include #include #include +#include #include "parameter.h" -#include "exception/missingRequiredParameter.h" +#include "grouped.h" namespace command { /** * Main class for handling user passed parameters from command line. */ - class Command { - protected: - std::vector parameters; + class Command : protected Grouped { public: /** * Default constructor. * - * @param argc passed to the main function - * @param argv passed to the main function + * @param argc from the main function + * @param argv from the main function * @param params initializer_list containing Parameter handlers * responsible for correctly handle user data. */ Command(unsigned int argc, char *argv[], std::initializer_list params) - : parameters(params) { - + : Grouped(params, "Command") { try { - matchArguments(argc, argv); + for (unsigned int i = 1; i < argc; i++) { + this->understand(argv[i]); + } + handle(); } catch(const std::invalid_argument & exception) { releaseMemory(); @@ -46,38 +47,6 @@ namespace command { ~Command() { releaseMemory(); } - protected: - /** - * Matches user passed arguments with available parameter handlers. - */ - void matchArguments(unsigned int argc, char *argv[]) { - for (unsigned int i = 1; i < argc; i++) { - for(Parameter *param : parameters) { - if (!param->isUsed() && param->understand(argv[i])) { - param->handle(); - break; - } - } - } - for(Parameter *param : parameters) { - if (param->isRequired() && !param->isUsed()) { - throw MissingRequiredParameter(param->describe() + " is required but it was not passed"); - } - } - } - - /** - * Releases acquired memory - */ - void releaseMemory() { - for (Parameter * parameter : parameters) { - if (parameter != NULL) { - delete parameter; - } - } - parameters.clear(); - parameters.shrink_to_fit(); - } }; }