1 | #ifndef FACT_Configuration
|
---|
2 | #define FACT_Configuration
|
---|
3 |
|
---|
4 | #include <boost/program_options.hpp>
|
---|
5 |
|
---|
6 | namespace po = boost::program_options;
|
---|
7 |
|
---|
8 | class Configuration
|
---|
9 | {
|
---|
10 | private:
|
---|
11 | /// Convienience enum to access the fOption* data memebers more verbosely.
|
---|
12 | enum
|
---|
13 | {
|
---|
14 | kHidden = 0, ///< Index for hidden options (not shown in PrintParsed)
|
---|
15 | kVisible = 1 ///< Index for options visible in PrintParsed
|
---|
16 | };
|
---|
17 |
|
---|
18 | const std::string fName; /// argv[0]
|
---|
19 |
|
---|
20 | std::map<std::string, std::string> fEnvMap;
|
---|
21 |
|
---|
22 | po::options_description fOptionsCommandline[2]; /// Description of the command-line options
|
---|
23 | po::options_description fOptionsConfigfile[2]; /// Description of the options in the configuration file
|
---|
24 | po::options_description fOptionsDatabase[2]; /// Description of options from the database
|
---|
25 | po::options_description fOptionsEnvironment[2]; /// Description of options from the environment
|
---|
26 |
|
---|
27 | po::positional_options_description fArgumentPositions; /// Description of positional command-line options (arguments)
|
---|
28 |
|
---|
29 | std::vector<std::string> fUnknownCommandline; /// Storage container for unrecognized commandline options
|
---|
30 | std::vector<std::string> fUnknownConfigfile; /// Storage container for unrecognized options from configuration files
|
---|
31 | std::vector<std::string> fUnknownEnvironment; /// Storage container for unrecognized options from the environment
|
---|
32 | std::vector<std::string> fUnknownDatabase; /// Storage container for unrecognized options retrieved from the database
|
---|
33 |
|
---|
34 | std::string fPriorityFile; /// File name of the priority configuration file (overwrites option from the databse)
|
---|
35 | std::string fDefaultFile; /// File name of the default configuration file (usually {program}.rc)
|
---|
36 | std::string fDatabase; /// URL for database connection (see Configuration::parse_database)
|
---|
37 |
|
---|
38 | po::variables_map fVariables; /// Variables as compiled by the Parse-function, which will be passed to the program
|
---|
39 |
|
---|
40 | /// A default mapper for environment variables skipping all of them
|
---|
41 | std::string DefaultMapper(const std::string env)
|
---|
42 | {
|
---|
43 | return fEnvMap[env];
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// Pointer to the mapper function for environment variables
|
---|
47 | boost::function1<std::string, std::string> fNameMapper;
|
---|
48 |
|
---|
49 | /// Helper function which return the max of the two arguments in the first argument
|
---|
50 | static void Max(int &val, const int &comp)
|
---|
51 | {
|
---|
52 | if (comp>val)
|
---|
53 | val=comp;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// Print all options from a list of already parsed options
|
---|
57 | void PrintParsed(const po::parsed_options &parsed) const;
|
---|
58 | /// Print a list of all unkown options within the given vector
|
---|
59 | void PrintUnknown(std::vector<std::string> &vec, int steps=1);
|
---|
60 |
|
---|
61 | public:
|
---|
62 | Configuration(const std::string &prgname="");
|
---|
63 |
|
---|
64 | /// Retrieve data from a database and return them as options
|
---|
65 | static po::basic_parsed_options<char>
|
---|
66 | parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
|
---|
67 |
|
---|
68 | // Setup
|
---|
69 | void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
|
---|
70 | void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
|
---|
71 | void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
|
---|
72 | void AddOptionsDatabase(const po::options_description &db, bool visible=true);
|
---|
73 | void AddOptions(const po::options_description &opt, bool visible=true)
|
---|
74 | {
|
---|
75 | AddOptionsCommandline(opt, visible);
|
---|
76 | AddOptionsConfigfile(opt, visible);
|
---|
77 | AddOptionsEnvironment(opt, visible);
|
---|
78 | AddOptionsDatabase(opt, visible);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void SetArgumentPositions(const po::positional_options_description &desc);
|
---|
82 |
|
---|
83 | void SetNameMapper(const boost::function1<std::string, std::string> &func);
|
---|
84 | void SetNameMapper();
|
---|
85 |
|
---|
86 | void AddEnv(const std::string &conf, const std::string &env)
|
---|
87 | {
|
---|
88 | fEnvMap[env] = conf;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Output
|
---|
92 | void PrintOptions();
|
---|
93 | void PrintUnknown();
|
---|
94 |
|
---|
95 | // Process command line arguments
|
---|
96 | const po::variables_map &Parse(int argc, char **argv);
|
---|
97 |
|
---|
98 | bool HasHelp() const
|
---|
99 | {
|
---|
100 | return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
|
---|
101 | }
|
---|
102 |
|
---|
103 | bool HasPrint() const
|
---|
104 | {
|
---|
105 | return Has("print-all") || Has("print") || Has("print-default") ||
|
---|
106 | Has("print-database") || Has("print-config") ||
|
---|
107 | Has("print-environment") || Has("print-unkown") ||
|
---|
108 | Has("print-options");
|
---|
109 | }
|
---|
110 |
|
---|
111 | // Simplified access to the parsed options
|
---|
112 | template<class T>
|
---|
113 | T Get(const char *var) const { return Has(var) ? fVariables[var].as<T>() : T(); }
|
---|
114 | bool Has(const char *var) const { return fVariables.count(var)>0; }
|
---|
115 |
|
---|
116 | const std::string &GetName() const { return fName; }
|
---|
117 | };
|
---|
118 |
|
---|
119 | template<class T>
|
---|
120 | inline po::typed_value<T> *var(T *ptr=0)
|
---|
121 | { return po::value<T>(ptr); }
|
---|
122 |
|
---|
123 | template<class T>
|
---|
124 | inline po::typed_value<T> *var(const T &val, T *ptr=0)
|
---|
125 | { return po::value<T>(ptr)->default_value(val); }
|
---|
126 |
|
---|
127 | template<class T>
|
---|
128 | inline po::typed_value<std::vector<T>> *vars()
|
---|
129 | { return po::value<std::vector<T>>(); }
|
---|
130 |
|
---|
131 | inline po::typed_value<bool> *po_switch()
|
---|
132 | { return po::bool_switch(); }
|
---|
133 |
|
---|
134 | #endif
|
---|