These are chat archives for kbknapp/clap-rs

21st
Feb 2018
Jaanus Varus
@discosultan
Feb 21 2018 09:21

Quick question: https://play.rust-lang.org/?gist=52b1125daf7417f4c89c598b55b5629a&version=stable

In the sample code, in case the subcommand is not passed, is there a simple way to print help and exit gracefully? Currently it prints help but exits with code 1.

I checked for AppSettings, but couldn't find anything suitable. Alternatively, I could use get_matches_safe and process the result myself, but that's already getting complicated.

Kevin K.
@kbknapp
Feb 21 2018 14:36
Yeah SubcommandRequiredElseHelp will exit with a code of 1 because it was technically (although this is debatable) an error. i.e. the user failed to do something and got a message.
You can however do what you want manually.
One sec I'll send you a playground link
Keep in mind the help is printed to stdout in both of those cases, whereas in SubcommandRequiredElseHelp it's printed to stderr
Kevin K.
@kbknapp
Feb 21 2018 14:49
If you want to write the message to somewhere other than stdout you can use App::write_help instead of App::print_help
Jaanus Varus
@discosultan
Feb 21 2018 14:49

Thanks @kbknapp !

I'm still a bit curious. Reading the API doc for AppSettings::SubcommandRequiredElseHelp

Specifies that the help text should be displayed (before exiting gracefully) if no SubCommands are present at runtime (i.e. an empty run such as $ myprog).

shouldn't my sample already exit gracefully? Or I'm misunderstanding something.

Kevin K.
@kbknapp
Feb 21 2018 14:49
"exit gracefully" meaning not crash/panic
so it prints the error and exists, albeit with an error code of 1
but the error code is set by clap to indicate an error was encountered
Jaanus Varus
@discosultan
Feb 21 2018 14:50
Aha, yes, makes sense :)
Awesome, thanks a ton! Love the library!
Kevin K.
@kbknapp
Feb 21 2018 14:50
No problem! Thanks :D