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/bind.hpp>
|
---|
335 | #include <boost/regex.hpp>
|
---|
336 | #include <boost/filesystem.hpp>
|
---|
337 |
|
---|
338 | #define HAS_SQL
|
---|
339 |
|
---|
340 | #ifdef HAS_SQL
|
---|
341 | #include <mysql++/mysql++.h>
|
---|
342 | #endif
|
---|
343 |
|
---|
344 | using namespace std;
|
---|
345 |
|
---|
346 | namespace style = boost::program_options::command_line_style;
|
---|
347 |
|
---|
348 | // --------------------------------------------------------------------------
|
---|
349 | //
|
---|
350 | //! The purpose of this function is basically to connect to the database,
|
---|
351 | //! and retrieve all the options entries from the 'Configuration' table.
|
---|
352 | //!
|
---|
353 | //! @param database
|
---|
354 | //! The URL of the database from which the configuration data is
|
---|
355 | //! retrieved. It should be given in the form
|
---|
356 | //! \li [user[:password]@]server.com[:port]/database
|
---|
357 | //!
|
---|
358 | //! with
|
---|
359 | //! - user: user name (default is the current user)
|
---|
360 | //! - password: necessary if required by the database rights
|
---|
361 | //! - server: the URL of the server (can be 'localhost')
|
---|
362 | //! - port: the port to which to connect (usually obsolete)
|
---|
363 | //! - database: The name of the database containing the table
|
---|
364 | //!
|
---|
365 | //! @param desc
|
---|
366 | //! A reference to the object with the description of the options
|
---|
367 | //! which should be retrieved.
|
---|
368 | //!
|
---|
369 | //! @param allow_unregistered
|
---|
370 | //! If this is true also unregistered, i.e. options unknown to desc,
|
---|
371 | //! are returned. Otherwise an exception is thrown if such an option
|
---|
372 | //! was retrieved.
|
---|
373 | //!
|
---|
374 | //! @return
|
---|
375 | //! Return an object of type basic_parsed_options containing all
|
---|
376 | //! the entries retrieved from the database. Options not found in
|
---|
377 | //! desc are flagged as unregistered.
|
---|
378 | //!
|
---|
379 | //! @throws
|
---|
380 | //! Two types of exceptions are thrown
|
---|
381 | //! - It thows an unnamed exception if the options could not be
|
---|
382 | //! retrieved properly from the databse.
|
---|
383 | //! - If an option is not registered within the given descriptions
|
---|
384 | //! and \b allow_unregistered is \b false, an exception of type
|
---|
385 | //! \b po::unknown_option is thrown.
|
---|
386 | //!
|
---|
387 | //! @todo
|
---|
388 | //! - The exceptions handling should be improved.
|
---|
389 | //! - The final database layout is missing in the description
|
---|
390 | //! - Shell we allow options to be given more than once?
|
---|
391 | //
|
---|
392 | #ifdef HAS_SQL
|
---|
393 | po::basic_parsed_options<char>
|
---|
394 | Configuration::parse_database(const string &database, const po::options_description& desc, bool allow_unregistered)
|
---|
395 | {
|
---|
396 | //static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))?");
|
---|
397 | static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
|
---|
398 | // 2: user
|
---|
399 | // 4: pass
|
---|
400 | // 5: server
|
---|
401 | // 7: port
|
---|
402 | // 9: db
|
---|
403 |
|
---|
404 | boost::smatch what;
|
---|
405 | if (!boost::regex_match(database, what, expr, boost::match_extra))
|
---|
406 | {
|
---|
407 | cout << "Couldn't parse '" << database << "'." << endl;
|
---|
408 | throw;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (what.size()!=10)
|
---|
412 | {
|
---|
413 | cout << "Error parsing '" << database << "'." << endl;
|
---|
414 | throw;
|
---|
415 | }
|
---|
416 |
|
---|
417 | const string user = what[2];
|
---|
418 | const string passwd = what[4];
|
---|
419 | const string server = what[5];
|
---|
420 | const string db = what[9];
|
---|
421 | const int port = stoi(string(what[7]));
|
---|
422 |
|
---|
423 | cout << "Connecting to '";
|
---|
424 | if (!user.empty())
|
---|
425 | cout << user << "@";
|
---|
426 | cout << server;
|
---|
427 | if (port)
|
---|
428 | cout << ":" << port;
|
---|
429 | if (!db.empty())
|
---|
430 | cout << "/" << db;
|
---|
431 | cout << "'" << endl;
|
---|
432 |
|
---|
433 | mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
|
---|
434 | /* throws exceptions
|
---|
435 | if (!conn.connected())
|
---|
436 | {
|
---|
437 | cout << "MySQL connection error: " << conn.error() << endl;
|
---|
438 | throw;
|
---|
439 | }*/
|
---|
440 |
|
---|
441 | // Retrieve a subset of the sample stock table set up by resetdb
|
---|
442 | // and display it.
|
---|
443 | // FIXME: What about a prefix?
|
---|
444 | const mysqlpp::StoreQueryResult res = conn.query("select `Key`, Value from Configuration").store();
|
---|
445 | /* throws exceptions
|
---|
446 | if (!res)
|
---|
447 | {
|
---|
448 | cout << "MySQL query failed: " << query.error() << endl;
|
---|
449 | throw;
|
---|
450 | }*/
|
---|
451 |
|
---|
452 | set<string> allowed_options;
|
---|
453 |
|
---|
454 | const vector<boost::shared_ptr<po::option_description>> &options = desc.options();
|
---|
455 | for (unsigned i=0; i<options.size(); ++i)
|
---|
456 | {
|
---|
457 | const po::option_description &d = *options[i];
|
---|
458 | if (d.long_name().empty())
|
---|
459 | boost::throw_exception(po::error("long name required for database"));
|
---|
460 |
|
---|
461 | allowed_options.insert(d.long_name());
|
---|
462 | }
|
---|
463 |
|
---|
464 | po::parsed_options result(&desc);
|
---|
465 |
|
---|
466 | for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
|
---|
467 | {
|
---|
468 | const string key = (*v)[0].c_str();
|
---|
469 | if (key.empty()) // key == > Throw exception
|
---|
470 | continue;
|
---|
471 |
|
---|
472 | // Check if we are allowed to accept unregistered options,
|
---|
473 | // i.e. options which are not in options_description &desc.
|
---|
474 | const bool unregistered = allowed_options.find(key)==allowed_options.end();
|
---|
475 | if (unregistered && allow_unregistered)
|
---|
476 | boost::throw_exception(po::unknown_option(key));
|
---|
477 |
|
---|
478 | // Create a key/value-pair and store whether it is a
|
---|
479 | // registered option of not
|
---|
480 | po::option n;
|
---|
481 | n.string_key = key;
|
---|
482 | // This is now identical to file parsing. What if we want
|
---|
483 | // to concatenate options like on the command line?
|
---|
484 | n.value.clear(); // Fixme: composing?
|
---|
485 | n.value.push_back((*v)[1].c_str());
|
---|
486 | n.unregistered = unregistered;
|
---|
487 |
|
---|
488 | // If any parsing will be done in the future...
|
---|
489 | //n.value().original_tokens.clear();
|
---|
490 | //n.value().original_tokens.push_back(name);
|
---|
491 | //n.value().original_tokens.push_back(value);
|
---|
492 |
|
---|
493 | result.options.push_back(n);
|
---|
494 | }
|
---|
495 |
|
---|
496 | cout << endl;
|
---|
497 |
|
---|
498 | return result;
|
---|
499 | }
|
---|
500 | #else
|
---|
501 | po::basic_parsed_options<char>
|
---|
502 | Configuration::parse_database(const string &, const po::options_description &desc, bool)
|
---|
503 | {
|
---|
504 | return po::parsed_options(&desc);
|
---|
505 | }
|
---|
506 | #endif
|
---|
507 |
|
---|
508 | // --------------------------------------------------------------------------
|
---|
509 | //
|
---|
510 | //!
|
---|
511 | //
|
---|
512 | Configuration::Configuration(const string &prgname) : fName(UnLibToolize(prgname)),
|
---|
513 | fNameMapper(bind1st(mem_fun(&Configuration::DefaultMapper), this)),
|
---|
514 | fPrintUsage(boost::bind(&Configuration::PrintUsage, this))
|
---|
515 | {
|
---|
516 | po::options_description generic("Generic options");
|
---|
517 | generic.add_options()
|
---|
518 | ("version,V", "Print version information.")
|
---|
519 | ("help", "Print available commandline options.")
|
---|
520 | ("help-environment", "Print available environment variables.")
|
---|
521 | ("help-database", "Print available options retreived from the database.")
|
---|
522 | ("help-config", "Print available configuration file options.")
|
---|
523 | ("print-all", "Print all options as parsed from all the different sources.")
|
---|
524 | ("print", "Print options as parsed from the commandline.")
|
---|
525 | ("print-default", "Print options as parsed from default configuration file.")
|
---|
526 | ("print-database", "Print options as retrieved from the database.")
|
---|
527 | ("print-config", "Print options as parsed from the high priority configuration file.")
|
---|
528 | ("print-environment", "Print options as parsed from the environment.")
|
---|
529 | ("print-unknown", "Print unrecognized options.")
|
---|
530 | ("print-options", "Print options as passed to program.")
|
---|
531 | ("dont-check", "Do not check validity of options from files and database.")
|
---|
532 | ("dont-check-files", "Do not check validity of options from files.")
|
---|
533 | ("dont-check-database", "Do not check validity of options from database.")
|
---|
534 | ;
|
---|
535 |
|
---|
536 | po::options_description def_config;
|
---|
537 | def_config.add_options()
|
---|
538 | ("default", var<string>(fName+string(".rc")), "Default configuration file.")
|
---|
539 | ;
|
---|
540 |
|
---|
541 | po::options_description config("Configuration options");
|
---|
542 | config.add_options()
|
---|
543 | ("config,C", var<string>(), "Configuration file overwriting options retrieved from the database.")
|
---|
544 | ("database", var<string>(), "Database link as in\n\t[user[:password]@]server.com[:port]/database\nOverwrites options from the default configuration file.")
|
---|
545 | ("no-database", "Suppress any access to the database even if a database URL was set.")
|
---|
546 | ;
|
---|
547 |
|
---|
548 | fOptionsCommandline[kVisible].add(generic);
|
---|
549 | fOptionsCommandline[kVisible].add(config);
|
---|
550 | fOptionsCommandline[kVisible].add(def_config);
|
---|
551 | fOptionsConfigfile[kVisible].add(config);
|
---|
552 | }
|
---|
553 |
|
---|
554 | // --------------------------------------------------------------------------
|
---|
555 | //
|
---|
556 | //!
|
---|
557 | //
|
---|
558 | void Configuration::PrintParsed(const po::parsed_options &parsed) const
|
---|
559 | {
|
---|
560 | const vector< po::basic_option<char> >& options = parsed.options;
|
---|
561 |
|
---|
562 | // .description -> Pointer to opt_commandline
|
---|
563 | // const std::vector< shared_ptr<option_description> >& options() const;
|
---|
564 |
|
---|
565 | //const std::string& key(const std::string& option) const;
|
---|
566 | //const std::string& long_name() const;
|
---|
567 | //const std::string& description() const;
|
---|
568 | //shared_ptr<const value_semantic> semantic() const;
|
---|
569 |
|
---|
570 | int maxlen = 0;
|
---|
571 | for (unsigned i=0; i<options.size(); ++i)
|
---|
572 | {
|
---|
573 | const po::basic_option<char> &opt = options[i];
|
---|
574 |
|
---|
575 | if (opt.value.size()>0 && opt.string_key[0]!='-')
|
---|
576 | Max(maxlen, opt.string_key.length());
|
---|
577 | }
|
---|
578 |
|
---|
579 | cout.setf(ios_base::left);
|
---|
580 |
|
---|
581 | // =============> Implement prining of parsed options
|
---|
582 | for(unsigned i=0; i<options.size(); ++i)
|
---|
583 | {
|
---|
584 | const po::basic_option<char> &opt = options[i];
|
---|
585 |
|
---|
586 | if (opt.value.size()==0 && !opt.string_key[0]=='-')
|
---|
587 | cout << "--";
|
---|
588 | cout << setw(maxlen) << opt.string_key;
|
---|
589 | if (opt.value.size()>0)
|
---|
590 | cout << " = " << opt.value[0];
|
---|
591 |
|
---|
592 | //for (int j=0; j<options[i].value.size(); j++)
|
---|
593 | // cout << "\t = " << options[i].value[j];
|
---|
594 |
|
---|
595 | //cout << "/" << options[i].position_key;
|
---|
596 | //cout << "/" << options[i].original_tokens[0];
|
---|
597 | //cout << "/" << options[i].unregistered << endl;
|
---|
598 | if (opt.unregistered)
|
---|
599 | cout << " # option unknown";
|
---|
600 | cout << endl;
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | template<class T>
|
---|
605 | string Configuration::VecAsStr(const po::variable_value &v) const
|
---|
606 | {
|
---|
607 | ostringstream str;
|
---|
608 |
|
---|
609 | const vector<T> vec = v.as<vector<T>>();
|
---|
610 | for (typename std::vector<T>::const_iterator s=vec.begin(); s<vec.end(); s++)
|
---|
611 | str << " " << *s;
|
---|
612 |
|
---|
613 | return str.str().substr(1);
|
---|
614 | }
|
---|
615 |
|
---|
616 | string Configuration::VarAsStr(const po::variable_value &v) const
|
---|
617 | {
|
---|
618 | if (v.value().type()==typeid(bool))
|
---|
619 | return v.as<bool>() ? "yes ": "no";
|
---|
620 |
|
---|
621 | if (v.value().type()==typeid(string))
|
---|
622 | return v.as<string>();
|
---|
623 |
|
---|
624 | if (v.value().type()==typeid(int16_t))
|
---|
625 | return to_string((long long int)v.as<int16_t>());
|
---|
626 |
|
---|
627 | if (v.value().type()==typeid(int32_t))
|
---|
628 | return to_string((long long int)v.as<int32_t>());
|
---|
629 |
|
---|
630 | if (v.value().type()==typeid(int64_t))
|
---|
631 | return to_string((long long int)v.as<int64_t>());
|
---|
632 |
|
---|
633 | if (v.value().type()==typeid(uint16_t))
|
---|
634 | return to_string((long long unsigned int)v.as<uint16_t>());
|
---|
635 |
|
---|
636 | if (v.value().type()==typeid(uint32_t))
|
---|
637 | return to_string((long long unsigned int)v.as<uint32_t>());
|
---|
638 |
|
---|
639 | if (v.value().type()==typeid(uint64_t))
|
---|
640 | return to_string((long long unsigned int)v.as<uint64_t>());
|
---|
641 |
|
---|
642 | if (v.value().type()==typeid(float))
|
---|
643 | return to_string((long double)v.as<float>());
|
---|
644 |
|
---|
645 | if (v.value().type()==typeid(double))
|
---|
646 | return to_string((long double)v.as<double>());
|
---|
647 |
|
---|
648 | if (v.value().type()==typeid(vector<string>))
|
---|
649 | return VecAsStr<string>(v);
|
---|
650 |
|
---|
651 | if (v.value().type()==typeid(vector<int16_t>))
|
---|
652 | return VecAsStr<int16_t>(v);
|
---|
653 |
|
---|
654 | if (v.value().type()==typeid(vector<int32_t>))
|
---|
655 | return VecAsStr<int32_t>(v);
|
---|
656 |
|
---|
657 | if (v.value().type()==typeid(vector<int64_t>))
|
---|
658 | return VecAsStr<int64_t>(v);
|
---|
659 |
|
---|
660 | if (v.value().type()==typeid(vector<uint16_t>))
|
---|
661 | return VecAsStr<uint16_t>(v);
|
---|
662 |
|
---|
663 | if (v.value().type()==typeid(vector<uint32_t>))
|
---|
664 | return VecAsStr<uint32_t>(v);
|
---|
665 |
|
---|
666 | if (v.value().type()==typeid(vector<uint64_t>))
|
---|
667 | return VecAsStr<uint64_t>(v);
|
---|
668 |
|
---|
669 | if (v.value().type()==typeid(vector<float>))
|
---|
670 | return VecAsStr<float>(v);
|
---|
671 |
|
---|
672 | if (v.value().type()==typeid(vector<double>))
|
---|
673 | return VecAsStr<double>(v);
|
---|
674 |
|
---|
675 | ostringstream str;
|
---|
676 | str << hex << setfill('0') << "0x";
|
---|
677 | if (v.value().type()==typeid(Hex<uint16_t>))
|
---|
678 | str << setw(4) << v.as<Hex<uint16_t>>();
|
---|
679 |
|
---|
680 | if (v.value().type()==typeid(Hex<uint32_t>))
|
---|
681 | str << setw(8) << v.as<Hex<uint32_t>>();
|
---|
682 |
|
---|
683 | if (v.value().type()==typeid(Hex<uint64_t>))
|
---|
684 | str << setw(16) << v.as<Hex<uint64_t>>();
|
---|
685 |
|
---|
686 | return str.str();
|
---|
687 | }
|
---|
688 |
|
---|
689 | // --------------------------------------------------------------------------
|
---|
690 | //
|
---|
691 | //!
|
---|
692 | //
|
---|
693 | void Configuration::PrintOptions() const
|
---|
694 | {
|
---|
695 | cout << "Options propagated to program:" << endl;
|
---|
696 |
|
---|
697 | int maxlen = 0;
|
---|
698 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
699 | m!=fVariables.end(); m++)
|
---|
700 | Max(maxlen, m->first.length());
|
---|
701 |
|
---|
702 | cout.setf(ios_base::left);
|
---|
703 |
|
---|
704 | // =============> Implement prining of options in use
|
---|
705 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
706 | m!=fVariables.end(); m++)
|
---|
707 | {
|
---|
708 | const po::variable_value &v = m->second;
|
---|
709 |
|
---|
710 | ostringstream str;
|
---|
711 |
|
---|
712 | if (v.value().type()==typeid(bool))
|
---|
713 | str << " bool";
|
---|
714 | if (v.value().type()==typeid(string))
|
---|
715 | str << " string";
|
---|
716 | if (v.value().type()==typeid(int16_t))
|
---|
717 | str << " int16_t";
|
---|
718 | if (v.value().type()==typeid(int32_t))
|
---|
719 | str << " int32_t";
|
---|
720 | if (v.value().type()==typeid(int64_t))
|
---|
721 | str << " int64_t";
|
---|
722 | if (v.value().type()==typeid(uint16_t))
|
---|
723 | str << " uint16_t";
|
---|
724 | if (v.value().type()==typeid(uint32_t))
|
---|
725 | str << " uint32_t";
|
---|
726 | if (v.value().type()==typeid(uint64_t))
|
---|
727 | str << " uint64_t";
|
---|
728 | if (v.value().type()==typeid(float))
|
---|
729 | str << " float";
|
---|
730 | if (v.value().type()==typeid(double))
|
---|
731 | str << " double";
|
---|
732 | if (v.value().type()==typeid(Hex<uint16_t>))
|
---|
733 | str << " Hex<uint16_t>";
|
---|
734 | if (v.value().type()==typeid(Hex<uint32_t>))
|
---|
735 | str << " Hex<uint32_t>";
|
---|
736 | if (v.value().type()==typeid(Hex<uint64_t>))
|
---|
737 | str << " Hex<uint64_t>";
|
---|
738 | if (v.value().type()==typeid(vector<string>))
|
---|
739 | str << " vector<string>";
|
---|
740 | if (v.value().type()==typeid(vector<int16_t>))
|
---|
741 | str << " vector<int16_t>";
|
---|
742 | if (v.value().type()==typeid(vector<int32_t>))
|
---|
743 | str << " vector<int32_t>";
|
---|
744 | if (v.value().type()==typeid(vector<int64_t>))
|
---|
745 | str << " vector<int64_t>";
|
---|
746 | if (v.value().type()==typeid(vector<uint16_t>))
|
---|
747 | str << " vector<uint16_t>";
|
---|
748 | if (v.value().type()==typeid(vector<uint32_t>))
|
---|
749 | str << " vector<uint32_t>";
|
---|
750 | if (v.value().type()==typeid(vector<uint64_t>))
|
---|
751 | str << " vector<uint64_t>";
|
---|
752 | if (v.value().type()==typeid(vector<float>))
|
---|
753 | str << " vector<float>";
|
---|
754 | if (v.value().type()==typeid(vector<double>))
|
---|
755 | str << " vector<double>";
|
---|
756 |
|
---|
757 | if (str.str().empty())
|
---|
758 | str << " unknown[" << v.value().type().name() << "]";
|
---|
759 |
|
---|
760 | const string var = VarAsStr(v);
|
---|
761 | cout << setw(maxlen) << m->first;
|
---|
762 | if (!var.empty())
|
---|
763 | cout << " = ";
|
---|
764 | cout << var << " #" << str.str();
|
---|
765 |
|
---|
766 | if (v.defaulted())
|
---|
767 | cout << " [default]";
|
---|
768 | if (v.empty())
|
---|
769 | cout << " [empty]";
|
---|
770 |
|
---|
771 | cout << endl;
|
---|
772 | }
|
---|
773 |
|
---|
774 | cout << endl;
|
---|
775 | }
|
---|
776 |
|
---|
777 | // --------------------------------------------------------------------------
|
---|
778 | //
|
---|
779 | //!
|
---|
780 | //
|
---|
781 | void Configuration::PrintUnknown(const vector<string> &vec, int steps) const
|
---|
782 | {
|
---|
783 | for (vector<string>::const_iterator v=vec.begin(); v<vec.end(); v+=steps)
|
---|
784 | cout << " " << *v << endl;
|
---|
785 | cout << endl;
|
---|
786 | }
|
---|
787 |
|
---|
788 | multimap<string, string> Configuration::GetOptions() const
|
---|
789 | {
|
---|
790 | multimap<string,string> rc;
|
---|
791 |
|
---|
792 | for (map<string,po::variable_value>::const_iterator m=fVariables.begin();
|
---|
793 | m!=fVariables.end(); m++)
|
---|
794 | rc.insert(make_pair(m->first, VarAsStr(m->second)));
|
---|
795 |
|
---|
796 | return rc;
|
---|
797 | }
|
---|
798 |
|
---|
799 | // --------------------------------------------------------------------------
|
---|
800 | //
|
---|
801 | //!
|
---|
802 | //
|
---|
803 | void Configuration::PrintUnknown() const
|
---|
804 | {
|
---|
805 | if (fUnknownCommandline.size())
|
---|
806 | {
|
---|
807 | cout << "Unknown commandline options:" << endl;
|
---|
808 | PrintUnknown(fUnknownCommandline);
|
---|
809 | }
|
---|
810 |
|
---|
811 | if (fUnknownConfigfile.size())
|
---|
812 | {
|
---|
813 | cout << "Unknown options in configfile:" << endl;
|
---|
814 | PrintUnknown(fUnknownConfigfile, 2);
|
---|
815 | }
|
---|
816 |
|
---|
817 | if (fUnknownEnvironment.size())
|
---|
818 | {
|
---|
819 | cout << "Unknown environment variables:" << endl;
|
---|
820 | PrintUnknown(fUnknownEnvironment);
|
---|
821 | }
|
---|
822 |
|
---|
823 | if (fUnknownDatabase.size())
|
---|
824 | {
|
---|
825 | cout << "Unknown database entry:" << endl;
|
---|
826 | PrintUnknown(fUnknownDatabase);
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | // --------------------------------------------------------------------------
|
---|
831 | //
|
---|
832 | //!
|
---|
833 | //
|
---|
834 | void Configuration::AddOptionsCommandline(const po::options_description &cl, bool visible)
|
---|
835 | {
|
---|
836 | fOptionsCommandline[visible].add(cl);
|
---|
837 | }
|
---|
838 |
|
---|
839 | // --------------------------------------------------------------------------
|
---|
840 | //
|
---|
841 | //!
|
---|
842 | //
|
---|
843 | void Configuration::AddOptionsConfigfile(const po::options_description &cf, bool visible)
|
---|
844 | {
|
---|
845 | fOptionsConfigfile[visible].add(cf);
|
---|
846 | }
|
---|
847 |
|
---|
848 | // --------------------------------------------------------------------------
|
---|
849 | //
|
---|
850 | //!
|
---|
851 | //
|
---|
852 | void Configuration::AddOptionsEnvironment(const po::options_description &env, bool visible)
|
---|
853 | {
|
---|
854 | fOptionsEnvironment[visible].add(env);
|
---|
855 | }
|
---|
856 |
|
---|
857 | // --------------------------------------------------------------------------
|
---|
858 | //
|
---|
859 | //!
|
---|
860 | //
|
---|
861 | void Configuration::AddOptionsDatabase(const po::options_description &db, bool visible)
|
---|
862 | {
|
---|
863 | fOptionsDatabase[visible].add(db);
|
---|
864 | }
|
---|
865 |
|
---|
866 | // --------------------------------------------------------------------------
|
---|
867 | //
|
---|
868 | //!
|
---|
869 | //
|
---|
870 | void Configuration::SetArgumentPositions(const po::positional_options_description &desc)
|
---|
871 | {
|
---|
872 | fArgumentPositions = desc;
|
---|
873 | }
|
---|
874 |
|
---|
875 | // --------------------------------------------------------------------------
|
---|
876 | //
|
---|
877 | //!
|
---|
878 | //
|
---|
879 | void Configuration::SetNameMapper(const boost::function<string(string)> &func)
|
---|
880 | {
|
---|
881 | fNameMapper = func;
|
---|
882 | }
|
---|
883 |
|
---|
884 | void Configuration::SetNameMapper()
|
---|
885 | {
|
---|
886 | fNameMapper = bind1st(mem_fun(&Configuration::DefaultMapper), this);
|
---|
887 | }
|
---|
888 |
|
---|
889 | void Configuration::SetPrintUsage(const boost::function<void(void)> &func)
|
---|
890 | {
|
---|
891 | fPrintUsage = func;
|
---|
892 | }
|
---|
893 |
|
---|
894 | void Configuration::SetPrintUsage()
|
---|
895 | {
|
---|
896 | fPrintUsage = boost::bind(&Configuration::PrintUsage, this);
|
---|
897 | }
|
---|
898 |
|
---|
899 | void Configuration::SetPrintVersion(const boost::function<void(const string&)> &func)
|
---|
900 | {
|
---|
901 | fPrintVersion = func;
|
---|
902 | }
|
---|
903 |
|
---|
904 | void Configuration::SetPrintVersion()
|
---|
905 | {
|
---|
906 | fPrintVersion = boost::function<void(const string&)>();
|
---|
907 | }
|
---|
908 |
|
---|
909 | // --------------------------------------------------------------------------
|
---|
910 | //
|
---|
911 | //!
|
---|
912 | //! The idea of the Parse() memeber-function is to parse the command-line,
|
---|
913 | //! the configuration files, the databse and the environment and return
|
---|
914 | //! a proper combined result.
|
---|
915 | //!
|
---|
916 | //! In details the following actions are performed in the given order:
|
---|
917 | //!
|
---|
918 | //! - (0) Init local variables with the list of options described by the
|
---|
919 | //! data members.
|
---|
920 | //! - (1) Reset the data members fPriorityFile, fDefaultFile, fDatabase
|
---|
921 | //! - (2) Parse the command line
|
---|
922 | //! - (3) Check for \b --help* command-line options and performe
|
---|
923 | //! corresponding action
|
---|
924 | //! - (4) Check for \b --print and \b --print-all and perform corresponding
|
---|
925 | //! action
|
---|
926 | //! - (5) Read and parse the global configuration file, which is compiled
|
---|
927 | //! from the path corresponding to the argument given in the
|
---|
928 | //! constructor + "/fact++.rc", unrecognized options are always
|
---|
929 | //! allowed.
|
---|
930 | //! - (6) Read and parse the default configuration file, which is either
|
---|
931 | //! given by the default name or the \b --default command-line
|
---|
932 | //! option. The default name is compiled from the argument
|
---|
933 | //! given to the constructor and ".rc". If the file-name is
|
---|
934 | //! identical to the default (no command-line option given)
|
---|
935 | //! a missing configuration file is no error. Depending on
|
---|
936 | //! the \b --dont-check and \b --dont-check-files options,
|
---|
937 | //! unrecognized options in the file throw an exception or not.
|
---|
938 | //! - (7) Check for \b --print-default and \b --print-all and perform
|
---|
939 | //! corresponding action
|
---|
940 | //! - (8) Read and parse the priority configuration file, which must be given
|
---|
941 | //! by the \b --config or \b -C command-line option or a
|
---|
942 | //! corresponding entry in the default-configuration file.
|
---|
943 | //! If an option on the command-line and the in the configuration
|
---|
944 | //! file exists, the command-line option has priority.
|
---|
945 | //! If none is given, no priority file is read. Depending on
|
---|
946 | //! the \b --dont-check and \b --dont-check-files options,
|
---|
947 | //! unrecognized options in the file throw an exception or not.
|
---|
948 | //! - (9) Check for \b --print-config and \b --print-all and perform
|
---|
949 | //! corresponding action
|
---|
950 | //! - (10) Retrieve options from the database according to the
|
---|
951 | //! options \b --database and \b --no-database. Note that
|
---|
952 | //! options given on the command-line have highest priority.
|
---|
953 | //! The second priority is the priority-configuration file.
|
---|
954 | //! The options from the default configuration-file have
|
---|
955 | //! lowest priority.
|
---|
956 | //! - (11) Check for \b --print-database and \b --print-all and perform
|
---|
957 | //! corresponding action
|
---|
958 | //! - (12) Parse the environment options.
|
---|
959 | //! - (13) Check for \b --print-environment and \b --print-all and perform
|
---|
960 | //! corresponding action
|
---|
961 | //! - (14) Compile the final result. The priority of the options is (in
|
---|
962 | //! decreasing order): command-line options, options from the
|
---|
963 | //! priority configuration file, options from the database,
|
---|
964 | //! options from the default configuration-file and options
|
---|
965 | //! from the environment.
|
---|
966 | //! - (15) Finally all options which were found and flagged as unrecognized,
|
---|
967 | //! because they are not in the user-defined list of described
|
---|
968 | //! options, are collected and stored in the corresponding
|
---|
969 | //! data-members.
|
---|
970 | //! - (16) Before the function returns it check for \b --print-options
|
---|
971 | //! and \b --print-unknown and performs the corresponding actions.
|
---|
972 | //!
|
---|
973 | //!
|
---|
974 | //! @param argc,argv
|
---|
975 | //! arguments passed to <B>main(int argc, char **argv)</B>
|
---|
976 | //!
|
---|
977 | //! @returns
|
---|
978 | //! A reference to the list with the resulting options with their
|
---|
979 | //! values.
|
---|
980 | //!
|
---|
981 | //! @todo
|
---|
982 | //! - describe the exceptions
|
---|
983 | //! - describe what happens in a more general way
|
---|
984 | //! - print a waring when no default coonfig file is read
|
---|
985 | //! - proper handling and error messages if files not available
|
---|
986 | //
|
---|
987 | const po::variables_map &Configuration::Parse(int argc, const char **argv)
|
---|
988 | {
|
---|
989 | const po::positional_options_description &opt_positional = fArgumentPositions;
|
---|
990 |
|
---|
991 | // ------------------------ (0) --------------------------
|
---|
992 |
|
---|
993 | po::options_description opt_commandline;
|
---|
994 | po::options_description opt_configfile;
|
---|
995 | po::options_description opt_environment;
|
---|
996 | po::options_description opt_database;
|
---|
997 |
|
---|
998 | for (int i=0; i<2; i++)
|
---|
999 | {
|
---|
1000 | opt_commandline.add(fOptionsCommandline[i]);
|
---|
1001 | opt_configfile.add(fOptionsConfigfile[i]);
|
---|
1002 | opt_environment.add(fOptionsEnvironment[i]);
|
---|
1003 | opt_database.add(fOptionsDatabase[i]);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | // ------------------------ (1) --------------------------
|
---|
1007 |
|
---|
1008 | fPriorityFile = "";
|
---|
1009 | fDefaultFile = "";
|
---|
1010 | fDatabase = "";
|
---|
1011 |
|
---|
1012 | // ------------------------ (2) --------------------------
|
---|
1013 |
|
---|
1014 | po::command_line_parser parser(argc, const_cast<char**>(argv));
|
---|
1015 | parser.options(opt_commandline);
|
---|
1016 | parser.positional(opt_positional);
|
---|
1017 | parser.style(style::unix_style&~style::allow_guessing);
|
---|
1018 | //parser.allow_unregistered();
|
---|
1019 |
|
---|
1020 | const po::parsed_options parsed_commandline = parser.run();
|
---|
1021 |
|
---|
1022 | // ------------------------ (3) --------------------------
|
---|
1023 |
|
---|
1024 | po::variables_map getfiles;
|
---|
1025 | po::store(parsed_commandline, getfiles);
|
---|
1026 |
|
---|
1027 | if (getfiles.count("version"))
|
---|
1028 | PrintVersion();
|
---|
1029 | if (getfiles.count("help"))
|
---|
1030 | {
|
---|
1031 | fPrintUsage();
|
---|
1032 | cout <<
|
---|
1033 | "Options:\n"
|
---|
1034 | "The following describes the available commandline options. "
|
---|
1035 | "For further details on how command line option are parsed "
|
---|
1036 | "and in which order which configuration sources are accessed "
|
---|
1037 | "please refer to the class reference of the Configuration class." << endl;
|
---|
1038 | cout << fOptionsCommandline[kVisible] << endl;
|
---|
1039 | }
|
---|
1040 | if (getfiles.count("help-config"))
|
---|
1041 | cout << fOptionsConfigfile[kVisible] << endl;
|
---|
1042 | if (getfiles.count("help-env"))
|
---|
1043 | cout << fOptionsEnvironment[kVisible] << endl;
|
---|
1044 | if (getfiles.count("help-database"))
|
---|
1045 | cout << fOptionsDatabase[kVisible] << endl;
|
---|
1046 |
|
---|
1047 | // ------------------------ (4) --------------------------
|
---|
1048 |
|
---|
1049 | if (getfiles.count("print") || getfiles.count("print-all"))
|
---|
1050 | {
|
---|
1051 | cout << endl << "Parsed commandline options:" << endl;
|
---|
1052 | PrintParsed(parsed_commandline);
|
---|
1053 | cout << endl;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | // ------------------------ (5) --------------------------
|
---|
1057 |
|
---|
1058 | const boost::filesystem::path path(GetName());
|
---|
1059 | const string globalfile = path.parent_path().string()+"/fact++.rc";
|
---|
1060 |
|
---|
1061 | cerr << "Reading options from '" << globalfile << "'." << endl;
|
---|
1062 |
|
---|
1063 | ifstream gfile(globalfile.c_str());
|
---|
1064 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1065 | const po::parsed_options parsed_globalfile =
|
---|
1066 | !gfile ?
|
---|
1067 | po::parsed_options(&opt_configfile) :
|
---|
1068 | po::parse_config_file<char>(gfile, opt_configfile, false);
|
---|
1069 |
|
---|
1070 | // ------------------------ (6) --------------------------
|
---|
1071 |
|
---|
1072 | // Get default file from command line
|
---|
1073 | if (getfiles.count("default"))
|
---|
1074 | {
|
---|
1075 | fDefaultFile = getfiles["default"].as<string>();
|
---|
1076 | cerr << "Reading options from '" << fDefaultFile << "'." << endl;
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | const bool checkf = !getfiles.count("dont-check-files") && !getfiles.count("dont-check");
|
---|
1080 | const bool defaulted = getfiles.count("default") && getfiles["default"].defaulted();
|
---|
1081 | //const bool exists = boost::filesystem::exists(fDefaultFile);
|
---|
1082 |
|
---|
1083 | ifstream indef(fDefaultFile.c_str());
|
---|
1084 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1085 | const po::parsed_options parsed_defaultfile =
|
---|
1086 | !indef && defaulted ?
|
---|
1087 | po::parsed_options(&opt_configfile) :
|
---|
1088 | po::parse_config_file<char>(indef, opt_configfile, !checkf);
|
---|
1089 |
|
---|
1090 | // ------------------------ (7) --------------------------
|
---|
1091 |
|
---|
1092 | if (getfiles.count("print-default") || getfiles.count("print-all"))
|
---|
1093 | {
|
---|
1094 | if (!indef.is_open() && defaulted)
|
---|
1095 | cout << "No configuration file by --default option specified." << endl;
|
---|
1096 | else
|
---|
1097 | {
|
---|
1098 | cout << endl << "Parsed options from '" << fDefaultFile << "':" << endl;
|
---|
1099 | PrintParsed(parsed_defaultfile);
|
---|
1100 | cout << endl;
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | po::store(parsed_defaultfile, getfiles);
|
---|
1105 |
|
---|
1106 | // ------------------------ (8) --------------------------
|
---|
1107 |
|
---|
1108 | // Get priority from commandline(1), defaultfile(2)
|
---|
1109 | if (getfiles.count("config"))
|
---|
1110 | {
|
---|
1111 | fPriorityFile = getfiles["config"].as<string>();
|
---|
1112 | cerr << "Reading options from '" << fPriorityFile << "'." << endl;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | ifstream inpri(fPriorityFile.c_str());
|
---|
1116 | // ===> FIXME: Proper handling of missing file or wrong file name
|
---|
1117 | const po::parsed_options parsed_priorityfile =
|
---|
1118 | fPriorityFile.empty() ? po::parsed_options(&opt_configfile) :
|
---|
1119 | po::parse_config_file<char>(inpri, opt_configfile, !checkf);
|
---|
1120 |
|
---|
1121 | // ------------------------ (9) --------------------------
|
---|
1122 |
|
---|
1123 | if (getfiles.count("print-config") || getfiles.count("print-all"))
|
---|
1124 | {
|
---|
1125 | if (fPriorityFile.empty())
|
---|
1126 | cout << "No configuration file by --config option specified." << endl;
|
---|
1127 | else
|
---|
1128 | {
|
---|
1129 | cout << endl << "Parsed options from '" << fPriorityFile << "':" << endl;
|
---|
1130 | PrintParsed(parsed_priorityfile);
|
---|
1131 | cout << endl;
|
---|
1132 | }
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | // ------------------------ (10) -------------------------
|
---|
1136 |
|
---|
1137 | po::variables_map getdatabase;
|
---|
1138 | po::store(parsed_commandline, getdatabase);
|
---|
1139 | po::store(parsed_priorityfile, getdatabase);
|
---|
1140 | po::store(parsed_defaultfile, getdatabase);
|
---|
1141 | po::store(parsed_globalfile, getdatabase);
|
---|
1142 |
|
---|
1143 | if (getdatabase.count("database") && !getdatabase.count("no-database"))
|
---|
1144 | {
|
---|
1145 | fDatabase = getdatabase["database"].as<string>();
|
---|
1146 | cerr << "Requesting options from '" << fDatabase << "'." << endl;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | const bool checkdb = !getdatabase.count("dont-check-database") && !getdatabase.count("dont-check");
|
---|
1150 |
|
---|
1151 | const po::parsed_options parsed_database =
|
---|
1152 | fDatabase.empty() ? po::parsed_options(&opt_database) :
|
---|
1153 | parse_database(fDatabase, opt_database, !checkdb);
|
---|
1154 |
|
---|
1155 | // ------------------------ (11) -------------------------
|
---|
1156 |
|
---|
1157 | if (getfiles.count("print-database") || getfiles.count("print-all"))
|
---|
1158 | {
|
---|
1159 | if (fDatabase.empty())
|
---|
1160 | cout << "No database access requested." << endl;
|
---|
1161 | else
|
---|
1162 | {
|
---|
1163 | cout << endl << "Options received from '" << fDatabase << "':" << endl;
|
---|
1164 | PrintParsed(parsed_database);
|
---|
1165 | cout << endl;
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | // ------------------------ (12) -------------------------
|
---|
1170 |
|
---|
1171 | const po::parsed_options parsed_environment = po::parse_environment(opt_environment, fNameMapper);
|
---|
1172 |
|
---|
1173 | // ------------------------ (13) -------------------------
|
---|
1174 |
|
---|
1175 | if (getfiles.count("print-environment"))
|
---|
1176 | {
|
---|
1177 | cout << "Parsed options from environment:" << endl;
|
---|
1178 | PrintParsed(parsed_environment);
|
---|
1179 | cout << endl;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | // ------------------------ (14) -------------------------
|
---|
1183 | po::variables_map result;
|
---|
1184 | po::store(parsed_commandline, result);
|
---|
1185 | po::store(parsed_priorityfile, result);
|
---|
1186 | po::store(parsed_database, result);
|
---|
1187 | po::store(parsed_defaultfile, result);
|
---|
1188 | po::store(parsed_globalfile, result);
|
---|
1189 | po::store(parsed_environment, result);
|
---|
1190 | po::notify(result);
|
---|
1191 |
|
---|
1192 | fVariables = result;
|
---|
1193 |
|
---|
1194 | // ------------------------ (15) -------------------------
|
---|
1195 |
|
---|
1196 | const vector<string> unknown0 = collect_unrecognized(parsed_globalfile.options, po::exclude_positional);
|
---|
1197 | const vector<string> unknown1 = collect_unrecognized(parsed_defaultfile.options, po::exclude_positional);
|
---|
1198 | const vector<string> unknown2 = collect_unrecognized(parsed_priorityfile.options, po::exclude_positional);
|
---|
1199 |
|
---|
1200 | fUnknownConfigfile.clear();
|
---|
1201 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown0.begin(), unknown0.end());
|
---|
1202 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown1.begin(), unknown1.end());
|
---|
1203 | fUnknownConfigfile.insert(fUnknownConfigfile.end(), unknown2.begin(), unknown2.end());
|
---|
1204 |
|
---|
1205 | fUnknownCommandline = collect_unrecognized(parsed_commandline.options, po::exclude_positional);
|
---|
1206 | fUnknownEnvironment = collect_unrecognized(parsed_environment.options, po::exclude_positional);
|
---|
1207 | fUnknownDatabase = collect_unrecognized(parsed_database.options, po::exclude_positional);
|
---|
1208 |
|
---|
1209 | // ------------------------ (16) -------------------------
|
---|
1210 |
|
---|
1211 | if (result.count("print-options"))
|
---|
1212 | PrintOptions();
|
---|
1213 |
|
---|
1214 | if (result.count("print-unknown"))
|
---|
1215 | PrintUnknown();
|
---|
1216 |
|
---|
1217 | return fVariables;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | // --------------------------------------------------------------------------
|
---|
1221 | //
|
---|
1222 | //! Removes /.libs/lt- from a path or just lt- from the filename.
|
---|
1223 | //!
|
---|
1224 | //! @param src
|
---|
1225 | //! input path with filename
|
---|
1226 | //! @returns
|
---|
1227 | //! path cleaned from libtool extensions
|
---|
1228 | //!
|
---|
1229 | string Configuration::UnLibToolize(const string &src) const
|
---|
1230 | {
|
---|
1231 | const boost::filesystem::path path(src);
|
---|
1232 |
|
---|
1233 | string pname = path.parent_path().string();
|
---|
1234 | string fname = path.filename();
|
---|
1235 |
|
---|
1236 | // If the filename starts with "lt-" remove it from the name
|
---|
1237 | if (fname.substr(0, 3)=="lt-")
|
---|
1238 | fname = fname.substr(3);
|
---|
1239 |
|
---|
1240 | // If no directory is contained determine the current directory
|
---|
1241 | if (pname.empty())
|
---|
1242 | pname = boost::filesystem::current_path().string();
|
---|
1243 |
|
---|
1244 | // If the directory is relative and just ".libs" forget about it
|
---|
1245 | if (pname==".libs")
|
---|
1246 | return fname;
|
---|
1247 |
|
---|
1248 | // Check if the directory is long enough to contain "/.libs"
|
---|
1249 | if (pname.length()>=6)
|
---|
1250 | {
|
---|
1251 | // If the directory ends with "/.libs", remove it
|
---|
1252 | const size_t pos = pname.length()-6;
|
---|
1253 | if (pname.substr(pos)=="/.libs")
|
---|
1254 | pname = pname.substr(0, pos);
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | return pname+'/'+fname;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | // --------------------------------------------------------------------------
|
---|
1261 | //
|
---|
1262 | //! Print version information about the program and package.
|
---|
1263 | //!
|
---|
1264 | //! The program name is taken from fName. If a leading "lt-" is found,
|
---|
1265 | //! it is removed. This is useful if the program was build and run
|
---|
1266 | //! using libtool.
|
---|
1267 | //!
|
---|
1268 | //! The package name is taken from the define PACKAGE_STRING. If it is
|
---|
1269 | //! not defined (like automatically done by autoconf) no package information
|
---|
1270 | //! is printed. The same is true for PACKAGE_URL and PACKAGE_BUGREPORT.
|
---|
1271 | //!
|
---|
1272 | //! From help2man:
|
---|
1273 | //!
|
---|
1274 | //! The first line of the --version information is assumed to be in one
|
---|
1275 | //! of the following formats:
|
---|
1276 | //!
|
---|
1277 | //! \verbatim
|
---|
1278 | //! - <version>
|
---|
1279 | //! - <program> <version>
|
---|
1280 | //! - {GNU,Free} <program> <version>
|
---|
1281 | //! - <program> ({GNU,Free} <package>) <version>
|
---|
1282 | //! - <program> - {GNU,Free} <package> <version>
|
---|
1283 | //! \endverbatim
|
---|
1284 | //!
|
---|
1285 | //! and separated from any copyright/author details by a blank line.
|
---|
1286 | //!
|
---|
1287 | //! Handle multi-line bug reporting sections of the form:
|
---|
1288 | //!
|
---|
1289 | //! \verbatim
|
---|
1290 | //! - Report <program> bugs to <addr>
|
---|
1291 | //! - GNU <package> home page: <url>
|
---|
1292 | //! - ...
|
---|
1293 | //! \endverbatim
|
---|
1294 | //!
|
---|
1295 | //! @param name
|
---|
1296 | //! name of the program (usually argv[0]).
|
---|
1297 | //!
|
---|
1298 | void Configuration::PrintVersion() const
|
---|
1299 | {
|
---|
1300 | #ifndef PACKAGE_STRING
|
---|
1301 | #define PACKAGE_STRING ""
|
---|
1302 | #endif
|
---|
1303 |
|
---|
1304 | #ifndef PACKAGE_URL
|
---|
1305 | #define PACKAGE_URL ""
|
---|
1306 | #endif
|
---|
1307 |
|
---|
1308 | #ifndef PACKAGE_BUGREPORT
|
---|
1309 | #define PACKAGE_BUGREPORT ""
|
---|
1310 | #endif
|
---|
1311 |
|
---|
1312 | if (!fPrintVersion.empty())
|
---|
1313 | {
|
---|
1314 | fPrintVersion(fName);
|
---|
1315 | return;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | const std::string n = boost::filesystem::path(fName).filename();
|
---|
1319 |
|
---|
1320 | const string name = PACKAGE_STRING;
|
---|
1321 | const string bugs = PACKAGE_BUGREPORT;
|
---|
1322 | const string url = PACKAGE_URL;
|
---|
1323 |
|
---|
1324 | cout << n;
|
---|
1325 | if (!name.empty())
|
---|
1326 | cout << " - " << name;
|
---|
1327 | cout <<
|
---|
1328 | "\n\n"
|
---|
1329 | "Written by Thomas Bretz et al.\n"
|
---|
1330 | "\n";
|
---|
1331 | if (!bugs.empty())
|
---|
1332 | cout << "Report bugs to <" << bugs << ">\n";
|
---|
1333 | if (!url.empty())
|
---|
1334 | cout << "Home page: " << url << "\n";
|
---|
1335 | cout <<
|
---|
1336 | "\n"
|
---|
1337 | "Copyright (C) 2011 by the FACT Collaboration.\n"
|
---|
1338 | "This is free software; see the source for copying conditions.\n"
|
---|
1339 | << std::endl;
|
---|
1340 | }
|
---|