1 #ifndef FACT_Configuration 2 #define FACT_Configuration 5 #include <boost/program_options.hpp> 7 namespace po = boost::program_options;
21 std::map<std::string, std::string>
fEnvMap;
55 static void Max(
int &val,
const int &comp)
66 std::string
VecAsStr(
const po::variable_value &v)
const;
67 std::string
VarAsStr(
const po::variable_value &v)
const;
70 void PrintParsed(
const po::parsed_options &parsed)
const;
72 void PrintUnknown(
const std::vector<std::string> &vec,
int steps=1)
const;
84 static po::basic_parsed_options<char>
85 parse_database(
const std::string &prgname,
const std::string &database,
const po::options_description& desc,
bool allow_unregistered=
false);
92 void AddOptions(
const po::options_description &opt,
bool visible=
true)
102 void SetNameMapper(
const std::function<std::string(std::string)> &func);
108 void SetPrintVersion(
const std::function<
void(
const std::string &)> &func);
111 void AddEnv(
const std::string &conf,
const std::string &env)
125 const std::map<std::string,T>
GetOptions(
const std::string &opt)
129 std::map<std::string,T> map;
130 for (
auto it=rc.begin(); it!=rc.end(); it++)
131 map[it->substr(opt.length())] = Get<T>(*it);
136 std::multimap<std::string, std::string>
GetOptions()
const;
139 const po::variables_map &
Parse(
int argc,
const char **argv,
const std::function<
void()> &func=std::function<
void()>());
140 bool DoParse(
int argc,
const char **argv,
const std::function<
void()> &func=std::function<
void()>());
144 return Has(
"version");
149 return Has(
"help") ||
Has(
"help-config") ||
Has(
"help-env") ||
Has(
"help-database");
154 return Has(
"print-all") ||
Has(
"print") ||
Has(
"print-default") ||
155 Has(
"print-database") ||
Has(
"print-config") ||
156 Has(
"print-environment") ||
Has(
"print-unknown") ||
157 Has(
"print-options") ||
Has(
"print-wildcards");
162 T
Get(
const std::string &
var) { fWildcardOptions.erase(var);
return fVariables[
var].as<T>(); }
163 bool Has(
const std::string &
var) { fWildcardOptions.erase(var);
return fVariables.count(var)>0; }
166 std::vector<T>
Vec(
const std::string &
var) {
return Has(var) ? fVariables[
var].as<std::vector<T>>() : std::vector<T>(); }
168 template<
class T,
class S>
169 T
Get(
const std::string &
var,
const S &val)
171 std::ostringstream
str;
173 return Get<T>(str.str());
177 bool Has(
const std::string &
var,
const T &val)
179 std::ostringstream
str;
181 return Has(str.str());
184 template<
class T,
class S>
187 return Has(var, val) ? Get<T>(
var, val) : Get<T>(var+
"default");
194 const bool rc =
Has(var+
"default");
196 return Has(var, val) ?
true : rc;
201 fVariables.erase(var);
241 Hex(
const T &v) : val(v) { }
242 operator T()
const {
return val; }
248 in >> std::hex >> val;
254 inline po::typed_value<T> *
var(T *ptr=0)
255 {
return po::value<T>(ptr); }
258 inline po::typed_value<T> *
var(
const T &val, T *ptr=0)
259 {
return po::value<T>(ptr)->default_value(val); }
262 inline po::typed_value<std::vector<T>> *
vars()
263 {
return po::value<std::vector<T>>(); }
266 {
return po::bool_switch(); }
268 inline po::typed_value<bool> *
po_bool(
bool def=
false)
269 {
return po::value<bool>()->implicit_value(
true)->default_value(def); }
T Get(const std::string &var, const S &val)
void PrintWildcardOptions() const
void AddOptionsEnvironment(const po::options_description &env, bool visible=true)
std::string fDatabase
File name of the default configuration file (usually {program}.rc)
std::string fPriorityFile
Options which were registered using wildcards.
Configuration(const std::string &prgname="")
bool Has(const std::string &var, const T &val)
std::string VecAsStr(const po::variable_value &v) const
std::vector< std::string > fUnknownDatabase
Storage container for unrecognized options from the environment.
void AddOptionsConfigfile(const po::options_description &cf, bool visible=true)
T Get(const std::string &var)
po::options_description fOptionsEnvironment[2]
Description of options from the database.
po::typed_value< T > * var(T *ptr=0)
po::typed_value< bool > * po_switch()
void CreateWildcardOptions()
Helper for Parse to create list of used wildcard options.
const std::map< std::string, T > GetOptions(const std::string &opt)
void AddEnv(const std::string &conf, const std::string &env)
void Remove(const std::string &var)
std::vector< T > Vec(const std::string &var)
Index for hidden options (not shown in PrintParsed)
void SetArgumentPositions(const po::positional_options_description &desc)
std::function< std::string(std::string)> fNameMapper
Pointer to the mapper function for environment variables.
Index for options visible in PrintParsed.
po::positional_options_description fArgumentPositions
Description of options from the environment.
std::multimap< std::string, std::string > GetOptions() const
po::options_description fOptionsDatabase[2]
Description of the options in the configuration file.
void PrintParsed(const po::parsed_options &parsed) const
Print all options from a list of already parsed options.
void AddOptionsDatabase(const po::options_description &db, bool visible=true)
std::function< void(const std::string &)> fPrintVersion
void PrintUnknown() const
bool Has(const std::string &var)
T GetDef(const std::string &var, const S &val)
void PrintOptions() const
po::typed_value< std::vector< T > > * vars()
std::istream & operator>>(std::istream &in, Hex< T > &rc)
std::string fDefaultFile
File name of the priority configuration file (overwrites option from the databse) ...
void AddOptions(const po::options_description &opt, bool visible=true)
void AddOptionsCommandline(const po::options_description &cl, bool visible=true)
po::options_description fOptionsCommandline[2]
Commandline parsing, resource file parsing and database access.
std::map< std::string, std::string > fWildcardOptions
Storage container for unrecognized options retrieved from the database.
po::options_description fOptionsConfigfile[2]
Description of the command-line options.
std::vector< std::string > fUnknownCommandline
Description of positional command-line options (arguments)
std::function< void()> fPrintUsage
std::string VarAsStr(const po::variable_value &v) const
static po::basic_parsed_options< char > parse_database(const std::string &prgname, const std::string &database, const po::options_description &desc, bool allow_unregistered=false)
Retrieve data from a database and return them as options.
static void Max(int &val, const int &comp)
Helper function which return the max of the two arguments in the first argument.
const std::map< std::string, std::string > & GetWildcardOptions() const
po::variables_map fVariables
URL for database connection (see Configuration::parse_database)
std::string DefaultMapper(const std::string env)
Variables as compiled by the Parse-function, which will be passed to the program. ...
virtual void PrintVersion() const
po::typed_value< bool > * po_bool(bool def=false)
std::string UnLibToolize(const std::string &src) const
const po::variables_map & Parse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
virtual void PrintUsage() const
std::vector< std::string > fUnknownConfigfile
Storage container for unrecognized commandline options.
bool HasDef(const std::string &var, const T &val)
std::vector< std::string > fUnknownEnvironment
Storage container for unrecognized options from configuration files.
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
const std::string & GetName() const
std::map< std::string, std::string > fEnvMap
argv[0]