#ifndef FACT_Configuration #define FACT_Configuration #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::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 boost::function fNameMapper; boost::function fPrintUsage; /// 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; } /// 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(std::vector &vec, int steps=1); virtual void PrintUsage() const { } public: Configuration(const std::string &prgname=""); /// Retrieve data from a database and return them as options static po::basic_parsed_options parse_database(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 boost::function &func); void SetNameMapper(); void SetPrintUsage(const boost::function &func); void SetPrintUsage(); void AddEnv(const std::string &conf, const std::string &env) { fEnvMap[env] = conf; } // Output void PrintOptions(); void PrintUnknown(); // Process command line arguments const po::variables_map &Parse(int argc, const char **argv); bool HasVersion() const { return Has("version"); } bool HasHelp() const { return Has("help") || Has("help-config") || Has("help-env") || Has("help-database"); } bool HasPrint() const { return Has("print-all") || Has("print") || Has("print-default") || Has("print-database") || Has("print-config") || Has("print-environment") || Has("print-unkown") || Has("print-options"); } // Simplified access to the parsed options template T Get(const char *var) const { return Has(var) ? fVariables[var].as() : T(); } bool Has(const char *var) const { return fVariables.count(var)>0; } const std::string &GetName() const { return fName; } }; 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() { return po::value()->implicit_value(true)->default_value(false); } #endif