Question

I want to handle (note, not use in any way) the unknown options.

So there is this:

http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123440592

That can be used to collect and use any unknown options, so I could do:

po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(ac,av).options(desc).allow_unregistered().run();

{
  vector<string> to_pass_further = collect_unrecognized(parsed.options, po::include_positional);
  if (to_pass_further.size())
  {
    cout << "Unrecognized options:" << endl;
    for (auto i = to_pass_further.cbegin(); i != to_pass_further.cend(); ++i)
    {
      cout << std::left << std::setw(2) << ' ' << *i;
    return -1;
    }
  }
}

But since I would think people do this all the time and the library is supposed to handle all the things we do all the time, am I missing some cleaner what of doing it?

Était-ce utile?

La solution

try
{
  po::options_description desc("Allowed options");

  desc.add_options()
    ("help,h", "produce help message")
    ("compression", po::value<int>()->required(), "set compression level")
    ;


  po::variables_map vm;
  po::store(po::parse_command_line(ac, av, desc), vm);
  po::notify(vm);
}
catch(std::exception& e)
{
  std::cerr << "Error: " << e.what() << "\n";
  return -1;
}
catch(...)
{
  std::cerr << "Unknown error!" << "\n";
  return -1;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top