#ifndef FACT_Configuration #define FACT_Configuration #include #include namespace po = boost::program_options; class Configuration { private: /// Convienience enum to access the fOption* data memebers more verbosely. enum { kHidden = 0, ///< Index for hidden options (not shown in PrintParsed) kVisible = 1 ///< Index for options visible in PrintParsed }; const std::string fName; /// argv[0] std::map fEnvMap; po::options_description fOptionsCommandline[2]; /// Description of the command-line options po::options_description fOptionsConfigfile[2]; /// Description of the options in the configuration file po::options_description fOptionsDatabase[2]; /// Description of options from the database po::options_description fOptionsEnvironment[2]; /// Description of options from the environment po::positional_options_description fArgumentPositions; /// Description of positional command-line options (arguments) std::vector fUnknownCommandline; /// Storage container for unrecognized commandline options std::vector fUnknownConfigfile; /// Storage container for unrecognized options from configuration files std::vector fUnknownEnvironment; /// Storage container for unrecognized options from the environment std::vector fUnknownDatabase; /// Storage container for unrecognized options retrieved from the database std::map fWildcardOptions; /// Options which were registered using wildcards std::string fPriorityFile; /// File name of the priority configuration file (overwrites option from the databse) std::string fDefaultFile; /// File name of the default configuration file (usually {program}.rc) std::string fDatabase; /// URL for database connection (see Configuration::parse_database) po::variables_map fVariables; /// Variables as compiled by the Parse-function, which will be passed to the program /// A default mapper for environment variables skipping all of them std::string DefaultMapper(const std::string env) { return fEnvMap[env]; } /// Pointer to the mapper function for environment variables std::function fNameMapper; std::function fPrintUsage; std::function fPrintVersion; /// Helper function which return the max of the two arguments in the first argument static void Max(int &val, const int &comp) { if (comp>val) val=comp; } /// Helper for Parse to create list of used wildcard options void CreateWildcardOptions(); // Helper functions for PrintOptions and GetOptions template std::string VecAsStr(const po::variable_value &v) const; std::string VarAsStr(const po::variable_value &v) const; /// Print all options from a list of already parsed options void PrintParsed(const po::parsed_options &parsed) const; /// Print a list of all unkown options within the given vector void PrintUnknown(const std::vector &vec, int steps=1) const; virtual void PrintUsage() const { } virtual void PrintVersion() const; std::string UnLibToolize(const std::string &src) const; public: Configuration(const std::string &prgname=""); virtual ~Configuration() { } /// Retrieve data from a database and return them as options static po::basic_parsed_options parse_database(const std::string &prgname, const std::string &database, const po::options_description& desc, bool allow_unregistered=false); // Setup void AddOptionsCommandline(const po::options_description &cl, bool visible=true); void AddOptionsConfigfile(const po::options_description &cf, bool visible=true); void AddOptionsEnvironment(const po::options_description &env, bool visible=true); void AddOptionsDatabase(const po::options_description &db, bool visible=true); void AddOptions(const po::options_description &opt, bool visible=true) { AddOptionsCommandline(opt, visible); AddOptionsConfigfile(opt, visible); AddOptionsEnvironment(opt, visible); AddOptionsDatabase(opt, visible); } void SetArgumentPositions(const po::positional_options_description &desc); void SetNameMapper(const std::function &func); void SetNameMapper(); void SetPrintUsage(const std::function &func); void SetPrintUsage(); void SetPrintVersion(const std::function &func); void SetPrintVersion(); void AddEnv(const std::string &conf, const std::string &env) { fEnvMap[env] = conf; } // Output void PrintOptions() const; void PrintUnknown() const; void PrintWildcardOptions() const; const std::map &GetWildcardOptions() const { return fWildcardOptions; } const std::vector GetWildcardOptions(const std::string &opt) const; template const std::map GetOptions(const std::string &opt) { const std::vector rc = GetWildcardOptions(opt+'*'); std::map map; for (auto it=rc.begin(); it!=rc.end(); it++) map[it->substr(opt.length())] = Get(*it); return map; } std::multimap GetOptions() const; // Process command line arguments const po::variables_map &Parse(int argc, const char **argv, const std::function &func=std::function()); const po::variables_map &ParseFile(const std::string &fname, const bool &checkf); bool DoParse(int argc, const char **argv, const std::function &func=std::function()); bool ReadFile(const std::string &fname, const bool &checkf=false); bool HasVersion() { return Has("version"); } bool HasHelp() { return Has("help") || Has("help-config") || Has("help-env") || Has("help-database"); } bool HasPrint() { return Has("print-all") || Has("print") || Has("print-default") || Has("print-database") || Has("print-config") || Has("print-environment") || Has("print-unknown") || Has("print-options") || Has("print-wildcards"); } // Simplified access to the parsed options template T Get(const std::string &var) { fWildcardOptions.erase(var); return fVariables[var].as(); } bool Has(const std::string &var) { fWildcardOptions.erase(var); return fVariables.count(var)>0; } template std::vector Vec(const std::string &var) { return Has(var) ? fVariables[var].as>() : std::vector(); } template T Get(const std::string &var, const S &val) { std::ostringstream str; str << var << val; return Get(str.str()); } template bool Has(const std::string &var, const T &val) { std::ostringstream str; str << var << val; return Has(str.str()); } template T GetDef(const std::string &var, const S &val) { return Has(var, val) ? Get(var, val) : Get(var+"default"); } template bool HasDef(const std::string &var, const T &val) { // Make sure the .default option is touched const bool rc = Has(var+"default"); return Has(var, val) ? true : rc; } void Remove(const std::string &var) { fVariables.erase(var); } /* template std::map GetMap(const std::string &var) { const size_t len = var.length(); std::map rc; for (std::map::const_iterator it=fVariables.begin(); it!=fVariables.end(); it++) if (it->first.substr(0, len)==var) rc[it->first] = it->second.as(); return rc; } template std::vector GetKeys(const std::string &var) { const size_t len = var.length(); std::vector rc; for (std::map::const_iterator it=fVariables.begin(); it!=fVariables.end(); it++) if (it->first.substr(0, len)==var) rc.push_back(it->first); return rc; } */ const std::string &GetName() const { return fName; } }; template struct Hex { T val; Hex() { } Hex(const T &v) : val(v) { } operator T() const { return val; } }; template std::istream &operator>>(std::istream &in, Hex &rc) { T val; in >> std::hex >> val; rc.val = val; return in; } template inline po::typed_value *var(T *ptr=0) { return po::value(ptr); } template inline po::typed_value *var(const T &val, T *ptr=0) { return po::value(ptr)->default_value(val); } template inline po::typed_value> *vars() { return po::value>(); } inline po::typed_value *po_switch() { return po::bool_switch(); } inline po::typed_value *po_bool(bool def=false) { return po::value()->implicit_value(true)->default_value(def); } #endif