program_options: emit usage message on unknown option ?
Is it possible to have boost::program_options emit a usage message when an unknown option is supplied ? I have code which is parsing its options like this:
void UserAPI::parse_options(const std::vectorstd::string& required_options, int argc, char* argv[], std::string usage) { po::store(po::parse_command_line(argc, argv, options_desc_, options_style_), vm_); po::notify(vm_);
// count how many required options we have
uint count = 0; for (std::vectorstd::string::const_iterator s = required_options.begin(); s != required_options.end(); ++s) { count += vm_.count(*s); }
// if we have a help option, or we don't have all the required options // emit the usage message
if ( vm_.count("help") || count != required_options.size() ) { std::cout << usage << std::endl; std::cout << options_desc_ << std::endl; exit(1); } }
If I pass an unknown option, it seems to be handled automagically:
install/linux/general -orgid ABC -p fred unknown option -p
I don't want this - I want to catch it somehow and emit my usage message, but this seems not to be documented, AFAICS. Anyone know how this can be done ? -- Regards Steve Collyer Netspinner Ltd
Hello,
I don't want this - I want to catch it somehow and emit my usage message, but this seems not to be documented, AFAICS.
Yes it is, you have to use allow_unregistered(): http://tinyurl.com/4jxgb2 Bruno
Bruno Lalande wrote:
Hello,
I don't want this - I want to catch it somehow and emit my usage message, but this seems not to be documented, AFAICS.
Yes it is, you have to use allow_unregistered():
Right. Thanks for that. I've solved the problem anyway, however. (the posting was stuck in the moderation queue). -- Regards Steve Collyer Netspinner Ltd
participants (2)
-
Bruno Lalande
-
Stephen Collyer