diff options
-rw-r--r-- | cmd2/cmd2.py | 27 | ||||
-rw-r--r-- | tests/test_cmd2.py | 17 |
2 files changed, 40 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 7273286b..94b75e5f 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1858,8 +1858,30 @@ class Cmd(cmd.Cmd): pipe runs out. We can't refactor it because we need to retain backwards compatibility with the standard library version of cmd. """ - statement = self.statement_parser.parse(self.preparse(line)) - while statement.multiline_command and not statement.terminator: + # preparse() is deprecated, use self.register_postparsing_hook() instead + line = self.preparse(line) + + while True: + try: + statement = self.statement_parser.parse(line) + if statement.multiline_command and statement.terminator: + # we have a completed multiline command, we are done + break + if not statement.multiline_command: + # it's not a multiline command, but we parsed it ok + # so we are done + break + except ValueError: + # 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) + if not statement.multiline_command: + # not a multiline command, so raise the exception + raise + + # if we get here we must have: + # - a multiline command with no terminator + # - a multiline command with unclosed quotation marks if not self.quit_on_sigint: try: newline = self.pseudo_raw_input(self.continuation_prompt) @@ -1885,7 +1907,6 @@ class Cmd(cmd.Cmd): newline = '\n' self.poutput(newline) line = '{}\n{}'.format(statement.raw, newline) - statement = self.statement_parser.parse(line) if not statement.command: raise EmptyStatement() diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 3324a105..0ec993e9 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1471,7 +1471,8 @@ def test_multiline_complete_empty_statement_raises_exception(multiline_app): multiline_app._complete_statement('') def test_multiline_complete_statement_without_terminator(multiline_app): - # Mock out the input call so we don't actually wait for a user's response on stdin when it looks for more input + # Mock out the input call so we don't actually wait for a user's response + # on stdin when it looks for more input m = mock.MagicMock(name='input', return_value='\n') builtins.input = m @@ -1481,6 +1482,20 @@ def test_multiline_complete_statement_without_terminator(multiline_app): statement = multiline_app._complete_statement(line) assert statement == args assert statement.command == command + assert statement.multiline_command == command + +def test_multiline_complete_statement_with_unclosed_quotes(multiline_app): + # Mock out the input call so we don't actually wait for a user's response + # on stdin when it looks for more input + m = mock.MagicMock(name='input', side_effect=['quotes', '" now closed;']) + builtins.input = m + + line = 'orate hi "partially open' + statement = multiline_app._complete_statement(line) + assert statement == 'hi "partially open\nquotes\n" now closed' + assert statement.command == 'orate' + assert statement.multiline_command == 'orate' + assert statement.terminator == ';' def test_clipboard_failure(base_app, capsys): |