|
Boost Users : |
From: Rupert Bruce (rupert_at_[hidden])
Date: 2008-05-24 11:03:31
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Frank Birbacher wrote:
> Hi!
>
> Rupert Bruce schrieb:
>> So anywhere in my code I can do, for example:
>> if (PropertyCatalog::getBooleanProperty("testapp.profile",false))
>> {
>> profile.append(currentStatus);
>> }
>
> Which has a design flaw, I think. I just got rid of a lot of such
> function calls in a project I'm working at. The flaw: do not keep
> configuration default values distributed in the code. What if the
> default value needs to change? It does commonly change for directory
> paths. If you now tell me to always write the correct value into the
> config file, then I wouldn't need any default value at all and I could
> just remove them. And if you need different default values for the same
> key in different places, then you simply need also different keys.
>
> I now have "getConfig" calls without defaults in the call.
>
> Frank
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
Personally, I like the defaults (I typically replace a hard-coded value
in the code with a call to PropertyCatalog so the default is obvious).
One advantage of Polymorphism is that I don't need to specify a default:
PropertyCatalog::getBooleanProperty("testapp.profile")
As you mention, file paths are a common default that changes - one that
I frequently have to override on the command line is the path to the
config file itself. One benefit of boost::program_options is that I have
that flexibility. This is from a test case with no properties:
int main(int argc, char *argv[])
{
PropertyCatalog::populate(argc, argv);
string
log_config_file=PropertyCatalog::getStringProperty("config.logging",
"../config/log4cxx.properties");
log4cxx::PropertyConfigurator::configure(LOG4CXX_FILE(log_config_file));
if (PropertyCatalog::isDefined("help"))
{
PropertyCatalog::logPropertyCatalog();
}
return UnitTest::RunAllTests();
}
void PropertyCatalog::populate(int argc, char **argv)
{
options_description general("General options");
general.add_options()
("help", "produce a help message")
("config", value<string>(),"override the default config file name")
("version", "output the version number");
string defaultConfigFileName =
static_cast<string>(argv[0])+".properties";
string config_file=PropertyCatalog::getStringProperty("config",
defaultConfigFileName);
options_description cmdline_options;
cmdline_options.add(general);
options_description config_file_options;
config_file_options.add(general);
//class variable 'visible' holds the set of option_descriptions that
the user
//may want to have listed via logPropertyCatalog()
visible.add(general);
store(parse_command_line(argc, argv, cmdline_options), vm);
//load the config file
ifstream ifs(config_file.c_str());
store(parse_config_file(ifs, config_file_options), vm);
notify(vm);
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org
iD8DBQFIOC5D/X5Z5iyVCbERAoHPAJ9/p/GNwuLHpZh2yBiqlyp+WbrEaQCdFMIZ
yvAtqhEkkaRRrB7ojhSe9Lw=
=Pu8n
-----END PGP SIGNATURE-----
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net