Boost logo

Boost Users :

Subject: [Boost-users] problem with boost::program_options - unregistered option in Config File
From: Hasanul Ferdaus (sunny_register_at_[hidden])
Date: 2009-06-25 12:58:52


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"].as<std::string>() << "\n";
}

if (variablesMap->count("tcp.port")){
        std::cout <<(*variablesMap)["tcp.port"].as<std::string>() << "\n";
}

if (variablesMap->count("bootstrap.hints")){
        std::cout <<(*variablesMap)["bootstrap.hints"].as<std::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_config_file.html

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 <sunny_register_at_[hidden]> wrote:

From: Hasanul Ferdaus <sunny_register_at_[hidden]>
Subject: problem with boost::program_options
To: boost-users_at_[hidden]
Date: Thursday, June 25, 2009, 9:41 AM

Hi,
I am trying to parse unregistered options using  boost::program_options for config files but it does not recognize the unregistered options. the code is like to the following -
--------------------------------
boost::program_options::options_description *optionsDescription;
boost::program_options::variables_map *variablesMap;
optionsDescription->add_options()
        ("ariba.bootstrap.hints", "Bootstrap Hints");

std::ifstream ifs(configFileName.c_str());
store(parse_config_file(ifs, *optionsDescription, true), *variablesMap);
notify(*variablesMap);
----------------------------------

As it says, passing true to the 3rd argument of parse_config_file() should allow unregistered options to be parsed, but it does not recognize any unregistered options in config file.
If anyone knows how to enable unregistered options in config file, please respond.
Thanks in advance.
Sunny



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