7 #include <boost/program_options.hpp> 11 namespace po = boost::program_options;
14 bool config_file_found(
16 boost::filesystem::path& full_path) {
20 }
catch (std::runtime_error
const&) {
32 int& argc,
char* argv[], std::string
const& filename,
33 char const* environment_variable_name) {
34 char argv0[] =
"undefined";
36 argc > 1 ? argv[0] : argv0, environment_variable_name);
37 boost::filesystem::path full;
38 if (config_file_found(search, filename, full)) {
40 std::ifstream is(full.string());
48 int& argc,
char* argv[], std::string
const& filename) {
50 char argv0[] =
"undefined";
52 boost::filesystem::path full;
53 if (config_file_found(search, filename, full)) {
55 std::ifstream is(full.string());
63 int& argc,
char* argv[], std::istream& is) {
64 YAML::Node doc = YAML::Load(is);
78 if (i->class_name() !=
"") {
79 std::string key = std::string(
":") + i->class_name();
80 auto n = by_class.find(key);
81 if (n != by_class.end()) {
82 i->apply_overrides(n->second, by_class);
87 if (by_name and by_name.IsMap() and by_name[i->name()]) {
88 nested = by_name[i->name()];
91 i->apply_overrides(nested, new_scope);
96 po::options_description options(
"Program Options");
97 options.add_options()(
"help",
"produce help message")(
98 "help-in-test",
"produce help message (Boost.Test captures --help)");
104 po::positional_options_description positional;
106 if (i->positional()) {
107 positional.add(i->name().c_str(), 1);
111 po::variables_map vm;
113 po::command_line_parser(argc, argv)
115 .positional(positional)
120 if (vm.count(
"help") or vm.count(
"help-in-test")) {
121 std::ostringstream os;
122 os << options <<
"\n";
139 po::options_description& options, std::string
const& prefix,
141 po::options_description group(d.
helpmsg);
144 i->add_options(group, prefix, i->descriptor());
150 po::variables_map
const& vm, std::string
const& prefix) {
172 std::string
const& prefix, std::string
const& name) {
176 std::string tmp = prefix;
185 doc[i->name()] = i->to_yaml();
attribute_base(attribute_descriptor const &d, config_object *container)
Constructor.
class_overrides clone(class_overrides const &by_class)
Recursively clone all the overrides in by_class.
void merge(class_overrides &by_class, YAML::Node source)
Merge the class-overrides from source into by_class.
Base class for all configuration objects.
virtual void validate() const
Validate the settings.
boost::filesystem::path find_configuration_file(std::string const &filename, validator_functor validator) const
Find a configuration file in the computed search path.
void validate_attributes() const
Run validate() on each attribute contained by this config_object.
std::map< std::string, YAML::Node > class_overrides
Store the overrides for each class.
virtual ~attribute_base()=0
Destructor.
void load_overrides(int &argc, char *argv[], std::string const &filename, char const *environment_variable_name)
Read the configuration file and load the overrides defined therein.
config_object()
Default constructor.
std::vector< attribute_base * > attributes_
The list of attributes.
std::ostream & to_stream(std::ostream &os) const
Print out the settings.
void auto_register(attribute_base *a)
Define the interface to manipulate and access configuration attributes embedded in a config_object...
A simple class to communicate the result of parsing the options.
void apply_cmdline_values(boost::program_options::variables_map const &vm, std::string const &prefix)
Apply the values from the cmdline to this configuration object.
void process_cmdline(int &argc, char *argv[])
Process the command line.
YAML::Node to_yaml() const
Print out the configuration settings in YAML format.
void apply_overrides(YAML::Node const &by_name)
Apply the overrides contained in the YAML document, compute the initial by_class overrides.
void validate_all() const
static std::string cmdline_arg_name(std::string const &prefix, std::string const &name)
Compute the full name of a command-line argument, given its prefix and short name.
void add_options(boost::program_options::options_description &options, std::string const &prefix, attribute_descriptor const &d) const
Add the attributes of this config_object as command-line options.
Compute the directories where a configuration file can be found.