program options: unrecognized command-line options

I'm using the program options library to parse both command-line and configuration file parameters. I'd like to know how to find command-line arguments that are *not* registered and therefore not recognized by the parser. I want to be able to print a warning message like "Following arguments were ignored:....".

Deane Yang wrote:
I'm using the program options library to parse both command-line and configuration file parameters. I'd like to know how to find command-line arguments that are *not* registered and therefore not recognized by the parser. I want to be able to print a warning message like "Following arguments were ignored:....".
I did it like that: // assume that options is a std::vectorstd::string containing // the passed options (argv[]) po::options_description desc; // contains the options your prog supports po::variables_map vm; po::command_line_parser parser(options).allow_unregistered().options(desc); po::parsed_options parsed = parser.run(); po::store(parsed, vm); po::notify(vm); // recognized options std::vectorstd::string unrecognizedOptions = po::collect_unrecognized(parsed.options, po::include_positional); This is documented at doc/html/program_options/howto.html#id3458178 in your Boost docs. Best regards Christoph

Christoph Duelli wrote:
Deane Yang wrote:
I'm using the program options library to parse both command-line and configuration file parameters. I'd like to know how to find command-line arguments that are *not* registered and therefore not recognized by the parser. I want to be able to print a warning message like "Following arguments were ignored:....".
I did it like that:
// assume that options is a std::vectorstd::string containing // the passed options (argv[]) po::options_description desc; // contains the options your prog supports
po::variables_map vm; po::command_line_parser parser(options).allow_unregistered().options(desc);
po::parsed_options parsed = parser.run(); po::store(parsed, vm); po::notify(vm); // recognized options
std::vectorstd::string unrecognizedOptions = po::collect_unrecognized(parsed.options, po::include_positional);
This is documented at doc/html/program_options/howto.html#id3458178 in your Boost docs.
Thanks. I did see this, but was hoping for something more graceful. I find it ironic that to disallow unregistered options, I have to use the function allow_unregistered. Shouldn't the opposite of "allow_unregistered" be a state where unregistered options are forbidden? Deane

Deane Yang wrote: <snip>
This is documented at doc/html/program_options/howto.html#id3458178 in your Boost docs.
Thanks. I did see this, but was hoping for something more graceful. I find it ironic that to disallow unregistered options, I have to use the function allow_unregistered. Shouldn't the opposite of "allow_unregistered" be a state where unregistered options are forbidden? afaicr they *are* disallowed unless you call allow_unregistered().

Quoting Deane Yang
Christoph Duelli wrote:
Deane Yang wrote:
I'm using the program options library to parse both command-line and configuration file parameters. I'd like to know how to find command-line arguments that are *not* registered and therefore not recognized by the parser. I want to be able to print a warning message like "Following arguments were ignored:....".
I did it like that:
// assume that options is a std::vectorstd::string containing // the passed options (argv[]) po::options_description desc; // contains the options your prog supports
po::variables_map vm; po::command_line_parser parser(options).allow_unregistered().options(desc);
po::parsed_options parsed = parser.run(); po::store(parsed, vm); po::notify(vm); // recognized options
std::vectorstd::string unrecognizedOptions = po::collect_unrecognized(parsed.options, po::include_positional);
This is documented at doc/html/program_options/howto.html#id3458178 in your Boost docs.
Thanks. I did see this, but was hoping for something more graceful. I find it ironic that to disallow unregistered options, I have to use the function allow_unregistered. Shouldn't the opposite of "allow_unregistered" be a state where unregistered options are forbidden?
That's perhaps a little unfair as the library by default throws an exception on discovering unregistered options. Is it the case that you are just looking for a better exception message?

Peter Bartlett wrote:
Quoting Deane Yang
: Christoph Duelli wrote:
Deane Yang wrote:
I'm using the program options library to parse both command-line and configuration file parameters. I'd like to know how to find command-line arguments that are *not* registered and therefore not recognized by the parser. I want to be able to print a warning message like "Following arguments were ignored:....".
I did it like that:
// assume that options is a std::vectorstd::string containing // the passed options (argv[]) po::options_description desc; // contains the options your prog supports
po::variables_map vm; po::command_line_parser parser(options).allow_unregistered().options(desc);
po::parsed_options parsed = parser.run(); po::store(parsed, vm); po::notify(vm); // recognized options
std::vectorstd::string unrecognizedOptions = po::collect_unrecognized(parsed.options, po::include_positional);
This is documented at doc/html/program_options/howto.html#id3458178 in your Boost docs.
Thanks. I did see this, but was hoping for something more graceful. I find it ironic that to disallow unregistered options, I have to use the function allow_unregistered. Shouldn't the opposite of "allow_unregistered" be a state where unregistered options are forbidden?
That's perhaps a little unfair as the library by default throws an exception on discovering unregistered options. Is it the case that you are just looking for a better exception message?
Agreed. So I apologize. For some reason I did not see this happening before. I was under the impression that the unregistered options were simply being ignored.
participants (3)
-
Christoph Duelli
-
Deane Yang
-
Peter Bartlett