diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-03-13 08:10:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-13 08:10:51 -0400 |
commit | 77e2a0fab7620d22e0fde6be7374dbbf26706fd4 (patch) | |
tree | 7596897fa233204b7c7b84c94f26c71ccaf5ad5b /cmd2/cmd2.py | |
parent | 59739aa5b6f253814fb019a9e777056a6efb61ca (diff) | |
parent | a4160cfe9ab39402511c1a445f3b978099743bc9 (diff) | |
download | cmd2-git-77e2a0fab7620d22e0fde6be7374dbbf26706fd4.tar.gz |
Merge pull request #906 from python-cmd2/parsing_exception
Parsing exception
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 4ab3df3f..44e02005 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 Cmd2ArgparseError, Cmd2ShlexError, 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,10 @@ class Cmd(cmd.Cmd): stop = False try: statement = self._input_line_to_statement(line) - except EmptyStatement: + except (EmptyStatement, Cmd2ShlexError) as ex: + if isinstance(ex, Cmd2ShlexError): + 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 +1682,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 (Cmd2ArgparseError, EmptyStatement): + # Don't do anything, but do allow command finalization hooks to run pass except Exception as ex: self.pexcept(ex) @@ -1744,6 +1742,8 @@ class Cmd(cmd.Cmd): :param line: the line being parsed :return: the completed Statement + :raises: Cmd2ShlexError if a shlex error occurs (e.g. No closing quotation) + EmptyStatement when the resulting Statement is blank """ while True: try: @@ -1755,7 +1755,7 @@ class Cmd(cmd.Cmd): # it's not a multiline command, but we parsed it ok # so we are done break - except ValueError: + except Cmd2ShlexError: # we have unclosed quotation marks, lets parse only the command # and see if it's a multiline statement = self.statement_parser.parse_command_only(line) @@ -1792,7 +1792,7 @@ class Cmd(cmd.Cmd): self._at_continuation_prompt = False if not statement.command: - raise EmptyStatement() + raise EmptyStatement return statement def _input_line_to_statement(self, line: str) -> Statement: @@ -1801,6 +1801,8 @@ class Cmd(cmd.Cmd): :param line: the line being parsed :return: parsed command line as a Statement + :raises: Cmd2ShlexError if a shlex error occurs (e.g. No closing quotation) + EmptyStatement when the resulting Statement is blank """ used_macros = [] orig_line = None @@ -1819,7 +1821,7 @@ class Cmd(cmd.Cmd): used_macros.append(statement.command) line = self._resolve_macro(statement) if line is None: - raise EmptyStatement() + raise EmptyStatement else: break |