boost::program_options silent name conflicts
Hello all, I've run into a problem with the boost::program_options library and I wanted to see if anyone has experienced something similar. When my application defines two program options, pi and pie, the "pie" option silently overrides the "pi" option. (Source code attached.) ./my_program --pie pi: false pie: true ./my_program --pi pi: false pie: true Is this the expected behavior? Admittedly, this is a contrived example, but I think there are real-life cases where it can cause problems. I have run into it a couple of times now. -Michael ----------------------------------------------------------- #include <boost/program_options.hpp> #include <iostream> namespace po = boost::program_options; int main( const int argc, char *argv[] ) { bool pi = false; bool pie = false; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message") ("pi", po::value<bool>(&pi)->zero_tokens(), "calculate pi") ("pie", po::value<bool>(&pie)->zero_tokens(), "serve pie to our guests") ; po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } if ( pi ) std::cout<<"pi: true"<<std::endl; else std::cout<<"pi: false"<<std::endl; if ( pie ) std::cout<<"pie: true"<<std::endl; else std::cout<<"pie: false"<<std::endl; }
On Wed, Apr 28, 2010 at 4:41 PM, Michael Jeung <jeungster@gmail.com> wrote:
Hello all,
I've run into a problem with the boost::program_options library and I wanted to see if anyone has experienced something similar.
When my application defines two program options, pi and pie, the "pie" option silently overrides the "pi" option. (Source code attached.)
./my_program --pie pi: false pie: true
./my_program --pi pi: false pie: true
Is this the expected behavior? Admittedly, this is a contrived example, but I think there are real-life cases where it can cause problems. I have run into it a couple of times now.
-Michael
-----------------------------------------------------------
#include <boost/program_options.hpp> #include <iostream>
namespace po = boost::program_options;
int main( const int argc, char *argv[] ) { bool pi = false; bool pie = false;
po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message") ("pi", po::value<bool>(&pi)->zero_tokens(), "calculate pi") ("pie", po::value<bool>(&pie)->zero_tokens(), "serve pie to our guests") ;
po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).allow_unregistered().run(), vm); po::notify(vm);
if (vm.count("help")) { std::cout << desc << std::endl; return 0; }
if ( pi ) std::cout<<"pi: true"<<std::endl; else std::cout<<"pi: false"<<std::endl;
if ( pie ) std::cout<<"pie: true"<<std::endl; else std::cout<<"pie: false"<<std::endl;
}
That is caused be the ability for you to specify shorter names for the longer versions on the command line, for some causes (such as yours), you want to disable it. I do not know the command to do that, but it was on the mailing list not far back as I recall (probably/hopefully in the docs too). I cannot look it up at the moment, on my phone.
That is caused be the ability for you to specify shorter names for the longer versions on the command line, for some causes (such as yours), you want to disable it. I do not know the command to do that, but it was on the mailing list not far back as I recall (probably/hopefully in the docs too). I cannot look it up at the moment, on my phone.
Ah, yes - I found it. This solved my problem: po::store(po::command_line_parser(argc, argv) .options(desc) .style(po::command_line_style::default_style ^ po::command_line_style::allow_guessing) .allow_unregistered() .run(), vm); Thanks! -Michael
participants (2)
-
Michael Jeung
-
OvermindDL1