From 946f329e8c43e1883e951fdad152b2aa6221352c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 30 Nov 2020 22:01:37 -0800 Subject: [PATCH] Add helpful decorator ctrlc_return1. --- voussoirkit/pipeable.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/voussoirkit/pipeable.py b/voussoirkit/pipeable.py index 4d5d851..399a406 100644 --- a/voussoirkit/pipeable.py +++ b/voussoirkit/pipeable.py @@ -18,6 +18,23 @@ class PipeableException(Exception): class NoArguments(PipeableException): pass +def ctrlc_return1(function): + ''' + Apply this decorator to your argparse gateways, and if the user presses + ctrl+c then the gateway will return 1 as its status code without the + stacktrace appearing. + + This helps me avoid wrapping the entire function in a try-except block. + + Don't use this if you need to perform some other kind of cleanup on ctrl+c. + ''' + def wrapped(*args, **kwargs): + try: + function(*args, **kwargs) + except KeyboardInterrupt: + return 1 + return wrapped + def multi_line_input(prompt=None): ''' Yield multiple lines of input from the user, until they submit EOF.