shflags icon indicating copy to clipboard operation
shflags copied to clipboard

Use of '--help' flag always results in exit, even when flags_help() is overriden

Open Mar-Veloz opened this issue 5 years ago • 1 comments

Hi,

I'm trying to use shflags to write a script with several different subcommands, such as (for example) git has: git pull, git push, git merge, etc. As such, I'd like the --help flag to print help for that specific subcommand instead of one monolithic help message for everything.

The way I'm attempting to achieve this is by overriding the flags_help function to do nothing unless a non-empty $1 is passed to it, and having each specific subcommand manually check for the help flag. However, the problem with this is it seems when the library detects the help flag, it executes flags_help and then exits.

An easy solution seems to me to make the exit part of the flags_help function, so that the behavior can be overriden for those who don't wish for usage of the help flag to result in an immediate exit of the flags parser.

Thanks!

Mar-Veloz avatar Apr 30 '20 01:04 Mar-Veloz

Taking a closer look at it, it seems what's happening isn't exactly that exit is being called explicitly, but rather that the return value of the flags evaluation is being set to false and the evaluation loop is being broken out of. The exit then results from the || exit $? that the flags evaluator is called with.

Perhaps it would be agreeable to not treat the help flag as an error, and remove the break that executes when --help is detected (this would trade some optimization for flexibility, but otherwise seems harmless). The flags_return variable could be set as the return value of flags_help, thereby allowing the behavior of --help being treated as an error to be overriden by overriding flags_help to return $FLAGS_TRUE.

I would be happy to submit a PR for this change, since it seems like something that is within my abilities to do.

Mar-Veloz avatar Apr 30 '20 02:04 Mar-Veloz