Add Grouped behaviour.
[command.git] / include / command.h
index f53227ea4a994d32e7b3c645b009def1f169b22f..7d01cb800cb9464a505fab1a957a9a5ef8698c00 100644 (file)
@@ -3,39 +3,49 @@
 
 #include <string>
 #include <vector>
+#include <typeinfo>
 #include <iostream>
 
 #include "parameter.h"
+#include "grouped.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;
+    class Command : protected Grouped {
     public:
         /**
          * Default constructor.
          *
-         * @param 
-         * @param 
-         * @param 
+         * @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<Parameter *> params)
-            : args(params) {
-            for(Parameter *param : params) {
-                std::cout << "Command foreach" << std::endl;
-                param->handle();
+            : Grouped(params, "Command") {
+            try {
+                for (unsigned int i = 1; i < argc; i++) {
+                    this->understand(argv[i]);
+                }
+                handle();
+            }
+            catch(const std::invalid_argument & exception) {
+                releaseMemory();
+                throw;
+            }
+            catch(const std::logic_error & exception) {
+                releaseMemory();
+                throw;
             }
         }
-        
+
+        /**
+         * Destructor. Releases allocated memory.
+         */
         ~Command() {
-            for (Parameter * parameter : args) {
-                delete parameter;
-            }
+            releaseMemory();
         }
     };
 }