From dd5fbed80e02dbb68a864fb59723dd106eebe14f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Sat, 25 Apr 2015 17:39:02 +0200 Subject: [PATCH] Save working version Command library. --- Makefile.am | 2 +- configure.ac | 4 ++-- include/Makefile.am | 5 +++++ include/argument.h | 31 +++++++++++-------------------- include/callable.h | 41 +++++++++++++++++++++++++++++++++++++++++ include/command.h | 43 +++++++++++++++++++++++++++++++++++++++++++ include/option.h | 12 +++++++++++- include/parameter.h | 36 ++++++++++++++++++++++++++++++++++++ src/main.cpp | 18 +++++++++++++----- tests/Makefile.am | 0 10 files changed, 163 insertions(+), 29 deletions(-) create mode 100644 include/Makefile.am create mode 100644 include/callable.h create mode 100644 include/command.h create mode 100644 include/parameter.h create mode 100644 tests/Makefile.am diff --git a/Makefile.am b/Makefile.am index 728aa9a..04be059 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,7 +23,7 @@ CLEANFILES = \ SUBDIRS = include . tests -noinst_PROGRAMS = bin_command +noinst_PROGRAMS = bin/command dist_noinst_SCRIPTS = autogen.sh diff --git a/configure.ac b/configure.ac index 90d8d73..2be3fb0 100644 --- a/configure.ac +++ b/configure.ac @@ -4,10 +4,10 @@ AM_INIT_AUTOMAKE([1.10 -Wall]) AC_CONFIG_HEADERS([config.h]) AC_PROG_CXX AC_CONFIG_FILES([ + include/Makefile Makefile + tests/Makefile ]) -# include/Makefile -# tests/Makefile # docs/Makefile AC_OUTPUT diff --git a/include/Makefile.am b/include/Makefile.am new file mode 100644 index 0000000..cf641ff --- /dev/null +++ b/include/Makefile.am @@ -0,0 +1,5 @@ +nobase_pkginclude_HEADERS = \ + descriptive.h \ + argument.h \ + option.h \ + command.h diff --git a/include/argument.h b/include/argument.h index 743f0c2..70d83c7 100644 --- a/include/argument.h +++ b/include/argument.h @@ -2,8 +2,10 @@ #define __COMMAND_ARGUMENT_H #include +#include -#include +#include "parameter.h" +#include "callable.h" namespace command { /** @@ -14,34 +16,23 @@ namespace command { * ./myprog ARGUMENT */ template - class Argument : public Descriptive { - public: - typedef void (*)(ArgumentType) FunctionType; - - protected: - /** - * Function handling user Arguments - */ - FunctionType function; - + class Argument : public Parameter, public Callable { public: + typedef class Argument Type; /** * Default constructor. * * @param description Description of current Argument * @param function Function used to handle current Argument. */ - Argument(std::string description, FunctionType function) - : Descriptive(description), function(function) { + Argument(std::string description, void (*function)(ArgumentType)) + : Parameter(description), Callable(function) { } + virtual ~Argument() { } - /** - * Executes command binded with argument - * - * @param value Value passed to program argument - */ - void run(ArgumentType value) { - this->function(value); + virtual void handle() { + std::cout << "Argument::handle()" << std::endl; + this->call(std::string("A")); } }; } diff --git a/include/callable.h b/include/callable.h new file mode 100644 index 0000000..7e6e503 --- /dev/null +++ b/include/callable.h @@ -0,0 +1,41 @@ +#ifndef __COMMAND_CALLABLE_H +#define __COMMAND_CALLABLE_H + +#include + +namespace command { + /** + * Callable behaviour class. + */ + template + class Callable { + protected: + /** + * Function handling user Arguments + */ + void (*func)(ArgumentType); + + public: + /** + * Default constructor. + * + * @param function Function that will be invoked + */ + Callable(void (*function)(ArgumentType)) + : func(function) { + } + virtual ~Callable() { } + + protected: + /** + * Executes command binded with argument + * + * @param value Value passed to program argument + */ + void call(ArgumentType value) { + this->func(value); + } + }; +} + +#endif /* __COMMAND_DESCRIPTIVE_H */ diff --git a/include/command.h b/include/command.h new file mode 100644 index 0000000..f53227e --- /dev/null +++ b/include/command.h @@ -0,0 +1,43 @@ +#ifndef __COMMAND_COMMAND_H +#define __COMMAND_COMMAND_H + +#include +#include +#include + +#include "parameter.h" + +namespace command { + /** + * Main class for handling user passed parameters from command line. + */ + class Command { + protected: + unsigned int argc; + std::vector _argv; + std::vector args; + public: + /** + * Default constructor. + * + * @param + * @param + * @param + */ + Command(unsigned int argc, char *argv[], std::initializer_list params) + : args(params) { + for(Parameter *param : params) { + std::cout << "Command foreach" << std::endl; + param->handle(); + } + } + + ~Command() { + for (Parameter * parameter : args) { + delete parameter; + } + } + }; +} + +#endif /* __COMMAND_COMMAND_H */ diff --git a/include/option.h b/include/option.h index 241754d..fe0fc85 100644 --- a/include/option.h +++ b/include/option.h @@ -1,6 +1,10 @@ #ifndef __COMMAND_OPTION_H #define __COMMAND_OPTION_H +#include + +#include "argument.h" + namespace command { /** * Class responsible for handling commandline options. @@ -12,6 +16,8 @@ namespace command { template class Option : Argument { + public: +// typedef typename Argument::FunctionType FunctionType; protected: /** * Option name @@ -26,9 +32,13 @@ namespace command { * @param description Description of current Option * @param function Function used to handle current Option. */ - Argument(std::string name, std::string description, FunctionType function) + Option(std::string name, std::string description, void (*function)(OptionType)) : name(name), Argument(description, function) { } + virtual ~Option() { } + + virtual void handle() { + } }; } diff --git a/include/parameter.h b/include/parameter.h new file mode 100644 index 0000000..9e25086 --- /dev/null +++ b/include/parameter.h @@ -0,0 +1,36 @@ +#ifndef __COMMAND_PARAMETER_H +#define __COMMAND_PARAMETER_H + +#include + +#include "descriptive.h" +#include "callable.h" + +namespace command { + /** + * Class responsible for handling commandline arguments. + * Arguments are required,x non-named parameters of program. + * + * Example: + * ./myprog ARGUMENT + */ + class Parameter : public Descriptive { + public: + typedef class Parameter Type; + /** + * Default constructor. + * + * @param description Description of current Argument + */ + Parameter(std::string description) + : Descriptive(description) { + } + virtual ~Parameter() {} + + virtual void handle() { + std::cout << "Parameter::handle()" << std::endl; + }; + }; +} + +#endif /* __COMMAND_PARAMETER_H */ diff --git a/src/main.cpp b/src/main.cpp index 37ca36f..80aeb27 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,18 @@ +#include +#include -int main() { +#include "option.h" +#include "argument.h" +#include "command.h" + + +int main(int argc, char *argv[]) { command::Command command(argc, argv, { - command::Option("f", "File path", [](std::string value)->void { cout << "Sth: " << value << endl; }), - command::Argument("File path", []()->void { cout << "Sth: " << value << endl; }), - command::Option("help", "Help description", [](void)->void { cout << "Sth: " << value << endl; }), - command::Option("verbose", "Verbose option description", &myClass->verbose) + new command::Argument("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }), + new command::Argument("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }), + new command::Argument("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }), + new command::Argument("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }), + new command::Argument("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }) }); return 0; diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..e69de29 -- 2.30.2