summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-03-05 13:45:11 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-03-05 13:45:11 -0500
commit4a2e0411da5c2626d93c9349757a341124eda299 (patch)
treecee8c7be31a1753d3a0a9e0e6f6c6f2abf00df31
parent59739aa5b6f253814fb019a9e777056a6efb61ca (diff)
downloadcmd2-git-4a2e0411da5c2626d93c9349757a341124eda299.tar.gz
Prevent postcmd_hook from running when argparse fails
-rw-r--r--cmd2/cmd2.py15
-rw-r--r--cmd2/decorators.py5
-rw-r--r--cmd2/exceptions.py5
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