Using boost 1_55, I have a bool_switch() option, which is defined as:
bpo::options_description opts;
opts->add_options()
("foo", bpo::bool_switch(), "Enable foo.");
opts_map = new bpo::variables_map;
And it is parsed on the command line by:
bpo::store(bpo::parse_command_line(argc, argv, opts), *opts_map);
And also parsed in a config file by:
ifstream ifs("foo.conf");
if (ifs.good()) {
bpo::store(bpo::parse_config_file(ifs, opts), *opts_map);
close(ifs);
}
The trouble is that it works fine on the command line by either specifying --foo
or not, but it is always false
(with second.defaulted() == true
) when I put it in the config file. I have tried the following in the config file:
foo
foo=true
foo=1
Other types of options (e.g. bpo::value<ANYTYPE>()
with or without composing()
) work fine both on the command line and also in the config file, only bool_switch()
options are not working.
Any idea what I'm doing wrong? Or can you not use bool_switch()
options with parse_config_file()
?
EDIT:
A workaround is to use a value()
type with default_value()
and implicit_value()
:
opts->AddOptions()("foo", bpo::value<bool>()->default_value(false)->implicit_value(true), "Enable foo.");