Boost logo

Boost Users :

Subject: [Boost-users] boost::program_options silent name conflicts
From: Michael Jeung (jeungster_at_[hidden])
Date: 2010-04-28 18:41:55


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;

}


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