From 6c90d3c617e2e3106e6f5419f301620f67f6ec71 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Tue, 10 Jul 2018 22:50:44 -0400 Subject: [PATCH 1/3] Allow onecmd to accept a raw string for backward compatibility with cmd This addresses #464 --- cmd2/cmd2.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 42e00c397..44f3a0685 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1901,14 +1901,19 @@ def _func_named(self, arg: str) -> str: result = target return result - def onecmd(self, statement: Statement) -> Optional[bool]: + def onecmd(self, statement: Union[Statement, str]) -> Optional[bool]: """ This executes the actual do_* method for a command. If the command provided doesn't exist, then it executes _default() instead. - :param statement: Command - a parsed command from the input stream + :param statement: Command - intended to be a Statement instance parsed command from the input stream, + alternative acceptance of a str is present only for backward compatibility with cmd :return: a flag indicating whether the interpretation of commands should stop """ + # For backwards compatibility with cmd, allow a str to be passed in + if not isinstance(statement, Statement): + statement = self._complete_statement(statement) + funcname = self._func_named(statement.command) if not funcname: self.default(statement) From 0419f2584d2c162746549daf29f0b1677dd4d8d1 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Tue, 10 Jul 2018 23:06:22 -0400 Subject: [PATCH 2/3] Added a couple simple unit tests for the case where onecmd accepts a raw string --- tests/test_cmd2.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 77dcc8750..b973fdf59 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1787,3 +1787,18 @@ def test_readline_remove_history_item(base_app): assert readline.get_current_history_length() == 1 readline.remove_history_item(0) assert readline.get_current_history_length() == 0 + +def test_onecmd_raw_str_continue(base_app): + line = "help" + stop = base_app.onecmd(line) + out = base_app.stdout.buffer + assert not stop + assert out.strip() == BASE_HELP.strip() + +def test_onecmd_raw_str_quit(base_app): + line = "quit" + stop = base_app.onecmd(line) + out = base_app.stdout.buffer + assert stop + assert out == '' + From 4f90260edb21d6134338e43f5465b54b261db2c5 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Wed, 11 Jul 2018 09:22:13 -0400 Subject: [PATCH 3/3] Updated CHANGELOG --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e22ee790..764d00617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ -## 0.9.3 (TBD, 2018) +## 0.9.3 (July TBD, 2018) +* Bug Fixes + * Fixed bug when StatementParser ``__init__()`` was called with ``terminators`` equal to ``None`` + * Fixed bug when ``Cmd.onecmd()`` was called with a raw ``str`` ## 0.9.2 (June 28, 2018) * Bug Fixes