Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::program_options silent name conflicts
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-04-28 19:57:30


On Wed, Apr 28, 2010 at 4:41 PM, Michael Jeung <jeungster_at_[hidden]> 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.


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