Lucretiel / autocommand

Autocommand turns a python function into a CLI program
GNU Lesser General Public License v3.0
52 stars 9 forks source link

Add KeyboardInterrupt handling #25

Closed jaraco closed 1 year ago

jaraco commented 1 year ago

I've started work on a patch for #18. In this PR, the automain accepts a new optional parameter on_interrupt, which can be ignore (the default and current behavior), suppress (suppress the exception and exit 0), or quiet (exit 1 without a traceback).

I ran into a bit of difficulty when trying to wire it into automain and especially when trying to wire that through autocommand. The current approach allows autocommand to accept a new automain parameter, overriding the default automain, such as with autocommand(automain=functools.partial(autocommand.automain, on_interrupt='quiet')). I don't love that interface.

And now that I think about it more, I'm not sure having this functionality integrated with automain makes any sense at all, and maybe there should just be a new, independent decorator for interrupt handling.

Still, I'd like to submit this for your consideration - are there any behaviors from this approach that you'd like to see in the final approach?

Lucretiel commented 1 year ago

I like your 3 options. I agree with you that this would be outside of automain, which is primarily about handling __name__ == "__main__". My approach would be to add a 4th decorator to the stack of nested decorators that are assembled by autocommand: https://github.com/Lucretiel/autocommand/blob/9d95de8ad227a76604a9205b5b211bc2a0576e91/src/autocommand/autocommand.py#L40-L66