1 | // **************************************************************************
|
---|
2 | /** @class Configuration
|
---|
3 |
|
---|
4 | @brief Commandline parsing, resource file parsing and database access
|
---|
5 |
|
---|
6 |
|
---|
7 | @section User For the user
|
---|
8 |
|
---|
9 | The Configuration class will process the following steps:
|
---|
10 |
|
---|
11 | Check the command-line for <B> --default=default.rc </B> (If no configuration
|
---|
12 | filename is given on the command-line use \e program_name.rc instead. (Note
|
---|
13 | that the name is retrieved from \b argv[0] and might change if you start
|
---|
14 | the program through a symbolic link with a different name)
|
---|
15 |
|
---|
16 | Read the "<B>database=user:password@database:port/database</B>" entry from the file.
|
---|
17 | (For details about the syntax see Configuration::parse_database)
|
---|
18 | The retrieved entry can be overwritten by
|
---|
19 | "<B>--database=user:passwd@server:port/database</B>" from the command line. If
|
---|
20 | neither option is given no configuration data will be read from the
|
---|
21 | database. To suppress any database access use \b --no-database.
|
---|
22 |
|
---|
23 | Check the command-line for <B> -C priority.rc </B>
|
---|
24 |
|
---|
25 | The configuration data is now evaluated from the following sources in
|
---|
26 | the following order. Note that options from earlier source have
|
---|
27 | priority.
|
---|
28 |
|
---|
29 | - (1) Commandline options
|
---|
30 | - (2) Options from the high prioroty configuration-file (given by \b -C or \b --config)
|
---|
31 | - (3) Database entries
|
---|
32 | - (4) Options from the default configuration-file (given by \b --default, defaults to \b program_name.rc)
|
---|
33 | - (5) Options from the global configuration-file (constrctor path + \b fact++.rc)
|
---|
34 | - (6) Environment variables
|
---|
35 |
|
---|
36 | Which options are accepted is defined by the program. To get a list
|
---|
37 | of all command-line option use \b --help. This also lists all other
|
---|
38 | available options to list for exmaple the options available in the
|
---|
39 | configuration files or from the databse. In addition some default options
|
---|
40 | are available which allow to debug parsing of the options, by either printing
|
---|
41 | the options retrieval or after parsing.
|
---|
42 |
|
---|
43 | Options in the configuration files must be given in the form
|
---|
44 |
|
---|
45 | - key = value
|
---|
46 |
|
---|
47 | which is equivalent to the command-line option <B>--key=value</B>.
|
---|
48 |
|
---|
49 | If there are sections in the configuration file like
|
---|
50 |
|
---|
51 | \code
|
---|
52 |
|
---|
53 | [section1]
|
---|
54 | key = value
|
---|
55 |
|
---|
56 | \endcode
|
---|
57 |
|
---|
58 | the key is transformed into <B>section1.key</B> (which would be equivalent
|
---|
59 | to <B>--section1.key</B>)
|
---|
60 |
|
---|
61 | @attention
|
---|
62 | In principle it is possible that an exception is thrown before options
|
---|
63 | like \b --help are properly parsed and evaluated. In this case it is
|
---|
64 | necessary to first resolve the problem. Usually, this mean that there
|
---|
65 | is a design flaw in the program rather than a mistake of usage.
|
---|
66 |
|
---|
67 | For more details on the order in which configuration is read,
|
---|
68 | check Configuration::Parse. For more details on the parsing itself
|
---|
69 | see the documentation of boost::program_options.
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | @section API For the programmer
|
---|
75 |
|
---|
76 | The Configuration class heavily uses the
|
---|
77 | <A HREF="http://www.boost.org"><B>C++ boost library</B></A>
|
---|
78 | and makes heavy use of the
|
---|
79 | <A HREF="http://www.boost.org/doc/libs/release/doc/html/program_options.html">
|
---|
80 | <B>boost::program_options</B></A>
|
---|
81 |
|
---|
82 | The databse access is based on the
|
---|
83 | <A HREF="http://tangentsoft.net/mysql++/"><B>MySQL++ library</B></A>.
|
---|
84 |
|
---|
85 | The basic idea is to have an easy to use, but powerfull setup. The setup on
|
---|
86 | all options is based on a special syntax of options_description. Here is an
|
---|
87 | example:
|
---|
88 |
|
---|
89 | \code
|
---|
90 |
|
---|
91 | int opt = 0;
|
---|
92 |
|
---|
93 | po::options_description config("Section");
|
---|
94 | config.add_options()
|
---|
95 | ("option1", var<string>(), "This is option1")
|
---|
96 | ("option2", var<int>(22), "This is option2")
|
---|
97 | ("option3,o", var<double>->required(), "This option is mandatory")
|
---|
98 | ("option4", var<int>(&opt), "This is option 4")
|
---|
99 | ("option5", vars<string>(), "A list of strings")
|
---|
100 | ("option6", vars<string>(), "A list of strings")
|
---|
101 | ("option7", vars<string>, "A list of strings")
|
---|
102 | ("option8", var<string>()->implicit_value("val"), "Just a string")
|
---|
103 | ("option9", var<string>()->default_value("def"), "Just a string")
|
---|
104 | ("optionA", var<string>("def"), "Just a string")
|
---|
105 | ("bool", po_bool(), "A special switch")
|
---|
106 | ;
|
---|
107 |
|
---|
108 | \endcode
|
---|
109 |
|
---|
110 | This will setup, e.g., the commandline option '<B>--option1 arg</B>' (which
|
---|
111 | is identical to '<B>--option1=arg</B>'. Option 3 can also be expressed
|
---|
112 | in a short form as '<B>-o arg</B>' or '<B>-o=arg</B>'. Option 2 defaults
|
---|
113 | to 22 if no explicit value is given. Option 3 is mandatory and an exceptionb
|
---|
114 | is thrown if not specified. Option 4 will, apart from the usual access to the
|
---|
115 | option values, also store its value in the variable opt.
|
---|
116 |
|
---|
117 | The used functions po_*() are defined in configuration.h and are abbreviations.
|
---|
118 | Generally speaking also other variable types are possible.
|
---|
119 |
|
---|
120 | If the options are displayed, e.g. by \b --help the corresponding section will
|
---|
121 | by titled \e Section, also untitled sections are possible.
|
---|
122 |
|
---|
123 | If an option can be given more than once then a std::vector<type> can be used.
|
---|
124 | Abbreviations po_ints(), po_doubles() and po_strings() are available.
|
---|
125 |
|
---|
126 | There are several ways to define the behaviour of the options. In the
|
---|
127 | example above Parse will throw an exception if the "--option3" or "-o"
|
---|
128 | option is not given. "option9" will evaluate to "def" if it is not
|
---|
129 | given on the command line. The syntax of "optionA" is just an
|
---|
130 | abbreviation. "option8" will evaluate to "val" if just "--option5" but
|
---|
131 | no argument is given. Note, that these modifiers can be concatenated.
|
---|
132 |
|
---|
133 | A special type po_bool() is provided which is an abbreviation of
|
---|
134 | var<bool>()->implicit_value(true)->default_value(false). In
|
---|
135 | contradiction to po_switch() this allows to set a true and
|
---|
136 | false value in the setup file.
|
---|
137 |
|
---|
138 | In addition to options introduced by a minus or double minus, so called
|
---|
139 | positional options can be given on the command line. To describe these
|
---|
140 | options use
|
---|
141 |
|
---|
142 | \code
|
---|
143 |
|
---|
144 | po::positional_options_description p;
|
---|
145 | p.add("option5", 2); // The first 2 positional options
|
---|
146 | p.add("option6", 3); // The next three positional options
|
---|
147 | // p.add("option7", -1); // All others, if wanted
|
---|
148 |
|
---|
149 | \endcode
|
---|
150 |
|
---|
151 | This assigns option-keys to the positional options (arguments) in the
|
---|
152 | command-line. Note that the validity of the given commandline is checked.
|
---|
153 | Hence, this way of defining the options makes sense.
|
---|
154 |
|
---|
155 | As needed options_descriptions can be grouped together
|
---|
156 |
|
---|
157 | \code
|
---|
158 |
|
---|
159 | po::options_description config1("Section1");
|
---|
160 | po::options_description config2("Section2");
|
---|
161 |
|
---|
162 | po::options_description configall;
|
---|
163 | configall.add(config1);
|
---|
164 | configall.add(config2);
|
---|
165 |
|
---|
166 | \endcode
|
---|
167 |
|
---|
168 | The member functions of Configurations allow to define for which option
|
---|
169 | source these options are valid. The member functions are:
|
---|
170 |
|
---|
171 | \code
|
---|
172 |
|
---|
173 | Configuration conf;
|
---|
174 |
|
---|
175 | conf.AddOptionsCommandline(configall, true);
|
---|
176 | conf.AddOptionsConfigfile(config1, true);
|
---|
177 | conf.AddOptionsDatabase(config2, true);
|
---|
178 |
|
---|
179 | // To enable the mapping of the position arguments call this
|
---|
180 | conf.SetArgumentPositions(p);
|
---|
181 |
|
---|
182 | \endcode
|
---|
183 |
|
---|
184 | If the second option is false, the options will not be displayed in any
|
---|
185 | \b --help directive, but are available to the user. Each of the functions
|
---|
186 | can be called more than once. If an option should be available from
|
---|
187 | all kind of inputs AddOptions() can be used which will call all
|
---|
188 | four other AddOptions() functions.
|
---|
189 |
|
---|
190 | A special case are the options from environment variables. Since you might
|
---|
191 | want to use the same option-key for the command-line and the environment,
|
---|
192 | a mapping is needed (e.g. from \b PATH to \b --path). This mapping
|
---|
193 | can be implemented by a mapping function or by the build in mapping
|
---|
194 | and be initialized like this:
|
---|
195 |
|
---|
196 | \code
|
---|
197 |
|
---|
198 | conf.AddEnv("path", "PATH");
|
---|
199 |
|
---|
200 | \endcode
|
---|
201 |
|
---|
202 | or
|
---|
203 |
|
---|
204 | \code
|
---|
205 |
|
---|
206 | const string name_mapper(const string str)
|
---|
207 | {
|
---|
208 | return str=="PATH" ? "path" : "";
|
---|
209 | }
|
---|
210 |
|
---|
211 | conf.SetNameMapper(name_mapper);
|
---|
212 |
|
---|
213 | \endcode
|
---|
214 |
|
---|
215 | Assuming all the above is done in a function calles SetupConfiguration(),
|
---|
216 | a simple program to demonstrate the power of the class could look like this:
|
---|
217 |
|
---|
218 | \code
|
---|
219 |
|
---|
220 | int main(int argc, char **argv)
|
---|
221 | {
|
---|
222 | int opt;
|
---|
223 |
|
---|
224 | Configuration conf(argv[0]);
|
---|
225 | SetupConfiguration(conf, opt);
|
---|
226 |
|
---|
227 | po::variables_map vm;
|
---|
228 | try
|
---|
229 | {
|
---|
230 | vm = conf.Parse(argc, argv);
|
---|
231 | }
|
---|
232 | catch (std::exception &e)
|
---|
233 | {
|
---|
234 | po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
|
---|
235 | if (MO)
|
---|
236 | cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
|
---|
237 | else
|
---|
238 | cout << "Error: " << e.what() << endl;
|
---|
239 | cout << endl;
|
---|
240 |
|
---|
241 | return -1;
|
---|
242 | }
|
---|
243 |
|
---|
244 | cout << "Opt1: " << conf.GetString("option1") << endl;
|
---|
245 | cout << "Opt2: " << conf.GetInt("option2") << endl;
|
---|
246 | cout << "Opt3: " << conf.GetDouble("option3") << endl;
|
---|
247 | cout << "Opt4: " << opt << endl;
|
---|
248 |
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | \endcode
|
---|
253 |
|
---|
254 | Another possibility to access the result is the direct approach, for example:
|
---|
255 |
|
---|
256 | \code
|
---|
257 |
|
---|
258 | vector<int> i = vm["option2"].as<int>();
|
---|
259 | vector<string> vec = vm["option6"].as<vector<string>>();
|
---|
260 |
|
---|
261 | \endcode
|
---|
262 |
|
---|
263 | Note that accessing an option which was not given will throw an exception.
|
---|
264 | Therefor its availability should first be checked in one of the following
|
---|
265 | ways:
|
---|
266 |
|
---|
267 | \code
|
---|
268 |
|
---|
269 | bool has_option1 = vm.count("option1");
|
---|
270 | bool has_option2 = conf.Has("option2");
|
---|
271 |
|
---|
272 | \endcode
|
---|
273 |
|
---|
274 | @section Extensions
|
---|
275 |
|
---|
276 | The configuration interpreter can be easily extended to new types, for example:
|
---|
277 |
|
---|
278 | \code
|
---|
279 |
|
---|
280 | template<class T,class S> // Just for the output
|
---|
281 | std::ostream &operator<<(std::ostream &out, const pair<T,S> &f)
|
---|
282 | {
|
---|
283 | out << f.first << "|" << f.second;
|
---|
284 | return out;
|
---|
285 | }
|
---|
286 |
|
---|
287 | template<class T, class S> // Needed to convert the option
|
---|
288 | std::istream &operator>>(std::istream &in, pair<T,S> &f)
|
---|
289 | {
|
---|
290 | char c;
|
---|
291 | in >> f.first;
|
---|
292 | in >> c;
|
---|
293 | if (c!=':')
|
---|
294 | return in;
|
---|
295 | in >> f.second;
|
---|
296 | return in;
|
---|
297 | }
|
---|
298 |
|
---|
299 | typedef pair<int,int> mytype; // Type definition
|
---|
300 |
|
---|
301 | void main(int argc, char **argv)
|
---|
302 | {
|
---|
303 | po::options_description config("Configuration");
|
---|
304 | config.add_options()
|
---|
305 | ("mytype", var<mytype>(), "my new type")
|
---|
306 | ;
|
---|
307 |
|
---|
308 | Configuration conf;
|
---|
309 | conf.AddOptionsCommandline(config);
|
---|
310 | conf.Parse(argc, argv);
|
---|
311 |
|
---|
312 | cout << conf.Get<mytype>("mytype") << endl;
|
---|
313 | }
|
---|
314 |
|
---|
315 | \endcode
|
---|
316 |
|
---|
317 | @section Examples
|
---|
318 |
|
---|
319 | - An example can be found in \ref argv.cc
|
---|
320 |
|
---|
321 | @todo
|
---|
322 |
|
---|
323 | - Maybe we should remove the necessity to propagate argv[0] in the constructor?
|
---|
324 | - Add an option to the constructor to switch of database/file access
|
---|
325 |
|
---|
326 | */
|
---|
327 | // **************************************************************************
|
---|
328 | #include "Configuration.h"
|
---|
329 |
|
---|
330 | #include <fstream>
|
---|
331 | #include <iostream>
|
---|
332 | #include <iomanip>
|
---|
333 |
|
---|
334 | #include <boost/regex.hpp>
|
---|
335 | #include <boost/filesystem.hpp>
|
---|
336 |
|
---|
337 | #define HAS_SQL
|
---|
338 |
|
---|
339 | #ifdef HAS_SQL
|
---|
340 | #include <mysql++/mysql++.h>
|
---|
341 | #endif
|
---|
342 |
|
---|
343 | using namespace std;
|
---|
344 |
|
---|
345 | namespace style = boost::program_options::command_line_style;
|
---|
346 |
|
---|
347 | // --------------------------------------------------------------------------
|
---|
348 | //
|
---|
349 | //! The purpose of this function is basically to connect to the database,
|
---|
350 | //! and retrieve all the options entries from the 'Configuration' table.
|
---|
351 | //!
|
---|
352 | //! @param database
|
---|
353 | //! The URL of the database from which the configuration data is
|
---|
354 | //! retrieved. It should be given in the form
|
---|
355 | //! \li [user[:password]@]server.com[:port]/database
|
---|
356 | //!
|
---|
357 | //! with
|
---|
358 | //! - user: user name (default is the current user)
|
---|
359 | //! - password: necessary if required by the database rights
|
---|
360 | //! - server: the URL of the server (can be 'localhost')
|
---|
361 | //! - port: the port to which to connect (usually obsolete)
|
---|
362 | //! - database: The name of the database containing the table
|
---|
363 | //!
|
---|
364 | //! @param desc
|
---|
365 | //! A reference to the object with the description of the options
|
---|
366 | //! which should be retrieved.
|
---|
367 | //!
|
---|
368 | //! @param allow_unregistered
|
---|
369 | //! If this is true also unregistered, i.e. options unknown to desc,
|
---|
370 | //! are returned. Otherwise an exception is thrown if such an option
|
---|
371 | //! was retrieved.
|
---|
372 | //!
|
---|
373 | //! @return
|
---|
374 | //! Return an object of type basic_parsed_options containing all
|
---|
375 | //! the entries retrieved from the database. Options not found in
|
---|
376 | //! desc are flagged as unregistered.
|
---|
377 | //!
|
---|
378 | //! @throws
|
---|
379 | //! Two types of exceptions are thrown
|
---|
380 | //! - It thows an unnamed exception if the options could not be
|
---|
381 | //! retrieved properly from the databse.
|
---|
382 | //! - If an option is not registered within the given descriptions
|
---|
383 | //! and \b allow_unregistered is \b false, an exception of type
|
---|
384 | //! \b po::unknown_option is thrown.
|
---|
385 | //!
|
---|
386 | //! @todo
|
---|
387 | //! - The exceptions handling should be improved.
|
---|
388 | //! - The final database layout is missing in the description
|
---|
389 | //! - Shell we allow options to be given more than once?
|
---|
390 | //
|
---|
391 | #ifdef HAS_SQL
|
---|
392 | po::basic_parsed_options<char>
|
---|
393 | Configuration::parse_database(const string &prgname, const string &database, const po::options_description& desc, bool allow_unregistered)
|
---|
394 | {
|
---|
395 | //static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))?");
|
---|
396 | static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
|
---|
397 | // 2: user
|
---|
398 | // 4: pass
|
---|
399 | // 5: server
|
---|
400 | // 7: port
|
---|
401 | // 9: db
|
---|
402 |
|
---|
403 | boost::smatch what;
|
---|
404 | if (!boost::regex_match(database, what, expr, boost::match_extra))
|
---|
405 | throw runtime_error("Couldn't parse '"+database+"'.");
|
---|
406 |
|
---|
407 | if (what.size()!=10)
|
---|
408 | throw runtime_error("Error parsing '"+database+"'.");
|
---|
409 |
|
---|
410 | const string user = what[2];
|
---|
411 | const string passwd = what[4];
|
---|
412 | const string server = what[5];
|
---|
413 | const string db = what[9];
|
---|
414 | const int port = atoi(string(what[7]).c_str());
|
---|
415 |
|
---|
416 | cout << "Connecting to '";
|
---|
417 | if (!user.empty())
|
---|
418 | cout << user << "@";
|
---|
419 | cout << server;
|
---|
420 | if (port)
|
---|
421 | cout << ":" << port;
|
---|
422 | if (!db.empty())
|
---|
423 | cout << "/" << db;
|
---|
424 | cout << "' for " << prgname << endl;
|
---|
425 |
|
---|
426 | mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
|
---|
427 | /* throws exceptions
|
---|
428 | if (!conn.connected())
|
---|
429 | {
|
---|
430 | cout << "MySQL connection error: " << conn.error() << endl;
|
---|
431 | throw;
|
---|
432 | }*/
|
---|
433 |
|
---|
434 | const mysqlpp::StoreQueryResult res =
|
---|
435 | conn.query("SELECT CONCAT(fKey1,fKey2), fValue "
|
---|
436 | "FROM ProgramOption "
|
---|
437 | "WHERE fCounter=(SELECT MAX(fCounter) FROM History) "
|
---|
438 | "AND (fProgram='"+prgname+"' OR fProgram='*')").store();
|
---|
439 | /* throws exceptions
|
---|
440 | if (!res)
|
---|
441 | {
|
---|
442 | cout << "MySQL query failed: " << query.error() << endl;
|
---|
443 | throw;
|
---|
444 | }*/
|
---|
445 |
|
---|
446 | set<string> allowed_options;
|
---|
447 |
|
---|
448 | const vector<boost::shared_ptr<po::option_description>> &options = desc.options();
|
---|
449 | for (unsigned i=0; i<options.size(); ++i)
|
---|
450 | {
|
---|
451 | const po::option_description &d = *options[i];
|
---|
452 | if (d.long_name().empty())
|
---|
453 | boost::throw_exception(po::error("long name required for database"));
|
---|
454 |
|
---|
455 | allowed_options.insert(d.long_name());
|
---|
456 | }
|
---|
457 |
|
---|
458 | po::parsed_options result(&desc);
|
---|
459 |
|
---|
460 | for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
|
---|
461 | {
|
---|
462 | const string key = (*v)[0].c_str();
|
---|
463 | if (key.empty()) // key == > Throw exception
|
---|
464 | continue;
|
---|
465 |
|
---|
466 | // Check if we are allowed to accept unregistered options,
|
---|
467 | // i.e. options which are not in options_description &desc.
|
---|
468 | const bool unregistered = allowed_options.find(key)==allowed_options.end();
|
---|
469 | if (unregistered && allow_unregistered)
|
---|
470 | boost::throw_exception(po::unknown_option(key));
|
---|
471 |
|
---|
472 | // Create a key/value-pair and store whether it is a
|
---|
473 | // registered option of not
|
---|
474 | po::option n;
|
---|
475 | n.string_key = key;
|
---|
476 | // This is now identical to file parsing. What if we want
|
---|
477 | // to concatenate options like on the command line?
|
---|
478 | n.value.clear(); // Fixme: composing?
|
---|
479 | n.value.push_back((*v)[1].c_str());
|
---|
480 | //n.unregistered = unregistered;
|
---|
481 |
|
---|
482 | // If any parsing will be done in the future...
|
---|
483 | //n.value().original_tokens.clear();
|
---|
484 | //n.value().original_tokens.push_back(name);
|
---|
485 | //n.value().original_tokens.push_back(value);
|
---|
486 |
|
---|
487 | result.options.push_back(n);
|
---|
488 | }
|
---|
489 |
|
---|
490 | return result;
|
---|
491 | }
|
---|
492 | #else
|
---|
493 | po::basic_parsed_options<char>
|
---|
494 | Configuration::parse_database(const string &, const string &, const po::options_description &desc, bool)
|
---|
495 | {
|
---|
496 | return po::parsed_options(&desc);
|
---|
497 | }
|
---|
498 | #endif
|
---|
499 |
|
---|
500 | // --------------------------------------------------------------------------
|
---|
501 | //
|
---|
502 | //!
|
---|
503 | //
|
---|
504 | Configuration::Configuration(const string &prgname) : fName(UnLibToolize(prgname)),
|
---|
505 | fNameMapper(bind1st(mem_fun(&Configuration::DefaultMapper), this)),
|
---|
506 | fPrintUsage(bind(&Configuration::PrintUsage, this))
|
---|
507 | {
|
---|
508 | po::options_description generic("Generic options");
|
---|
509 | generic.add_options()
|
---|
510 | ("version,V", "Print version information.")
|
---|
511 | ("help", "Print available commandline options.")
|
---|
512 | ("help-environment", "Print available environment variables.")
|
---|
513 | ("help-database", "Print available options retreived from the database.")
|
---|
514 | ("help-config", "Print available configuration file options.")
|
---|
515 | ("print-all", "Print all options as parsed from all the different sources.")
|
---|
516 | ("print", "Print options as parsed from the commandline.")
|
---|
517 | ("print-default", "Print options as parsed from default configuration file.")
|
---|
518 | ("print-database", "Print options as retrieved from the database.")
|
---|
519 | ("print-config", "Print options as parsed from the high priority configuration file.")
|
---|
520 | ("print-environment", "Print options as parsed from the environment.")
|
---|
521 | ("print-unknown", "Print unrecognized options.")
|
---|
522 | ("print-options", "Print options as passed to program.")
|
---|
523 | ("print-wildcards", "Print all options registered with wildcards.")
|
---|
524 | ("dont-check", "Do not check validity of options from files and database.")
|
---|
525 | ("dont-check-files", "Do not check validity of options from files.")
|
---|
526 | ("dont-check-database", "Do not check validity of options from database.")
|
---|
527 | ;
|
---|
528 |
|
---|
529 | po::options_description def_config;
|
---|
530 | def_config.add_options()
|
---|
531 | ("default", var<string>(fName+string(".rc")), "Default configuration file.")
|
---|
532 | ;
|
---|
533 |
|
---|
534 | po::options_description config("Configuration options");
|
---|
535 | config.add_options()
|
---|
536 | ("config,C", var<string>(), "Configuration file overwriting options retrieved from the database.")
|
---|
537 | ("database", var<string>(), "Database link as in\n\t[user[:password]@]server.com[:port]/database\nOverwrites options from the default configuration file.")
|
---|
538 | ("no-database", "Suppress any access to the database even if a database URL was set.")
|
---|
539 | ;
|
---|
540 |
|
---|
541 | fOptionsCommandline[kVisible].add(generic);
|
---|
542 | fOptionsCommandline[kVisible].add(config);
|
---|
543 | fOptionsCommandline[kVisible].add(def_config);
|
---|
544 | fOptionsConfigfile[kVisible].add(config);
|
---|
545 | }
|
---|
546 |
|
---|
547 | // --------------------------------------------------------------------------
|
---|
548 | //
|
---|
549 | //!
|
---|
550 | //
|
---|
551 | void Configuration::PrintParsed(const po::parsed_options &parsed) const
|
---|
552 | {
|
---|
553 | const vector< po::basic_option<char> >& options = parsed.options;
|
---|
554 |
|
---|
555 | // .description -> Pointer to opt_commandline
|
---|
556 | // const std::vector< shared_ptr<option_description> >& options() const;
|
---|
557 |
|
---|
558 | //const std::string& key(const std::string& option) const;
|
---|
559 | //const std::string& long_name() const;
|
---|
560 | //const std::string& description() const;
|
---|
561 | //shared_ptr<const value_semantic> semantic() const;
|
---|
562 |
|
---|
563 | int maxlen = 0;
|
---|
564 | for (unsigned i=0; i<options.size(); ++i)
|
---|
565 | {
|
---|
566 | const po::basic_option<char> &opt = options[i];
|
---|
567 |
|
---|
568 | if (opt.value.size()>0 && opt.string_key[0]!='-')
|
---|
569 | Max(maxlen, opt.string_key.length());
|
---|
570 | }
|
---|
571 |
|
---|
572 | cout.setf(ios_base::left);
|
---|
573 |
|
---|
574 | // =============> Implement prining of parsed options
|
---|
575 | for(unsigned i=0; i<options.size(); ++i)
|
---|
576 | {
|
---|
577 | const po::basic_option<char> &opt = options[i];
|
---|
578 |
|
---|
579 | if (opt.value.size()==0 && !opt.string_key[0]=='-')
|
---|
580 | cout << "--";
|
---|
581 | cout << setw(maxlen) << opt.string_key;
|
---|
582 | if (opt.value.size()>0)
|
---|
583 | cout << " = " << opt.value[0];
|
---|
584 |
|
---|
585 | //for (int j=0; j<options[i].value.size(); j++)
|
---|
586 | // cout << "\t = " << options[i].value[j];
|
---|
587 | //cout << "/" << options[i].original_tokens[0];
|
---|
588 |
|
---|
589 | ostringstream com;
|
---|
590 |
|
---|
591 | if (opt.position_key>=0)
|
---|
592 | com << " [position=" << opt.position_key << "]";
|
---|
593 | if (opt.unregistered)
|
---|
594 | com << " [unregistered]";
|
---|
595 |
|
---|
596 | if (!com.str().empty())
|
---|
597 | cout << " # " << com.str();
|
---|
598 |
|
---|
599 | cout << endl;
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | template<class T>
|
---|
604 | string Configuration::VecAsStr(const po::variable_value &v) const
|
---|
605 | {
|
---|
606 | ostringstream str;
|
---|
607 |
|
---|
608 | const vector<T> vec = v.as<vector<T>>();
|
---|
609 | for (typename std::vector<T>::const_iterator s=vec.begin(); s<vec.end(); s++)
|
---|
610 | str << " " << *s;
|
---|
611 |
|
---|
612 | return str.str().substr(1);
|
---|
613 | }
|
---|
614 |
|
---|
615 | string Configuration::VarAsStr(const po::variable_value &v) const
|
---|
616 | {
|
---|
617 | if (v.value().type()==typeid(bool))
|
---|
618 | return v.as<bool>() ? "yes ": "no";
|
---|
619 |
|
---|
620 | if (v.value().type()==typeid(string))
|
---|
621 | return v.as<string>();
|
---|
622 |
|
---|
623 | if (v.value().type()==typeid(int16_t))
|
---|
624 | return to_string((long long int)v.as<int16_t>());
|
---|
625 |
|
---|
626 | if (v.value().type()==typeid(int32_t))
|
---|
627 | return to_string((long long int)v.as<int32_t>());
|
---|
628 |
|
---|
629 | if (v.value().type()==typeid(int64_t))
|
---|
630 | return to_string((long long int)v.as<int64_t>());
|
---|
631 |
|
---|
632 | if (v.value().type()==typeid(uint16_t))
|
---|
633 | return to_string((long long unsigned int)v.as<uint16_t>());
|
---|
634 |
|
---|
635 | if (v.value().type()==typeid(uint32_t))
|
---|
636 | return to_string((long long unsigned int)v.as<uint32_t>());
|
---|
637 |
|
---|
638 | if (v.value().type()==typeid(uint64_t))
|
---|
639 | return to_string((long long unsigned int)v.as<uint64_t>());
|
---|
640 |
|
---|
641 | if (v.value().type()==typeid(float))
|
---|
642 | return to_string((long double)v.as<float>());
|
---|
643 |
|
---|
644 | if (v.value().type()==typeid(double))
|
---|
645 | return to_string((long double)v.as<double>());
|
---|
646 |
|
---|
647 | if (v.value().type()==typeid(vector<string>))
|
---|
648 | return VecAsStr<string>(v);
|
---|
649 |
|
---|
650 | if (v.value().type()==typeid(vector<int16_t>))
|
---|
651 | return VecAsStr<int16_t>(v);
|
---|
652 |
|
---|
653 | if (v.value().type()==typeid(vector<int32_t>))
|
---|
654 | return VecAsStr<int32_t>(v);
|
---|
655 |
|
---|
656 | if (v.value().type()==typeid(vector<int64_t>))
|
---|
657 | return VecAsStr<int64_t>(v);
|
---|
658 |
|
---|
659 | if (v.value().type()==typeid(vector<uint16_t>))
|
---|
660 | return VecAsStr<uint16_t>(v);
|
---|
661 |
|
---|
662 | if (v.value().type()==typeid(vector<uint32_t>))
|
---|
663 | return VecAsStr<uint32_t>(v);
|
---|
664 |
|
---|
665 | if (v.value().type()==typeid(vector<uint64_t>))
|
---|
666 | return VecAsStr<uint64_t>(v);
|
---|
667 |
|
---|
668 | if (v.value().type()==typeid(vector<float>))
|
---|
669 | return VecAsStr<float>(v);
|
---|
670 |
|
---|
671 | if (v.value().type()==typeid(vector<double>))
|
---|
672 | return VecAsStr<double>(v);
|
---|
673 |
|
---|
674 | ostringstream str;
|
---|
675 | str << hex << setfill('0') << "0x";
|
---|
676 | if (v.value().type()==typeid(Hex<uint16_t>))
|
---|
677 | str << setw(4) << v.as<Hex<uint16_t>>();
|
---|
678 |
|
---|
679 | if (v.value().type()==typeid(Hex<uint32_t>))
|
---|
680 | str << setw(8) << v.as<Hex<uint32_t>>();
|
---|
681 |
|
---|
682 | if (v.value().type()==typeid(Hex<uint64_t>))
|
---|
683 | str << setw(16) << v.as<Hex<uint64_t>>();
|
---|
684 |
|
---|
685 | return str.str();
|
---|
686 | }
|
---|
687 |
|
---|
688 | // --------------------------------------------------------------------------
|
---|
689 | //
|
---|
690 | //!
|
---|
691 | //
|
---|
692 | void Configuration::PrintOptions() const
|
---|
693 | {
|
---|
694 | cout << "Options propagated to program:" << endl;
|
---|
695 |
|
---|
696 | int maxlen = 0;
|
---|
697 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
698 | m!=fVariables.end(); m++)
|
---|
699 | Max(maxlen, m->first.length());
|
---|
700 |
|
---|
701 | cout.setf(ios_base::left);
|
---|
702 |
|
---|
703 | // =============> Implement prining of options in use
|
---|
704 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
705 | m!=fVariables.end(); m++)
|
---|
706 | {
|
---|
707 | const po::variable_value &v = m->second;
|
---|
708 |
|
---|
709 | ostringstream str;
|
---|
710 |
|
---|
711 | if (v.value().type()==typeid(bool))
|
---|
712 | str << " bool";
|
---|
713 | if (v.value().type()==typeid(string))
|
---|
714 | str << " string";
|
---|
715 | if (v.value().type()==typeid(int16_t))
|
---|
716 | str << " int16_t";
|
---|
717 | if (v.value().type()==typeid(int32_t))
|
---|
718 | str << " int32_t";
|
---|
719 | if (v.value().type()==typeid(int64_t))
|
---|
720 | str << " int64_t";
|
---|
721 | if (v.value().type()==typeid(uint16_t))
|
---|
722 | str << " uint16_t";
|
---|
723 | if (v.value().type()==typeid(uint32_t))
|
---|
724 | str << " uint32_t";
|
---|
725 | if (v.value().type()==typeid(uint64_t))
|
---|
726 | str << " uint64_t";
|
---|
727 | if (v.value().type()==typeid(float))
|
---|
728 | str << " float";
|
---|
729 | if (v.value().type()==typeid(double))
|
---|
730 | str << " double";
|
---|
731 | if (v.value().type()==typeid(Hex<uint16_t>))
|
---|
732 | str << " Hex<uint16_t>";
|
---|
733 | if (v.value().type()==typeid(Hex<uint32_t>))
|
---|
734 | str << " Hex<uint32_t>";
|
---|
735 | if (v.value().type()==typeid(Hex<uint64_t>))
|
---|
736 | str << " Hex<uint64_t>";
|
---|
737 | if (v.value().type()==typeid(vector<string>))
|
---|
738 | str << " vector<string>";
|
---|
739 | if (v.value().type()==typeid(vector<int16_t>))
|
---|
740 | str << " vector<int16_t>";
|
---|
741 | if (v.value().type()==typeid(vector<int32_t>))
|
---|
742 | str << " vector<int32_t>";
|
---|
743 | if (v.value().type()==typeid(vector<int64_t>))
|
---|
744 | str << " vector<int64_t>";
|
---|
745 | if (v.value().type()==typeid(vector<uint16_t>))
|
---|
746 | str << " vector<uint16_t>";
|
---|
747 | if (v.value().type()==typeid(vector<uint32_t>))
|
---|
748 | str << " vector<uint32_t>";
|
---|
749 | if (v.value().type()==typeid(vector<uint64_t>))
|
---|
750 | str << " vector<uint64_t>";
|
---|
751 | if (v.value().type()==typeid(vector<float>))
|
---|
752 | str << " vector<float>";
|
---|
753 | if (v.value().type()==typeid(vector<double>))
|
---|
754 | str << " vector<double>";
|
---|
755 |
|
---|
756 | if (str.str().empty())
|
---|
757 | str << " unknown[" << v.value().type().name() << "]";
|
---|
758 |
|
---|
759 | const string var = VarAsStr(v);
|
---|
760 | cout << setw(maxlen) << m->first;
|
---|
761 | if (!var.empty())
|
---|
762 | cout << " = ";
|
---|
763 | cout << var << " #" << str.str();
|
---|
764 |
|
---|
765 | if (v.defaulted())
|
---|
766 | cout << " [default]";
|
---|
767 | if (v.empty())
|
---|
768 | cout << " [empty]";
|
---|
769 |
|
---|
770 | cout << endl;
|
---|
771 | }
|
---|
772 |
|
---|
773 | cout << endl;
|
---|
774 | }
|
---|
775 |
|
---|
776 | // --------------------------------------------------------------------------
|
---|
777 | //
|
---|
778 | //!
|
---|
779 | //
|
---|
780 | void Configuration::PrintUnknown(const vector<string> &vec, int steps) const
|
---|
781 | {
|
---|
782 | for (vector<string>::const_iterator v=vec.begin(); v<vec.end(); v+=steps)
|
---|
783 | cout << " " << *v << endl;
|
---|
784 | cout << endl;
|
---|
785 | }
|
---|
786 |
|
---|
787 | multimap<string, string> Configuration::GetOptions() const
|
---|
788 | {
|
---|
789 | multimap<string,string> rc;
|
---|
790 |
|
---|
791 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
792 | m!=fVariables.end(); m++)
|
---|
793 | rc.insert(make_pair(m->first, VarAsStr(m->second)));
|
---|
794 |
|
---|
795 | return rc;
|
---|
796 | }
|
---|
797 |
|
---|
798 | // --------------------------------------------------------------------------
|
---|
799 | //
|
---|
800 | //!
|
---|
801 | //
|
---|
802 | void Configuration::PrintUnknown() const
|
---|
803 | {
|
---|
804 | if (fUnknownCommandline.size())
|
---|
805 | {
|
---|
806 | cout << "Unknown commandline options:" << endl;
|
---|
807 | PrintUnknown(fUnknownCommandline);
|
---|
808 | }
|
---|
809 |
|
---|
810 | if (fUnknownConfigfile.size())
|
---|
811 | {
|
---|
812 | cout << "Unknown options in configfile:" << endl;
|
---|
813 | PrintUnknown(fUnknownConfigfile, 2);
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (fUnknownEnvironment.size())
|
---|
817 | {
|
---|
818 | cout << "Unknown environment variables:" << endl;
|
---|
819 | PrintUnknown(fUnknownEnvironment);
|
---|
820 | }
|
---|
821 |
|
---|
822 | if (fUnknownDatabase.size())
|
---|
823 | {
|
---|
824 | cout << "Unknown database entry:" << endl;
|
---|
825 | PrintUnknown(fUnknownDatabase);
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | // --------------------------------------------------------------------------
|
---|
830 | //
|
---|
831 | //!
|
---|
832 | //
|
---|
833 | void Configuration::AddOptionsCommandline(const po::options_description &cl, bool visible)
|
---|
834 | {
|
---|
835 | fOptionsCommandline[visible].add(cl);
|
---|
836 | }
|
---|
837 |
|
---|
838 | // --------------------------------------------------------------------------
|
---|
839 | //
|
---|
840 | //!
|
---|
841 | //
|
---|
842 | void Configuration::AddOptionsConfigfile(const po::options_description &cf, bool visible)
|
---|
843 | {
|
---|
844 | fOptionsConfigfile[visible].add(cf);
|
---|
845 | }
|
---|
846 |
|
---|
847 | // --------------------------------------------------------------------------
|
---|
848 | //
|
---|
849 | //!
|
---|
850 | //
|
---|
851 | void Configuration::AddOptionsEnvironment(const po::options_description &env, bool visible)
|
---|
852 | {
|
---|
853 | fOptionsEnvironment[visible].add(env);
|
---|
854 | }
|
---|
855 |
|
---|
856 | // --------------------------------------------------------------------------
|
---|
857 | //
|
---|
858 | //!
|
---|
859 | //
|
---|
860 | void Configuration::AddOptionsDatabase(const po::options_description &db, bool visible)
|
---|
861 | {
|
---|
862 | fOptionsDatabase[visible].add(db);
|
---|
863 | }
|
---|
864 |
|
---|
865 | // --------------------------------------------------------------------------
|
---|
866 | //
|
---|
867 | //!
|
---|
868 | //
|
---|
869 | void Configuration::SetArgumentPositions(const po::positional_options_description &desc)
|
---|
870 | {
|
---|
871 | fArgumentPositions = desc;
|
---|
872 | }
|
---|
873 |
|
---|
874 | // --------------------------------------------------------------------------
|
---|
875 | //
|
---|
876 | //!
|
---|
877 | //
|
---|
878 | void Configuration::SetNameMapper(const function<string(string)> &func)
|
---|
879 | {
|
---|
880 | fNameMapper = func;
|
---|
881 | }
|
---|
882 |
|
---|
883 | void Configuration::SetNameMapper()
|
---|
884 | {
|
---|
885 | fNameMapper = bind1st(mem_fun(&Configuration::DefaultMapper), this);
|
---|
886 | }
|
---|
887 |
|
---|
888 | void Configuration::SetPrintUsage(const function<void(void)> &func)
|
---|
889 | {
|
---|
890 | fPrintUsage = func;
|
---|
891 | }
|
---|
892 |
|
---|
893 | void Configuration::SetPrintUsage()
|
---|
894 | {
|
---|
895 | fPrintUsage = bind(&Configuration::PrintUsage, this);
|
---|
896 | }
|
---|
897 |
|
---|
898 | void Configuration::SetPrintVersion(const function<void(const string&)> &func)
|
---|
899 | {
|
---|
900 | fPrintVersion = func;
|
---|
901 | }
|
---|
902 |
|
---|
903 | void Configuration::SetPrintVersion()
|
---|
904 | {
|
---|
905 | fPrintVersion = function<void(const string&)>();
|
---|
906 | }
|
---|
907 |
|
---|
908 | // --------------------------------------------------------------------------
|
---|
909 | //
|
---|
910 | //!
|
---|
911 | //! The idea of the Parse() memeber-function is to parse the command-line,
|
---|
912 | //! the configuration files, the databse and the environment and return
|
---|
913 | //! a proper combined result.
|
---|
914 | //!
|
---|
915 | //! In details the following actions are performed in the given order:
|
---|
916 | //!
|
---|
917 | //! - (0) Init local variables with the list of options described by the
|
---|
918 | //! data members.
|
---|
919 | //! - (1) Reset the data members fPriorityFile, fDefaultFile, fDatabase
|
---|
920 | //! - (2) Parse the command line
|
---|
921 | //! - (3) Check for \b --help* command-line options and performe
|
---|
922 | //! corresponding action
|
---|
923 | //! - (4) Check for \b --print and \b --print-all and perform corresponding
|
---|
924 | //! action
|
---|
925 | //! - (5) Read and parse the global configuration file, which is compiled
|
---|
926 | //! from the path corresponding to the argument given in the
|
---|
927 | //! constructor + "/fact++.rc", unrecognized options are always
|
---|
928 | //! allowed.
|
---|
929 | //! - (6) Read and parse the default configuration file, which is either
|
---|
930 | //! given by the default name or the \b --default command-line
|
---|
931 | //! option. The default name is compiled from the argument
|
---|
932 | //! given to the constructor and ".rc". If the file-name is
|
---|
933 | //! identical to the default (no command-line option given)
|
---|
934 | //! a missing configuration file is no error. Depending on
|
---|
935 | //! the \b --dont-check and \b --dont-check-files options,
|
---|
936 | //! unrecognized options in the file throw an exception or not.
|
---|
937 | //! - (7) Check for \b --print-default and \b --print-all and perform
|
---|
938 | //! corresponding action
|
---|
939 | //! - (8) Read and parse the priority configuration file, which must be given
|
---|
940 | //! by the \b --config or \b -C command-line option or a
|
---|
941 | //! corresponding entry in the default-configuration file.
|
---|
942 | //! If an option on the command-line and the in the configuration
|
---|
943 | //! file exists, the command-line option has priority.
|
---|
944 | //! If none is given, no priority file is read. Depending on
|
---|
945 | //! the \b --dont-check and \b --dont-check-files options,
|
---|
946 | //! unrecognized options in the file throw an exception or not.
|
---|
947 | //! - (9) Check for \b --print-config and \b --print-all and perform
|
---|
948 | //! corresponding action
|
---|
949 | //! - (10) Retrieve options from the database according to the
|
---|
950 | //! options \b --database and \b --no-database. Note that
|
---|
951 | //! options given on the command-line have highest priority.
|
---|
952 | //! The second priority is the priority-configuration file.
|
---|
953 | //! The options from the default configuration-file have
|
---|
954 | //! lowest priority.
|
---|
955 | //! - (11) Check for \b --print-database and \b --print-all and perform
|
---|
956 | //! corresponding action
|
---|
957 | //! - (12) Parse the environment options.
|
---|
958 | //! - (13) Check for \b --print-environment and \b --print-all and perform
|
---|
959 | //! corresponding action
|
---|
960 | //! - (14) Compile the final result. The priority of the options is (in
|
---|
961 | //! decreasing order): command-line options, options from the
|
---|
962 | //! priority configuration file, options from the database,
|
---|
963 | //! options from the default configuration-file and options
|
---|
964 | //! from the environment.
|
---|
965 | //! - (15) Find all options which were found and flagged as unrecognized,
|
---|
966 | //! because they are not in the user-defined list of described
|
---|
967 | //! options, are collected and stored in the corresponding
|
---|
968 | //! data-members.
|
---|
969 | //! - (16) Find all options which where registered with wildcards and
|
---|
970 | //! store the list in fWildcardOptions.
|
---|
971 | //! - (17) Before the function returns it check for \b --print-options
|
---|
972 | //! and \b --print-unknown and performs the corresponding actions.
|
---|
973 | //!
|
---|
974 | //!
|
---|
975 | //! @param argc,argv
|
---|
976 | //! arguments passed to <B>main(int argc, char **argv)</B>
|
---|
977 | //!
|
---|
978 | //! @returns
|
---|
979 | //! A reference to the list with the resulting options with their
|
---|
980 | //! values.
|
---|
981 | //!
|
---|
982 | //! @todo
|
---|
983 | //! - describe the exceptions
|
---|
984 | //! - describe what happens in a more general way
|
---|
985 | //! - print a waring when no default coonfig file is read
|
---|
986 | //! - proper handling and error messages if files not available
|
---|
987 | //
|
---|
988 | const po::variables_map &Configuration::Parse(int argc, const char **argv)
|
---|
989 | {
|
---|
990 | const po::positional_options_description &opt_positional = fArgumentPositions;
|
---|
991 |
|
---|
992 | // ------------------------ (0) --------------------------
|
---|
993 |
|
---|
994 | po::options_description opt_commandline;
|
---|
995 | po::options_description opt_configfile;
|
---|
996 | po::options_description opt_environment;
|
---|
997 | po::options_description opt_database;
|
---|
998 |
|
---|
999 | for (int i=0; i<2; i++)
|
---|
1000 | {
|
---|
1001 | opt_commandline.add(fOptionsCommandline[i]);
|
---|
1002 | opt_configfile.add(fOptionsConfigfile[i]);
|
---|
1003 | opt_environment.add(fOptionsEnvironment[i]);
|
---|
1004 | opt_database.add(fOptionsDatabase[i]);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | // ------------------------ (1) --------------------------
|
---|
1008 |
|
---|
1009 | fPriorityFile = "";
|
---|
1010 | fDefaultFile = "";
|
---|
1011 | fDatabase = "";
|
---|
1012 |
|
---|
1013 | // ------------------------ (2) --------------------------
|
---|
1014 |
|
---|
1015 | po::command_line_parser parser(argc, const_cast<char**>(argv));
|
---|
1016 | parser.options(opt_commandline);
|
---|
1017 | parser.positional(opt_positional);
|
---|
1018 | parser.style(style::unix_style&~style::allow_guessing);
|
---|
1019 | //parser.allow_unregistered();
|
---|
1020 |
|
---|
1021 | const po::parsed_options parsed_commandline = parser.run();
|
---|
1022 |
|
---|
1023 | // ------------------------ (3) --------------------------
|
---|
1024 |
|
---|
1025 | po::variables_map getfiles;
|
---|
1026 | po::store(parsed_commandline, getfiles);
|
---|
1027 |
|
---|
1028 | if (getfiles.count("version"))
|
---|
1029 | PrintVersion();
|
---|
1030 | if (getfiles.count("help"))
|
---|
1031 | {
|
---|
1032 | fPrintUsage();
|
---|
1033 | cout <<
|
---|
1034 | "Options:\n"
|
---|
1035 | "The following describes the available commandline options. "
|
---|
1036 | "For further details on how command line option are parsed "
|
---|
1037 | "and in which order which configuration sources are accessed "
|
---|
1038 | "please refer to the class reference of the Configuration class." << endl;
|
---|
1039 | cout << fOptionsCommandline[kVisible] << endl;
|
---|
1040 | }
|
---|
1041 | if (getfiles.count("help-config"))
|
---|
1042 | cout << fOptionsConfigfile[kVisible] << endl;
|
---|
1043 | if (getfiles.count("help-env"))
|
---|
1044 | cout << fOptionsEnvironment[kVisible] << endl;
|
---|
1045 | if (getfiles.count("help-database"))
|
---|
1046 | cout << fOptionsDatabase[kVisible] << endl;
|
---|
1047 |
|
---|
1048 | // ------------------------ (4) --------------------------
|
---|
1049 |
|
---|
1050 | if (getfiles.count("print") || getfiles.count("print-all"))
|
---|
1051 | {
|
---|
1052 | cout << endl << "Parsed commandline options:" << endl;
|
---|
1053 | PrintParsed(parsed_commandline);
|
---|
1054 | cout << endl;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | // ------------------------ (5) --------------------------
|
---|
1058 |
|
---|
1059 | const boost::filesystem::path path(GetName());
|
---|
1060 | const string globalfile = (path.parent_path()/boost::filesystem::path("fact++.rc")).string();
|
---|
1061 |
|
---|
1062 | cerr << "Reading global options from '" << globalfile << "'." << endl;
|
---|
1063 |
|
---|
1064 | ifstream gfile(globalfile.c_str());
|
---|
1065 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1066 | const po::parsed_options parsed_globalfile =
|
---|
1067 | !gfile ?
|
---|
1068 | po::parsed_options(&opt_configfile) :
|
---|
1069 | po::parse_config_file<char>(gfile, opt_configfile, false);
|
---|
1070 |
|
---|
1071 | // ------------------------ (6) --------------------------
|
---|
1072 |
|
---|
1073 | // Get default file from command line
|
---|
1074 | if (getfiles.count("default"))
|
---|
1075 | {
|
---|
1076 | fDefaultFile = getfiles["default"].as<string>();
|
---|
1077 | cerr << "Reading default options from '" << fDefaultFile << "'." << endl;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | const bool checkf = !getfiles.count("dont-check-files") && !getfiles.count("dont-check");
|
---|
1081 | const bool defaulted = getfiles.count("default") && getfiles["default"].defaulted();
|
---|
1082 | //const bool exists = boost::filesystem::exists(fDefaultFile);
|
---|
1083 |
|
---|
1084 | ifstream indef(fDefaultFile.c_str());
|
---|
1085 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1086 | const po::parsed_options parsed_defaultfile =
|
---|
1087 | !indef && defaulted ?
|
---|
1088 | po::parsed_options(&opt_configfile) :
|
---|
1089 | po::parse_config_file<char>(indef, opt_configfile, !checkf);
|
---|
1090 |
|
---|
1091 | // ------------------------ (7) --------------------------
|
---|
1092 |
|
---|
1093 | if (getfiles.count("print-default") || getfiles.count("print-all"))
|
---|
1094 | {
|
---|
1095 | if (!indef.is_open() && defaulted)
|
---|
1096 | cout << "No configuration file by --default option specified." << endl;
|
---|
1097 | else
|
---|
1098 | {
|
---|
1099 | cout << endl << "Parsed options from '" << fDefaultFile << "':" << endl;
|
---|
1100 | PrintParsed(parsed_defaultfile);
|
---|
1101 | cout << endl;
|
---|
1102 | }
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | po::store(parsed_defaultfile, getfiles);
|
---|
1106 |
|
---|
1107 | // ------------------------ (8) --------------------------
|
---|
1108 |
|
---|
1109 | // Get priority from commandline(1), defaultfile(2)
|
---|
1110 | if (getfiles.count("config"))
|
---|
1111 | {
|
---|
1112 | fPriorityFile = getfiles["config"].as<string>();
|
---|
1113 | cerr << "Reading config options from '" << fPriorityFile << "'." << endl;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | ifstream inpri(fPriorityFile.c_str());
|
---|
1117 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1118 | const po::parsed_options parsed_priorityfile =
|
---|
1119 | fPriorityFile.empty() ? po::parsed_options(&opt_configfile) :
|
---|
1120 | po::parse_config_file<char>(inpri, opt_configfile, !checkf);
|
---|
1121 |
|
---|
1122 | // ------------------------ (9) --------------------------
|
---|
1123 |
|
---|
1124 | if (getfiles.count("print-config") || getfiles.count("print-all"))
|
---|
1125 | {
|
---|
1126 | if (fPriorityFile.empty())
|
---|
1127 | cout << "No configuration file by --config option specified." << endl;
|
---|
1128 | else
|
---|
1129 | {
|
---|
1130 | cout << endl << "Parsed options from '" << fPriorityFile << "':" << endl;
|
---|
1131 | PrintParsed(parsed_priorityfile);
|
---|
1132 | cout << endl;
|
---|
1133 | }
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | // ------------------------ (10) -------------------------
|
---|
1137 |
|
---|
1138 | po::variables_map getdatabase;
|
---|
1139 | po::store(parsed_commandline, getdatabase);
|
---|
1140 | po::store(parsed_priorityfile, getdatabase);
|
---|
1141 | po::store(parsed_defaultfile, getdatabase);
|
---|
1142 | po::store(parsed_globalfile, getdatabase);
|
---|
1143 |
|
---|
1144 | if (getdatabase.count("database") && !getdatabase.count("no-database"))
|
---|
1145 | {
|
---|
1146 | fDatabase = getdatabase["database"].as<string>();
|
---|
1147 | cerr << "Requesting options from database for '" << fName << "'" << endl;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | const bool checkdb = !getdatabase.count("dont-check-database") && !getdatabase.count("dont-check");
|
---|
1151 |
|
---|
1152 | const po::parsed_options parsed_database =
|
---|
1153 | fDatabase.empty() ? po::parsed_options(&opt_database) :
|
---|
1154 | parse_database(path.filename(), fDatabase, opt_database, !checkdb);
|
---|
1155 |
|
---|
1156 | // ------------------------ (11) -------------------------
|
---|
1157 |
|
---|
1158 | if (getfiles.count("print-database") || getfiles.count("print-all"))
|
---|
1159 | {
|
---|
1160 | if (fDatabase.empty())
|
---|
1161 | cout << "No database access requested." << endl;
|
---|
1162 | else
|
---|
1163 | {
|
---|
1164 | cout << endl << "Options received from '" << fDatabase << "':" << endl;
|
---|
1165 | PrintParsed(parsed_database);
|
---|
1166 | cout << endl;
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | // ------------------------ (12) -------------------------
|
---|
1171 |
|
---|
1172 | const po::parsed_options parsed_environment = po::parse_environment(opt_environment, fNameMapper);
|
---|
1173 |
|
---|
1174 | // ------------------------ (13) -------------------------
|
---|
1175 |
|
---|
1176 | if (getfiles.count("print-environment"))
|
---|
1177 | {
|
---|
1178 | cout << "Parsed options from environment:" << endl;
|
---|
1179 | PrintParsed(parsed_environment);
|
---|
1180 | cout << endl;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | // ------------------------ (14) -------------------------
|
---|
1184 | po::variables_map result;
|
---|
1185 | po::store(parsed_commandline, result);
|
---|
1186 | po::store(parsed_priorityfile, result);
|
---|
1187 | po::store(parsed_database, result);
|
---|
1188 | po::store(parsed_defaultfile, result);
|
---|
1189 | po::store(parsed_globalfile, result);
|
---|
1190 | po::store(parsed_environment, result);
|
---|
1191 | po::notify(result);
|
---|
1192 |
|
---|
1193 | fVariables = result;
|
---|
1194 |
|
---|
1195 | // ------------------------ (15) -------------------------
|
---|
1196 |
|
---|
1197 | const vector<string> unknown0 = collect_unrecognized(parsed_globalfile.options, po::exclude_positional);
|
---|
1198 | const vector<string> unknown1 = collect_unrecognized(parsed_defaultfile.options, po::exclude_positional);
|
---|
1199 | const vector<string> unknown2 = collect_unrecognized(parsed_priorityfile.options, po::exclude_positional);
|
---|
1200 |
|
---|
1201 | fUnknownConfigfile.clear();
|
---|
1202 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown0.begin(), unknown0.end());
|
---|
1203 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown1.begin(), unknown1.end());
|
---|
1204 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown2.begin(), unknown2.end());
|
---|
1205 |
|
---|
1206 | fUnknownCommandline = collect_unrecognized(parsed_commandline.options, po::exclude_positional);
|
---|
1207 | fUnknownEnvironment = collect_unrecognized(parsed_environment.options, po::exclude_positional);
|
---|
1208 | fUnknownDatabase = collect_unrecognized(parsed_database.options, po::exclude_positional);
|
---|
1209 |
|
---|
1210 | // ------------------------ (16) -------------------------
|
---|
1211 |
|
---|
1212 | CreateWildcardOptions();
|
---|
1213 |
|
---|
1214 | // ------------------------ (17) -------------------------
|
---|
1215 |
|
---|
1216 | if (result.count("print-options"))
|
---|
1217 | PrintOptions();
|
---|
1218 |
|
---|
1219 | if (result.count("print-wildcards"))
|
---|
1220 | PrintWildcardOptions();
|
---|
1221 |
|
---|
1222 | if (result.count("print-unknown"))
|
---|
1223 | PrintUnknown();
|
---|
1224 |
|
---|
1225 | return fVariables;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | bool Configuration::DoParse(int argc, const char **argv, const std::function<void()> &PrintHelp)
|
---|
1229 | {
|
---|
1230 | try
|
---|
1231 | {
|
---|
1232 | Parse(argc, argv);
|
---|
1233 | }
|
---|
1234 | #if BOOST_VERSION > 104000
|
---|
1235 | catch (po::multiple_occurrences &e)
|
---|
1236 | {
|
---|
1237 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
---|
1238 | return false;
|
---|
1239 | }
|
---|
1240 | #endif
|
---|
1241 | catch (exception& e)
|
---|
1242 | {
|
---|
1243 | cerr << "Program options invalid due to: " << e.what() << endl;
|
---|
1244 | return false;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | if (HasVersion() || HasPrint())
|
---|
1248 | return false;
|
---|
1249 |
|
---|
1250 | if (HasHelp())
|
---|
1251 | {
|
---|
1252 | if (PrintHelp)
|
---|
1253 | PrintHelp();
|
---|
1254 | return false;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | return true;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | // --------------------------------------------------------------------------
|
---|
1261 | //
|
---|
1262 | //! Create a list of all options which were registered using wildcards
|
---|
1263 | //!
|
---|
1264 | void Configuration::CreateWildcardOptions()
|
---|
1265 | {
|
---|
1266 | po::options_description opts;
|
---|
1267 |
|
---|
1268 | for (int i=0; i<2; i++)
|
---|
1269 | {
|
---|
1270 | opts.add(fOptionsCommandline[i]);
|
---|
1271 | opts.add(fOptionsConfigfile[i]);
|
---|
1272 | opts.add(fOptionsEnvironment[i]);
|
---|
1273 | opts.add(fOptionsDatabase[i]);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | fWildcardOptions.clear();
|
---|
1277 |
|
---|
1278 | typedef map<string,po::variable_value> Vars;
|
---|
1279 | typedef vector<boost::shared_ptr<po::option_description>> Descs;
|
---|
1280 |
|
---|
1281 | const Descs &desc = opts.options();
|
---|
1282 |
|
---|
1283 | for (Vars::const_iterator io=fVariables.begin(); io!=fVariables.end(); io++)
|
---|
1284 | {
|
---|
1285 | for (Descs::const_iterator id=desc.begin(); id!=desc.end(); id++)
|
---|
1286 | #if BOOST_VERSION > 104000
|
---|
1287 | if ((*id)->match(io->first, false, false, false)==po::option_description::approximate_match)
|
---|
1288 | #else
|
---|
1289 | if ((*id)->match(io->first, false)==po::option_description::approximate_match)
|
---|
1290 | #endif
|
---|
1291 | fWildcardOptions[io->first] = (*id)->long_name();
|
---|
1292 | }
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | // --------------------------------------------------------------------------
|
---|
1296 | //
|
---|
1297 | //! Print a list of all options which were registered using wildcards and
|
---|
1298 | //! have not be registered subsequently by access.
|
---|
1299 | //!
|
---|
1300 | void Configuration::PrintWildcardOptions() const
|
---|
1301 | {
|
---|
1302 | cout << "Options registered with wildcards and not yet accessed:" << endl;
|
---|
1303 |
|
---|
1304 | size_t max = 0;
|
---|
1305 | for (map<string,string>::const_iterator it=fWildcardOptions.begin(); it!=fWildcardOptions.end(); it++)
|
---|
1306 | if (it->second.length()>max)
|
---|
1307 | max = it->second.length();
|
---|
1308 |
|
---|
1309 | cout.setf(ios_base::left);
|
---|
1310 | for (map<string,string>::const_iterator it=fWildcardOptions.begin(); it!=fWildcardOptions.end(); it++)
|
---|
1311 | cout << setw(max+1) << it->second << " : " << it->first <<endl;
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | // --------------------------------------------------------------------------
|
---|
1315 | //
|
---|
1316 | //! Removes /.libs/lt- from a path or just lt- from the filename.
|
---|
1317 | //!
|
---|
1318 | //! @param src
|
---|
1319 | //! input path with filename
|
---|
1320 | //! @returns
|
---|
1321 | //! path cleaned from libtool extensions
|
---|
1322 | //!
|
---|
1323 | string Configuration::UnLibToolize(const string &src) const
|
---|
1324 | {
|
---|
1325 | const boost::filesystem::path path(src);
|
---|
1326 |
|
---|
1327 | string pname = path.parent_path().string();
|
---|
1328 | string fname = path.filename();
|
---|
1329 |
|
---|
1330 | // If the filename starts with "lt-" remove it from the name
|
---|
1331 | if (fname.substr(0, 3)=="lt-")
|
---|
1332 | fname = fname.substr(3);
|
---|
1333 |
|
---|
1334 | string pwd;
|
---|
1335 | // If no directory is contained determine the current directory
|
---|
1336 | if (pname.empty())
|
---|
1337 | pname = boost::filesystem::current_path().string();
|
---|
1338 |
|
---|
1339 | // If the directory is relative and just ".libs" forget about it
|
---|
1340 | if (pname==".libs")
|
---|
1341 | return fname;
|
---|
1342 |
|
---|
1343 |
|
---|
1344 | // Check if the directory is long enough to contain "/.libs"
|
---|
1345 | if (pname.length()>=6)
|
---|
1346 | {
|
---|
1347 | // If the directory ends with "/.libs", remove it
|
---|
1348 | const size_t pos = pname.length()-6;
|
---|
1349 | if (pname.substr(pos)=="/.libs")
|
---|
1350 | pname = pname.substr(0, pos);
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | // If the path is the local path do not return the path-name
|
---|
1354 | if (pname==boost::filesystem::current_path().string())
|
---|
1355 | return fname;
|
---|
1356 |
|
---|
1357 | return pname+'/'+fname;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | // --------------------------------------------------------------------------
|
---|
1361 | //
|
---|
1362 | //! Print version information about the program and package.
|
---|
1363 | //!
|
---|
1364 | //! The program name is taken from fName. If a leading "lt-" is found,
|
---|
1365 | //! it is removed. This is useful if the program was build and run
|
---|
1366 | //! using libtool.
|
---|
1367 | //!
|
---|
1368 | //! The package name is taken from the define PACKAGE_STRING. If it is
|
---|
1369 | //! not defined (like automatically done by autoconf) no package information
|
---|
1370 | //! is printed. The same is true for PACKAGE_URL and PACKAGE_BUGREPORT.
|
---|
1371 | //!
|
---|
1372 | //! From help2man:
|
---|
1373 | //!
|
---|
1374 | //! The first line of the --version information is assumed to be in one
|
---|
1375 | //! of the following formats:
|
---|
1376 | //!
|
---|
1377 | //! \verbatim
|
---|
1378 | //! - <version>
|
---|
1379 | //! - <program> <version>
|
---|
1380 | //! - {GNU,Free} <program> <version>
|
---|
1381 | //! - <program> ({GNU,Free} <package>) <version>
|
---|
1382 | //! - <program> - {GNU,Free} <package> <version>
|
---|
1383 | //! \endverbatim
|
---|
1384 | //!
|
---|
1385 | //! and separated from any copyright/author details by a blank line.
|
---|
1386 | //!
|
---|
1387 | //! Handle multi-line bug reporting sections of the form:
|
---|
1388 | //!
|
---|
1389 | //! \verbatim
|
---|
1390 | //! - Report <program> bugs to <addr>
|
---|
1391 | //! - GNU <package> home page: <url>
|
---|
1392 | //! - ...
|
---|
1393 | //! \endverbatim
|
---|
1394 | //!
|
---|
1395 | //! @param name
|
---|
1396 | //! name of the program (usually argv[0]).
|
---|
1397 | //!
|
---|
1398 | void Configuration::PrintVersion() const
|
---|
1399 | {
|
---|
1400 | #ifndef PACKAGE_STRING
|
---|
1401 | #define PACKAGE_STRING ""
|
---|
1402 | #endif
|
---|
1403 |
|
---|
1404 | #ifndef PACKAGE_URL
|
---|
1405 | #define PACKAGE_URL ""
|
---|
1406 | #endif
|
---|
1407 |
|
---|
1408 | #ifndef PACKAGE_BUGREPORT
|
---|
1409 | #define PACKAGE_BUGREPORT ""
|
---|
1410 | #endif
|
---|
1411 |
|
---|
1412 | if (fPrintVersion)
|
---|
1413 | {
|
---|
1414 | fPrintVersion(fName);
|
---|
1415 | return;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | const std::string n = boost::filesystem::path(fName).filename();
|
---|
1419 |
|
---|
1420 | const string name = PACKAGE_STRING;
|
---|
1421 | const string bugs = PACKAGE_BUGREPORT;
|
---|
1422 | const string url = PACKAGE_URL;
|
---|
1423 |
|
---|
1424 | cout << n;
|
---|
1425 | if (!name.empty())
|
---|
1426 | cout << " - " << name;
|
---|
1427 | cout <<
|
---|
1428 | "\n\n"
|
---|
1429 | "Written by Thomas Bretz et al.\n"
|
---|
1430 | "\n";
|
---|
1431 | if (!bugs.empty())
|
---|
1432 | cout << "Report bugs to <" << bugs << ">\n";
|
---|
1433 | if (!url.empty())
|
---|
1434 | cout << "Home page: " << url << "\n";
|
---|
1435 | cout <<
|
---|
1436 | "\n"
|
---|
1437 | "Copyright (C) 2011 by the FACT Collaboration.\n"
|
---|
1438 | "This is free software; see the source for copying conditions.\n"
|
---|
1439 | << std::endl;
|
---|
1440 | }
|
---|