prefix_ void senf::console::detail::ConfigFileSource::v_parse(RestrictedExecutor & executor)
{
- parser_.parseFile(filename_, boost::bind( boost::ref(executor),
- boost::ref(std::cerr),
- _1 ));
+ try {
+ parser_.parseFile(filename_, boost::bind( boost::ref(executor),
+ boost::ref(std::cerr),
+ _1 ));
+ }
+ catch (SystemException & ex) {
+ if (! (ignoreMissing_ && ex.anyOf(ENOENT)))
+ throw;
+ }
}
///////////////////////////////////////////////////////////////////////////
return ptr(new ConfigFileSource(filename));
}
+prefix_ senf::console::detail::ConfigFileSource::ptr
+senf::console::detail::ConfigFileSource::ignoreMissing()
+{
+ ignoreMissing_ = true;
+ return ptr(this);
+}
+
prefix_ senf::console::detail::ConfigFileSource::ConfigFileSource(std::string const & filename)
- : filename_ (filename)
+ : filename_ (filename), ignoreMissing_ (true)
{}
///////////////////////////////////////////////////////////////////////////
prefix_ senf::console::ConfigFile::ConfigFile(std::string const & filename,
DirectoryNode & root)
- : detail::BundleMixin(root)
+ : detail::BundleMixin(root), self_ (add(detail::ConfigFileSource::create(filename)))
+{}
+
+prefix_ void senf::console::ConfigFile::ignoreMissing()
{
- add(detail::ConfigFileSource::create(filename));
+ self_.ignoreMissing();
}
///////////////////////////////////////////////////////////////////////////
///@}
///////////////////////////////////////////////////////////////////////////
+
+ void ignoreMissing(); ///< Call to ignore missing files
+
+ private:
+ detail::ConfigFileSource & self_;
};
/** \brief Read configuration file
static ptr create(std::string const & filename);
+ ptr ignoreMissing();
+
private:
ConfigFileSource(std::string const & filename);
virtual void v_parse(RestrictedExecutor & executor);
std::string filename_;
+ bool ignoreMissing_;
CommandParser parser_;
};
senf::console::ConfigFile cfg (cfgf.name());
var1 = 0;
- SENF_CHECK_NO_THROW( cfg.parse() )
+ SENF_CHECK_NO_THROW( cfg.parse() );
BOOST_CHECK_EQUAL( var1, 10 );
var1 = 0;
- SENF_CHECK_NO_THROW( cfg.parse() )
+ SENF_CHECK_NO_THROW( cfg.parse() );
BOOST_CHECK_EQUAL( var1, 0 );
var1 = 0;
cfg.reset();
- SENF_CHECK_NO_THROW( cfg.parse() )
+ SENF_CHECK_NO_THROW( cfg.parse() );
BOOST_CHECK_EQUAL( var1, 10 );
}
+
+ {
+ senf::console::ConfigFile cfg ("i don't exist");
+ cfg.ignoreMissing();
+ SENF_CHECK_NO_THROW( cfg.parse() );
+ }
}
BOOST_AUTO_UNIT_TEST(configFileRestrict)
\code
senf::console::ConfigFile cf ("some.conf");
+ // The following line is optional: Call to ignore mussing files
+ cf.ignoreMissing();
cf.parse();
\endcode
std::vector<std::string> args;
senf::console::ConfigBundle conf (senf::console::root()["config"]);
conf.add( senf::console::FileConfig("/etc/myserver.conf") );
- conf.add( senf::console::FileConfig(".myserver.conf") );
+ conf.add( senf::console::FileConfig(".myserver.conf")->ignoreMissing() );
conf.add( senf::console::OptionsConfig(senf::Daemon::instance().argc(),
senf::Daemon::instance().argv()) )
.nonOptions(args)