ad324d29 |
1 | #ifndef ARGUMENT_READER_H |
2 | #define ARGUMENT_READER_H |
3 | |
4 | #include <vector> |
5 | #include <string> |
6 | |
7 | using namespace std; |
8 | |
9 | /*! |
040533c5 |
10 | *\brief Hachti's even more wonderful configuration manager. |
ad324d29 |
11 | * |
12 | * This class is designed to do all the work with the parameters passed to |
13 | * the main() routine of a program.\n |
040533c5 |
14 | * Additionally, it is able to interpret configuration files. |
ad324d29 |
15 | * Usage:\n |
16 | * - Create an instance. |
909d3603 |
17 | * - Add Parameters/switches with add_option_*(). |
ad324d29 |
18 | * - Add Arguments with add_argument(). |
19 | * - Call read_args() with your main routine's arguments. |
20 | * The vector returned by read_args() contains all error |
21 | * messages. When it's empty, everything is fine! |
22 | * - Call get_help() if you want nice formatted help output. |
23 | * (You want!!!) |
909d3603 |
24 | * Sould be easy to use.... |
ad324d29 |
25 | */ |
ca5fce3a |
26 | class configuration_manager{ |
ad324d29 |
27 | |
909d3603 |
28 | protected: // Types |
ad324d29 |
29 | /*! |
ca5fce3a |
30 | *\brief Container for an option switch |
ad324d29 |
31 | */ |
909d3603 |
32 | class opt_switch_t{ |
ad324d29 |
33 | public: |
909d3603 |
34 | opt_switch_t (const string & shortname, |
35 | const string & longname, |
36 | const string & description, |
37 | int * status, |
38 | const bool & allow_commandline=true, |
39 | const bool & allow_config_file=true); |
ad324d29 |
40 | string shortname; |
41 | string longname; |
42 | int * status; |
43 | string * target; |
44 | string description; |
45 | string placeholder; |
909d3603 |
46 | bool allow_conffile; |
47 | bool allow_cmdline; |
ad324d29 |
48 | }; |
909d3603 |
49 | |
ca5fce3a |
50 | /*! |
51 | *\brief Container for an option value |
52 | */ |
909d3603 |
53 | class opt_value_t{ |
ca5fce3a |
54 | public: |
909d3603 |
55 | opt_value_t (const string & shortname, |
56 | const string & longname, |
57 | const string & description, |
58 | int * status, |
59 | string * target, |
60 | const string & placeholder=string("<string>"), |
61 | const bool & allow_commandline=true, |
62 | const bool & allow_config_file=true); |
63 | string shortname; |
64 | string longname; |
65 | int * status; |
66 | string * target; |
67 | string description; |
68 | string placeholder; |
69 | bool allow_conffile; |
70 | bool allow_cmdline; |
71 | }; |
72 | |
ad324d29 |
73 | /*! |
ca5fce3a |
74 | *\brief Container for a commandline argument |
ad324d29 |
75 | */ |
ca5fce3a |
76 | class cmd_arg_t{ |
ad324d29 |
77 | public: |
909d3603 |
78 | cmd_arg_t (const string & placeholder, |
79 | const string & description, |
80 | int * status, |
81 | string * target); |
ad324d29 |
82 | int * status; |
83 | string * target; |
84 | string description; |
85 | string placeholder; |
86 | }; |
ca5fce3a |
87 | |
909d3603 |
88 | public: // Methods |
89 | |
90 | configuration_manager(string name); |
91 | |
92 | void add_option_value (const string & shortname, |
93 | const string & longname, |
94 | const string & description, |
95 | int *status, |
96 | string *target=NULL, |
97 | const string & placeholder=string("<string>"), |
98 | const bool & allow_commandline=true, |
99 | const bool & allow_config_file=true |
100 | ); |
101 | |
102 | void add_option_switch (const string & shortname, |
103 | const string & longname, |
104 | const string & description, |
105 | int *status, |
106 | const bool & allow_commandline=true, |
107 | const bool & allow_config_file=true |
108 | ); |
109 | |
110 | void add_argument (const string & description, |
111 | int *status, |
112 | string *target=NULL, |
113 | const string & placeholder=string("<string>") |
114 | ); |
115 | |
116 | vector<string> read_args(int argc, char ** args); |
117 | vector<string> read_file(string filename); |
118 | |
119 | void get_help (vector<string> & target); |
120 | vector<string> get_help(); |
121 | |
122 | protected: // members |
ca5fce3a |
123 | vector<opt_switch_t>option_switches; |
124 | vector<opt_value_t>option_values; |
125 | vector<cmd_arg_t>cmd_args; |
ad324d29 |
126 | string app_name; |
127 | |
909d3603 |
128 | protected: // methods |
129 | string analyse_string(const string & line); |
130 | bool analyse_bool(const string & data); |
fed2c751 |
131 | bool analyse_bool_false(const string & data); |
ca5fce3a |
132 | }; // class configuration_manager |
ad324d29 |
133 | |
134 | |
135 | #endif |
909d3603 |
136 | |