I'm using program_options to parse commandLine and configuration options and I found what seems to be a bug. The problem appears when using vector options with similar names. If I have an unspecified parameter "myParam" and another specified parameter "myParam2", the library appends the values of myParam to myParam2.
For example, when I call my program like this:
./model -myParam2={7,8,9} -myParam={5,6}
I get:
myParam not declared <-- This is OK
myParam2 size: 5 <-- I would expect this to be size:3
I reduced my code to the following example showing the problem:
// Declare a group of options that will be allowed both on command line and in config file
po::options_description options("Simulator Configuration (and Command Line) options");
options.add_options()
//("myParam", po::value<std::vector<int>>(), "test") --> If I uncomment this line, it works as expected
("myParam2", po::value<std::vector<int>>(), "test");
// parse the cmdline options
auto parsed_cmdLine_options = po::command_line_parser(ac,av).options(options)
.style(po::command_line_style::unix_style | po::command_line_style::allow_long_disguise) // to enable options to start with a '-' instead of '--'
.allow_unregistered() // to allow generic parameters (which can be read by models)
.run();
po::store(parsed_cmdLine_options, simulatorParams);
notify(simulatorParams);
// print parsed options
std::vector<int> tmp1, tmp2;
if(simulatorParams.count("myParam")){
tmp1 = simulatorParams["myParam"].as<std::vector<int>>();
std::cout << "myParam size: " << tmp1.size() << "\n";
if(tmp1.size()!= 2){
throw "Wrong Size";
}
}else
{
std::cout << "myParam not declared \n";
}
if(simulatorParams.count("myParam2")){
tmp2 = simulatorParams["myParam2"].as<std::vector<int>>();
std::cout << "myParam2 size: " << tmp2.size() << "\n";
if(tmp2.size()!= 3){
throw "Wrong Size";
}
}
If I uncomment the line which registers "myParam" it all works as expected:
options.add_options()
("myParam", po::value<std::vector<int>>(), "test")
("myParam2", po::value<std::vector<int>>(), "test");
It prints:
myParam size: 2
myParam2 size: 3
I really need to use unregistered options as I re process them later in the execution.
It seem to be a very simple bug, so maybe I'm using the library somehow wrong.
Does anyone saw this problem before or any ideas how to workaround it?
Thank you very much!
EDIT: I'm using boost 1.48. I tried several other syntaxes for the parameters and it behaves the same way:
It's a feature. See command line style options:
Allow abbreviated spellings for long options, if they unambiguously identify long option. No long option name should be prefix of other long option name if guessing is in effect.