problem with boost::program_options - unregistered option in Config File
Thanks for your reply.
I am using boost::program_options to parse a config file like below
--- sample.conf ---
node.initiator=true
tcp.port=8080
bootstrap.hints={ip{127.0.0.1},tcp(ip,{5002})}
----------------------------
Below I give the code that tries to read the config file sample.conf and tries to get all the options mentioned in file sample.conf -
------- Code -----
void main(){
std::string configFileName("sample.conf");
boost::program_options::options_description *optionsDescription;
boost::program_options::variables_map *variablesMap;
optionsDescription = new boost::program_options::options_description("Configuration");
variablesMap = new boost::program_options::variables_map();
optionsDescription->add_options()
("tcp.port", "TCP Port Number")
("bootstrap.hints", "Bootstrap Hints");
std::ifstream ifs(configFileName.c_str());
store(parse_config_file(ifs, *optionsDescription, true), *variablesMap);
notify(*variablesMap);
if (variablesMap->count("node.initiator"){
std::cout <<(*variablesMap)["node.initiator"].asstd::string() << "\n";
}
if (variablesMap->count("tcp.port")){
std::cout <<(*variablesMap)["tcp.port"].asstd::string() << "\n";
}
if (variablesMap->count("bootstrap.hints")){
std::cout <<(*variablesMap)["bootstrap.hints"].asstd::string() << "\n";
}
delete optionsDescription;
delete variablesMap;
return ;
}
-----------------
Now, the output is like below -
--- output ---
8080
{ip{127.0.0.1},tcp(ip,{5002})}
---------
But the value of the option node.initiator (i.e. "true") is not shown, also there is no exception. If I pass "false" in the 3rd parameter of parse_config_file() rather than "true", it throws exception, as it should do. But my question is, why "node.initiator" option is not in the variablesMap, as parse_config_file() should have parsed both registered and unregistered options. Surely if(variablesMap->count("node.initiator") does not pass.
In the following link, it is mentioned that "allow_unregistered = false" is the default argument for parse_config_file() function, but I am surely passing "true" in my code.
http://www.boost.org/doc/libs/1_38_0/doc/html/boost/program_options/parse_co...
In another post you mentioned that SVN HEAD supports unregistered options in config file. Just now I have build the program_option library from boost svn, but there is no difference in the result.
Awaiting your response.
Thank you.
--- On Thu, 6/25/09, Hasanul Ferdaus
Hasanul Ferdaus wrote:
But the value of the option node.initiator (i.e. "true") is not shown, also there is no exception. If I pass "false" in the 3rd parameter of parse_config_file() rather than "true", it throws exception, as it should do. But my question is, why "node.initiator" option is not in the variablesMap, as parse_config_file() should have parsed both registered and unregistered options.
Why? Documentation say that you should use collect_unrecognized: http://www.boost.org/doc/libs/1_39_0/doc/html/program_options/howto.html#id2... It does not seem reasonable to not declare an option and then expect that the interface specifically design to work with previously registered options will be useful. - Volodya
participants (2)
-
Hasanul Ferdaus
-
Vladimir Prus