diff options
-rw-r--r-- | cmd2/cmd2.py | 15 | ||||
-rw-r--r-- | cmd2/decorators.py | 5 | ||||
-rw-r--r-- | cmd2/exceptions.py | 5 |
3 files changed, 15 insertions, 10 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 4ab3df3f..acb0797a 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -50,7 +50,7 @@ from . import utils from .argparse_custom import CompletionItem, DEFAULT_ARGUMENT_PARSER from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer from .decorators import with_argparser -from .exceptions import EmbeddedConsoleExit, EmptyStatement +from .exceptions import CmdLineError, EmbeddedConsoleExit, EmptyStatement from .history import History, HistoryItem from .parsing import StatementParser, Statement, Macro, MacroArg, shlex_split from .rl_utils import rl_type, RlType, rl_get_point, rl_set_prompt, vt100_support, rl_make_safe_prompt, rl_warning @@ -1599,12 +1599,11 @@ class Cmd(cmd.Cmd): stop = False try: statement = self._input_line_to_statement(line) - except EmptyStatement: + except (EmptyStatement, ValueError) as ex: + if isinstance(ex, ValueError): + # Since shlex.split() failed on syntax, let user know what's going on + self.perror("Invalid syntax: {}".format(ex)) return self._run_cmdfinalization_hooks(stop, None) - except ValueError as ex: - # If shlex.split failed on syntax, let user know what's going on - self.pexcept("Invalid syntax: {}".format(ex)) - return stop # now that we have a statement, run it with all the hooks try: @@ -1684,8 +1683,8 @@ class Cmd(cmd.Cmd): # Stop saving command's stdout before command finalization hooks run self.stdout.pause_storage = True - except EmptyStatement: - # don't do anything, but do allow command finalization hooks to run + except (CmdLineError, EmptyStatement): + # Don't do anything, but do allow command finalization hooks to run pass except Exception as ex: self.pexcept(ex) diff --git a/cmd2/decorators.py b/cmd2/decorators.py index 2c78134c..0e89fc7b 100644 --- a/cmd2/decorators.py +++ b/cmd2/decorators.py @@ -4,6 +4,7 @@ import argparse from typing import Callable, List, Optional, Union from . import constants +from .exceptions import CmdLineError from .parsing import Statement @@ -144,7 +145,7 @@ def with_argparser_and_unknown_args(parser: argparse.ArgumentParser, *, try: args, unknown = parser.parse_known_args(parsed_arglist, namespace) except SystemExit: - return + raise CmdLineError else: setattr(args, '__statement__', statement) return func(cmd2_app, args, unknown) @@ -216,7 +217,7 @@ def with_argparser(parser: argparse.ArgumentParser, *, try: args = parser.parse_args(parsed_arglist, namespace) except SystemExit: - return + raise CmdLineError else: setattr(args, '__statement__', statement) return func(cmd2_app, args) diff --git a/cmd2/exceptions.py b/cmd2/exceptions.py index 747e2368..1c00794f 100644 --- a/cmd2/exceptions.py +++ b/cmd2/exceptions.py @@ -2,6 +2,11 @@ """Custom exceptions for cmd2. These are NOT part of the public API and are intended for internal use only.""" +class CmdLineError(Exception): + """Custom class for when an error occurred parsing the command line""" + pass + + class EmbeddedConsoleExit(SystemExit): """Custom exception class for use with the py command.""" pass |