From c50db52da00f4e544a6b3a19ee5b0f54e8503914 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 7 May 2020 14:32:29 -0400 Subject: Added SkipPostcommandHooks exception and made Cmd2ArgparseError inherit from it. Both exception classes have been added to the public API. --- cmd2/exceptions.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'cmd2/exceptions.py') diff --git a/cmd2/exceptions.py b/cmd2/exceptions.py index 635192e1..d0a922db 100644 --- a/cmd2/exceptions.py +++ b/cmd2/exceptions.py @@ -1,16 +1,33 @@ # coding=utf-8 -"""Custom exceptions for cmd2. These are NOT part of the public API and are intended for internal use only.""" +"""Custom exceptions for cmd2""" -class Cmd2ArgparseError(Exception): +############################################################################################################ +# The following exceptions are part of the public API +############################################################################################################ + +class SkipPostcommandHooks(Exception): """ - Custom exception class for when a command has an error parsing its arguments. - This can be raised by argparse decorators or the command functions themselves. - The main use of this exception is to tell cmd2 not to run Postcommand hooks. + Custom exception class for when a command has a failure bad enough to skip post command + hooks, but not bad enough to print the exception to the user. """ pass +class Cmd2ArgparseError(SkipPostcommandHooks): + """ + A ``SkipPostcommandHooks`` exception for when a command fails parsing its arguments. + This is raised by argparse decorators but can also be raised by command functions. + If a command function still needs to run post command hooks when parsing fails, + just return instead of raising an exception. + """ + pass + + +############################################################################################################ +# The following exceptions are NOT part of the public API and are intended for internal use only. +############################################################################################################ + class Cmd2ShlexError(Exception): """Raised when shlex fails to parse a command line string in StatementParser""" pass -- cgit v1.2.1 From 6460d5707fffc725c50e0642b1e1e2edafa18d87 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 7 May 2020 16:29:59 -0400 Subject: Made following changes to onecmd_plus_hooks() 1. Added SystemExit handling by warning the user it's occured and setting stop to True 2. KeyboardInterrupts won't be raised if stop is already set to True. --- cmd2/exceptions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'cmd2/exceptions.py') diff --git a/cmd2/exceptions.py b/cmd2/exceptions.py index d0a922db..8a7fd81f 100644 --- a/cmd2/exceptions.py +++ b/cmd2/exceptions.py @@ -16,10 +16,10 @@ class SkipPostcommandHooks(Exception): class Cmd2ArgparseError(SkipPostcommandHooks): """ - A ``SkipPostcommandHooks`` exception for when a command fails parsing its arguments. - This is raised by argparse decorators but can also be raised by command functions. - If a command function still needs to run post command hooks when parsing fails, - just return instead of raising an exception. + A ``SkipPostcommandHooks`` exception for when a command fails to parse its arguments. + Normally argparse raises a SystemExit exception in these cases. To avoid stopping the command + loop, catch the SystemExit and raise this instead. If you still need to run post command hooks + after parsing fails, just return instead of raising an exception. """ pass -- cgit v1.2.1