Save working version Command library.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 25 Apr 2015 15:39:02 +0000 (17:39 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 25 Apr 2015 15:39:02 +0000 (17:39 +0200)
Makefile.am
configure.ac
include/Makefile.am [new file with mode: 0644]
include/argument.h
include/callable.h [new file with mode: 0644]
include/command.h [new file with mode: 0644]
include/option.h
include/parameter.h [new file with mode: 0644]
src/main.cpp
tests/Makefile.am [new file with mode: 0644]

index 728aa9afb457d0fefba1e112a2acb74cd5d1e70e..04be05967d900cea14c918de3ca261bc609c5673 100644 (file)
@@ -23,7 +23,7 @@ CLEANFILES = \
 
 SUBDIRS = include . tests
 
-noinst_PROGRAMS = bin_command
+noinst_PROGRAMS = bin/command
 
 dist_noinst_SCRIPTS = autogen.sh
 
index 90d8d73ad7b3ab759b6cfbf530175f64e80bcd9a..2be3fb0fade9c9ec6ea9810d1973bb9c2aa0cc98 100644 (file)
@@ -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 (file)
index 0000000..cf641ff
--- /dev/null
@@ -0,0 +1,5 @@
+nobase_pkginclude_HEADERS = \
+    descriptive.h \
+    argument.h \
+    option.h \
+    command.h
index 743f0c23e26ddda4fdbdc29563e6d32e7b8db197..70d83c787d042ede1d59b791c1170fba3e520cdd 100644 (file)
@@ -2,8 +2,10 @@
 #define __COMMAND_ARGUMENT_H
 
 #include <string>
+#include <iostream>
 
-#include <command/descriptive.h>
+#include "parameter.h"
+#include "callable.h"
 
 namespace command {
     /**
@@ -14,34 +16,23 @@ namespace command {
      *  ./myprog ARGUMENT
      */
     template<typename ArgumentType>
-    class Argument : public Descriptive {
-    public:
-        typedef void (*)(ArgumentType) FunctionType;
-
-    protected:
-        /**
-         * Function handling user Arguments
-         */
-        FunctionType function;
-
+    class Argument : public Parameter, public Callable<ArgumentType> {
     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<ArgumentType>(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 (file)
index 0000000..7e6e503
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef __COMMAND_CALLABLE_H
+#define __COMMAND_CALLABLE_H
+
+#include <string>
+
+namespace command {
+    /**
+     * Callable behaviour class.
+     */
+    template<typename ArgumentType>
+    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 (file)
index 0000000..f53227e
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef __COMMAND_COMMAND_H
+#define __COMMAND_COMMAND_H
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+#include "parameter.h"
+
+namespace command {
+    /**
+     * Main class for handling user passed parameters from command line.
+     */
+    class Command {
+    protected:
+        unsigned int argc;
+        std::vector<std::string> _argv;
+        std::vector<Parameter *> args;
+    public:
+        /**
+         * Default constructor.
+         *
+         * @param 
+         * @param 
+         * @param 
+         */
+        Command(unsigned int argc, char *argv[], std::initializer_list<Parameter *> 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 */
index 241754d973b274286360e1772b9d038c2ccfecb3..fe0fc857ddf54cc6ee745cd6506174b112dbd027 100644 (file)
@@ -1,6 +1,10 @@
 #ifndef __COMMAND_OPTION_H
 #define __COMMAND_OPTION_H
 
+#include <string>
+
+#include "argument.h"
+
 namespace command {
     /**
      * Class responsible for handling commandline options.
@@ -12,6 +16,8 @@ namespace command {
     template<typename OptionType>
     class Option
         : Argument<OptionType> {
+    public:
+//         typedef typename Argument<OptionType, Lambda>::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<OptionType>(description, function) {
         }
+        virtual ~Option() { }
+
+        virtual void handle() {
+        }
     };
 }
 
diff --git a/include/parameter.h b/include/parameter.h
new file mode 100644 (file)
index 0000000..9e25086
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef __COMMAND_PARAMETER_H
+#define __COMMAND_PARAMETER_H
+
+#include <string>
+
+#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 */
index 37ca36f9d6cabd4ceb67bf239b8a6a9f3c0149d8..80aeb274803d70e63d9a90b7b056586a2c9bd847 100644 (file)
@@ -1,10 +1,18 @@
+#include <iostream>
+#include <string>
 
-int main() {
+#include "option.h"
+#include "argument.h"
+#include "command.h"
+
+
+int main(int argc, char *argv[]) {
     command::Command command(argc, argv, {
-        command::Option<std::string>("f", "File path", [](std::string value)->void { cout << "Sth: " << value << endl; }),
-        command::Argument<std::string>("File path", []()->void { cout << "Sth: " << value << endl; }),
-        command::Option<void>("help", "Help description", [](void)->void { cout << "Sth: " << value << endl; }),
-        command::Option<void>("verbose", "Verbose option description", &myClass->verbose)
+        new command::Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
+        new command::Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
+        new command::Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
+        new command::Argument<std::string>("File path", [](std::string value)->void { std::cout << "Hello from lambda " << value << std::endl; }),
+        new command::Argument<std::string>("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 (file)
index 0000000..e69de29