Skip to content

Commit 4a2e041

Browse files
committed
Prevent postcmd_hook from running when argparse fails
1 parent 59739aa commit 4a2e041

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

cmd2/cmd2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from .argparse_custom import CompletionItem, DEFAULT_ARGUMENT_PARSER
5151
from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer
5252
from .decorators import with_argparser
53-
from .exceptions import EmbeddedConsoleExit, EmptyStatement
53+
from .exceptions import CmdLineError, EmbeddedConsoleExit, EmptyStatement
5454
from .history import History, HistoryItem
5555
from .parsing import StatementParser, Statement, Macro, MacroArg, shlex_split
5656
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 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge
15991599
stop = False
16001600
try:
16011601
statement = self._input_line_to_statement(line)
1602-
except EmptyStatement:
1602+
except (EmptyStatement, ValueError) as ex:
1603+
if isinstance(ex, ValueError):
1604+
# Since shlex.split() failed on syntax, let user know what's going on
1605+
self.perror("Invalid syntax: {}".format(ex))
16031606
return self._run_cmdfinalization_hooks(stop, None)
1604-
except ValueError as ex:
1605-
# If shlex.split failed on syntax, let user know what's going on
1606-
self.pexcept("Invalid syntax: {}".format(ex))
1607-
return stop
16081607

16091608
# now that we have a statement, run it with all the hooks
16101609
try:
@@ -1684,8 +1683,8 @@ def onecmd_plus_hooks(self, line: str, *, add_to_history: bool = True, py_bridge
16841683
# Stop saving command's stdout before command finalization hooks run
16851684
self.stdout.pause_storage = True
16861685

1687-
except EmptyStatement:
1688-
# don't do anything, but do allow command finalization hooks to run
1686+
except (CmdLineError, EmptyStatement):
1687+
# Don't do anything, but do allow command finalization hooks to run
16891688
pass
16901689
except Exception as ex:
16911690
self.pexcept(ex)

cmd2/decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Callable, List, Optional, Union
55

66
from . import constants
7+
from .exceptions import CmdLineError
78
from .parsing import Statement
89

910

@@ -144,7 +145,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):
144145
try:
145146
args, unknown = parser.parse_known_args(parsed_arglist, namespace)
146147
except SystemExit:
147-
return
148+
raise CmdLineError
148149
else:
149150
setattr(args, '__statement__', statement)
150151
return func(cmd2_app, args, unknown)
@@ -216,7 +217,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):
216217
try:
217218
args = parser.parse_args(parsed_arglist, namespace)
218219
except SystemExit:
219-
return
220+
raise CmdLineError
220221
else:
221222
setattr(args, '__statement__', statement)
222223
return func(cmd2_app, args)

cmd2/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"""Custom exceptions for cmd2. These are NOT part of the public API and are intended for internal use only."""
33

44

5+
class CmdLineError(Exception):
6+
"""Custom class for when an error occurred parsing the command line"""
7+
pass
8+
9+
510
class EmbeddedConsoleExit(SystemExit):
611
"""Custom exception class for use with the py command."""
712
pass

0 commit comments

Comments
 (0)