Custom cmd2 exceptions

class cmd2.exceptions.SkipPostcommandHooks

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.

class cmd2.exceptions.Cmd2ArgparseError

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.

class cmd2.exceptions.CommandSetRegistrationError

Exception that can be thrown when an error occurs while a CommandSet is being added or removed from a cmd2 application.

class cmd2.exceptions.CompletionError(*args, apply_style: bool = True)

Raised during tab completion operations to report any sort of error you want printed. This can also be used just to display a message, even if it’s not an error. For instance, ArgparseCompleter raises CompletionErrors to display tab completion hints and sets apply_style to False so hints aren’t colored like error text.

Example use cases

  • Reading a database to retrieve a tab completion data set failed
  • A previous command line argument that determines the data set being completed is invalid
  • Tab completion hints
class cmd2.exceptions.PassThroughException(*args, wrapped_ex: BaseException)

Normally all unhandled exceptions raised during commands get printed to the user. This class is used to wrap an exception that should be raised instead of printed.