devnote-dev / cling

A modular, non-macro-based command line interface library
Mozilla Public License 2.0
23 stars 2 forks source link

Exiting from `pre_run` hooks #13

Open devnote-dev opened 9 months ago

devnote-dev commented 9 months ago

For pre_run hook methods, there is the option to stop command execution to the main run method if the value returned is false. However, there are no checks in place to determine the actual exit code for this, the Executor simply returns which results in a 0 exit code:

https://github.com/devnote-dev/cling/blob/279db9a67b3724821842d9b4870050a05269a347/src/cling/executor.cr#L52-L54

This is obviously not ideal and even misleading for people designing applications that rely on the exit code for debugging or informational purposes. There are already ways to get around this, for example, raising a specific exception that gets funneled to the on_error hook method which then terminates the program with the appropriate exit code. But I think there should be a better way of doing this, even if it's just a shorthand method built into the Command class.

devnote-dev commented 8 months ago

A workaround has been implemented in https://github.com/devnote-dev/cling/commit/86caeefbafddb4e4fa7f83382117a53444aa0c0a and https://github.com/devnote-dev/cling/commit/40e3fe25a05b636bb730c2ab49ce8da75e3f68fc that makes use of exception funneling as previously mentioned, meaning that the on_error hook method has been changed to accommodate for it.

While this solution works, I'm contemplating the need for such verbosity/control: if the pre_run hook method were bound to the same rules as the run and post_run hook methods—that is, not having special functionality based on the return type—then it eliminates the need for all of this and thus resolves the initial issue. 🤔