Question

I am using boost::program_options to parse argv. I expect both -c and--config

  boost::program_options::options_description description("Utility");
  description.add_options()
    ("help,h", "display this message")
    ("config,c", boost::program_options::value<std::string>(), "Path to configuration file")
    ("config-type", boost::program_options::value<std::string>()->default_value("json"), "type of configuration file (json|xml)")
    ("verbose,v", boost::program_options::value<int>()->default_value(2), "verbosity(0 to 2)")
    ("thread,t",boost::program_options::value<int>()->default_value(0), (boost::format("Max Thread Count %1% to %2%(Processor cores of this machine) if not multi threaded") % 0 % boost::thread::hardware_concurrency()).str().c_str())
    ("action,a", boost::program_options::value<std::string>()->default_value("pack"), "action to perfoem (pack|unpack)");
  boost::program_options::positional_options_description positional_description;
  positional_description.add("action", -1);

  boost::program_options::variables_map var_map;
  boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(description).positional(positional_description).style(boost::program_options::command_line_style::unix_style).run(), var_map);
  boost::program_options::notify(var_map);

  if(var_map.count("help")){
    std::cout << description;
    return 1;
  }
  if(var_map.count("config") < 1){
    std::cout << "No Configuration file added" << std::endl;
    return 1;
  }

  if(var_map.count("action") < 1){
    std::cout << "Please specify an action to perfoem (pack|unpack)" << std::endl;
    return 1;
  }

but --config f or --config=f or--config="f" doesn't work and prints No Configuration file added though -c f works.
also If I use --config without any argument it throws exception saying required parameter is missing in 'config-type' which already has a default parameter.

Était-ce utile?

La solution

Your problem seems to be exactly the same as described here: boost::program_options - does it do an exact string matching for command line options?

I am using ubuntu with boost 1.42 and ubuntu repo doesn't have a higher version. but is 1.42 that buggy ?

Yes.

You can workaround the problem by swapping the specification of --config and --config-type.

The cleaner solution is to upgrade Boost. Vladimir Prus in the mentioned SO answer says the bug is fixed in 1.45. From what you wrote I suppose you're using Ubuntu 11.04 (Natty Narwhal). You can do either of the following:

  1. Install newer Boost packages from Ubuntu 11.10 (Boost 1.46) or Ubuntu 12.04 (Boost 1.48) - by temporarily substituting natty with oneiric or precise for 11.10 or 12.04 respectively in /etc/apt/sources.list
  2. Install newer Boost packages a PPA which contain Boost backports (like e.g. https://launchpad.net/~purplekarrot/+archive/ppa)
  3. Build Boost from source as Xeo suggested.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top