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