catchorg / Clara

A simple to use, composable, command line parser for C++ 11 and beyond
Boost Software License 1.0
649 stars 67 forks source link

Provide a built-in way of dealing with --help option #8

Closed vadz closed 6 years ago

vadz commented 8 years ago

Almost(?) all command line programs will have the --help option so IMO it would make sense to either support it out of the box or at least make it simpler to implement it than currently as it involves many steps:

  1. Call bindProcessName() (it's not going to be used for anything else than the usage message).
  2. Add bool help to the options struct.
  3. Bind -h and --help to it.
  4. Call usage() if help is true.

I'd rather have a way to tell parse() to do all of this automatically, but this would require changing its return type, as we need to know whether help was requested or not, so maybe a better idea would be to add another method, e.g. parse_or_give_help() returning bool.

OTOH I think it's quite wrong for parse() to throw on command line syntax errors, this is not something exceptional and can (and does) happen all the time. So personally I'd rather change parse() to return an enum indicating whether the command line was successfully parsed, an error occurred or help was requested.

philsquared commented 8 years ago

Yeah, I've had all the same thoughts - and a few ideas about how to address them - but not been entirely satisfied with any of them yet. Still thinking...

philsquared commented 7 years ago

The new version of Clara, which has been completely rewritten (warning: very breaking changes), now has a dedicated Help() parser which, I believe, does what you want. Also the new Clara uses a Result type for error propagation, rather than exceptions. Hopefully this is much closer to what you wanted (if you're still interested - obviously your original issue was raised a while ago)