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::function<std::string(std::string)> fNameMapper;
|
---|
48 | boost::function<void()> fPrintUsage;
|
---|
49 | boost::function<void(const std::string&)> fPrintVersion;
|
---|
50 |
|
---|
51 | /// Helper function which return the max of the two arguments in the first argument
|
---|
52 | static void Max(int &val, const int &comp)
|
---|
53 | {
|
---|
54 | if (comp>val)
|
---|
55 | val=comp;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// Print all options from a list of already parsed options
|
---|
59 | void PrintParsed(const po::parsed_options &parsed) const;
|
---|
60 | /// Print a list of all unkown options within the given vector
|
---|
61 | void PrintUnknown(std::vector<std::string> &vec, int steps=1);
|
---|
62 |
|
---|
63 | virtual void PrintUsage() const { }
|
---|
64 | virtual void PrintVersion() const;
|
---|
65 |
|
---|
66 | std::string UnLibToolize(const std::string &src) const;
|
---|
67 |
|
---|
68 | public:
|
---|
69 | Configuration(const std::string &prgname="");
|
---|
70 | virtual ~Configuration() { }
|
---|
71 |
|
---|
72 | /// Retrieve data from a database and return them as options
|
---|
73 | static po::basic_parsed_options<char>
|
---|
74 | parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
|
---|
75 |
|
---|
76 | // Setup
|
---|
77 | void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
|
---|
78 | void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
|
---|
79 | void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
|
---|
80 | void AddOptionsDatabase(const po::options_description &db, bool visible=true);
|
---|
81 | void AddOptions(const po::options_description &opt, bool visible=true)
|
---|
82 | {
|
---|
83 | AddOptionsCommandline(opt, visible);
|
---|
84 | AddOptionsConfigfile(opt, visible);
|
---|
85 | AddOptionsEnvironment(opt, visible);
|
---|
86 | AddOptionsDatabase(opt, visible);
|
---|
87 | }
|
---|
88 |
|
---|
89 | void SetArgumentPositions(const po::positional_options_description &desc);
|
---|
90 |
|
---|
91 | void SetNameMapper(const boost::function<std::string(std::string)> &func);
|
---|
92 | void SetNameMapper();
|
---|
93 |
|
---|
94 | void SetPrintUsage(const boost::function<void(void)> &func);
|
---|
95 | void SetPrintUsage();
|
---|
96 |
|
---|
97 | void SetPrintVersion(const boost::function<void(const std::string &)> &func);
|
---|
98 | void SetPrintVersion();
|
---|
99 |
|
---|
100 | void AddEnv(const std::string &conf, const std::string &env)
|
---|
101 | {
|
---|
102 | fEnvMap[env] = conf;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Output
|
---|
106 | void PrintOptions();
|
---|
107 | void PrintUnknown();
|
---|
108 |
|
---|
109 | // Process command line arguments
|
---|
110 | const po::variables_map &Parse(int argc, const char **argv);
|
---|
111 |
|
---|
112 | bool HasVersion() const
|
---|
113 | {
|
---|
114 | return Has("version");
|
---|
115 | }
|
---|
116 |
|
---|
117 | bool HasHelp() const
|
---|
118 | {
|
---|
119 | return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool HasPrint() const
|
---|
123 | {
|
---|
124 | return Has("print-all") || Has("print") || Has("print-default") ||
|
---|
125 | Has("print-database") || Has("print-config") ||
|
---|
126 | Has("print-environment") || Has("print-unkown") ||
|
---|
127 | Has("print-options");
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Simplified access to the parsed options
|
---|
131 | template<class T>
|
---|
132 | T Get(const char *var) const { return Has(var) ? fVariables[var].as<T>() : T(); }
|
---|
133 | bool Has(const char *var) const { return fVariables.count(var)>0; }
|
---|
134 |
|
---|
135 | const std::string &GetName() const { return fName; }
|
---|
136 | };
|
---|
137 |
|
---|
138 | template<class T>
|
---|
139 | inline po::typed_value<T> *var(T *ptr=0)
|
---|
140 | { return po::value<T>(ptr); }
|
---|
141 |
|
---|
142 | template<class T>
|
---|
143 | inline po::typed_value<T> *var(const T &val, T *ptr=0)
|
---|
144 | { return po::value<T>(ptr)->default_value(val); }
|
---|
145 |
|
---|
146 | template<class T>
|
---|
147 | inline po::typed_value<std::vector<T>> *vars()
|
---|
148 | { return po::value<std::vector<T>>(); }
|
---|
149 |
|
---|
150 | inline po::typed_value<bool> *po_switch()
|
---|
151 | { return po::bool_switch(); }
|
---|
152 |
|
---|
153 | inline po::typed_value<bool> *po_bool()
|
---|
154 | { return po::value<bool>()->implicit_value(true)->default_value(false); }
|
---|
155 |
|
---|
156 | #endif
|
---|