Browse Source

first version of settings module

tempestpy_adaptions
gereon 12 years ago
parent
commit
7f7234f62b
  1. 132
      src/utility/settings.h

132
src/utility/settings.h

@ -0,0 +1,132 @@
/*
* settings.h
*
* Created on: 22.11.2012
* Author: Gereon Kremer
*/
#pragma once
#include <iostream>
#include <boost/program_options.hpp>
namespace mrmc {
namespace settings {
namespace bpo = boost::program_options;
/*!
* @brief Simple wrapper around boost::program_options to handle configuration options.
*
* This class uses boost::program_options to read options from the
* commandline and additionally load options from a file.
*/
class Settings
{
private:
/*!
* @brief option descriptions
*/
bpo::options_description configfile;
bpo::options_description generic;
bpo::options_description commandline;
/*!
* @brief collecing option descriptions
*
* The options for command line and config file are collected
* here
*/
bpo::options_description cli;
bpo::options_description conf;
/*!
* @brief option mapping
*/
bpo::variables_map vm;
public:
/*!
* @brief Constructor of Settings file
*
* The constructor fills the option descriptions, parses the
* command line and the config file and puts the option values to
* our option mapping.
*
* If a configfile is set in the commandline, we load this one.
* Otherwise, if filename is not NULL, we load this one. Otherwise,
* we load no config file.
*
* @param argc should be argc passed to main function
* @param argv should be argv passed to main function
* @param filename either NULL or name of config file
*/
Settings(const int argc, const char* argv[], const char* filename)
: configfile("Config Options"), generic("Generic Options"), commandline("Commandline Options")
{
/*
* fill option descriptions
*/
this->commandline.add_options()
("help", "produce help message")
("help-config", "produce help message about config file")
("configfile", bpo::value<std::string>(), "name of config file")
;
/*
* construct option descriptions for commandline and config file
*/
this->cli.add(this->commandline).add(generic);
this->conf.add(this->configfile).add(generic);
try
{
/*
* load command line
*/
bpo::store(bpo::parse_command_line(argc, argv, this->cli), this->vm);
/*
* load config file if specified
*/
if (this->vm.count("configfile"))
{
bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), this->conf), this->vm, true);
}
else if (filename != NULL)
{
bpo::store(bpo::parse_config_file<char>(filename, this->conf), this->vm, true);
}
}
/*
* catch errors...
*/
catch (bpo::reading_file e)
{
std::cout << "Could not read config file " << filename << std::endl;
}
catch (bpo::error e)
{
std::cout << "Some error occurred: " << e.what() << std::endl;
}
bpo::notify(this->vm);
}
/*!
* @brief Get option descriptions for commandline options
*/
bpo::options_description getHelpForCommandline()
{
return this->cli;
}
/*!
* @brief Get option descriptions for config file options
*/
bpo::options_description getHelpForConfigfile()
{
return this->conf;
}
};
} // namespace parser
} // namespace mrmc
Loading…
Cancel
Save