Skip to content

cmd2.exceptions

cmd2.exceptions

Custom exceptions for cmd2

SkipPostcommandHooks

Bases: Exception

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.

Cmd2ArgparseError

Bases: SkipPostcommandHooks

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.

CommandSetRegistrationError

Bases: Exception

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

CompletionError

CompletionError(*args, apply_style=True)

Bases: Exception

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

Initializer for CompletionError

PARAMETER DESCRIPTION
apply_style

If True, then ansi.style_error will be applied to the message text when printed. Set to False in cases where the message text already has the desired style. Defaults to True.

TYPE: bool DEFAULT: True

Source code in cmd2/exceptions.py
def __init__(self, *args: Any, apply_style: bool = True) -> None:
    """
    Initializer for CompletionError
    :param apply_style: If True, then ansi.style_error will be applied to the message text when printed.
                        Set to False in cases where the message text already has the desired style.
                        Defaults to True.
    """
    self.apply_style = apply_style

    super().__init__(*args)

apply_style instance-attribute

apply_style = apply_style

PassThroughException

PassThroughException(*args, wrapped_ex)

Bases: Exception

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.

Initializer for PassThroughException

PARAMETER DESCRIPTION
wrapped_ex

the exception that will be raised

TYPE: BaseException

Source code in cmd2/exceptions.py
def __init__(self, *args: Any, wrapped_ex: BaseException) -> None:
    """
    Initializer for PassThroughException
    :param wrapped_ex: the exception that will be raised
    """
    self.wrapped_ex = wrapped_ex
    super().__init__(*args)

wrapped_ex instance-attribute

wrapped_ex = wrapped_ex

Cmd2ShlexError

Bases: Exception

Raised when shlex fails to parse a command line string in StatementParser

EmbeddedConsoleExit

Bases: SystemExit

Custom exception class for use with the py command.

EmptyStatement

Bases: Exception

Custom exception class for handling behavior when the user just presses .

RedirectionError

Bases: Exception

Custom exception class for when redirecting or piping output fails