Add Grouped behaviour.
[command.git] / include / command.h
1 #ifndef __COMMAND_COMMAND_H
2 #define __COMMAND_COMMAND_H
3
4 #include <string>
5 #include <vector>
6 #include <typeinfo>
7 #include <iostream>
8
9 #include "parameter.h"
10 #include "grouped.h"
11
12 namespace command {
13     /**
14      * Main class for handling user passed parameters from command line.
15      */
16     class Command : protected Grouped {
17     public:
18         /**
19          * Default constructor.
20          *
21          * @param argc from the main function
22          * @param argv from the main function
23          * @param params initializer_list containing Parameter handlers
24          *      responsible for correctly handle user data.
25          */
26         Command(unsigned int argc, char *argv[], std::initializer_list<Parameter *> params)
27             : Grouped(params, "Command") {
28             try {
29                 for (unsigned int i = 1; i < argc; i++) {
30                     this->understand(argv[i]);
31                 }
32                 handle();
33             }
34             catch(const std::invalid_argument & exception) {
35                 releaseMemory();
36                 throw;
37             }
38             catch(const std::logic_error & exception) {
39                 releaseMemory();
40                 throw;
41             }
42         }
43
44         /**
45          * Destructor. Releases allocated memory.
46          */
47         ~Command() {
48             releaseMemory();
49         }
50     };
51 }
52
53 #endif /* __COMMAND_COMMAND_H */