Add helpful decorator ctrlc_return1.
This commit is contained in:
parent
10a447cd07
commit
946f329e8c
1 changed files with 17 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue