Skip to content

Commit e10afbe

Browse files
authored
Merge pull request #465 from python-cmd2/onecmd_str
Allow onecmd to accept a raw string for backward compatibility with cmd
2 parents 6ddb684 + 4f90260 commit e10afbe

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
## 0.9.3 (TBD, 2018)
1+
## 0.9.3 (July TBD, 2018)
2+
* Bug Fixes
3+
* Fixed bug when StatementParser ``__init__()`` was called with ``terminators`` equal to ``None``
4+
* Fixed bug when ``Cmd.onecmd()`` was called with a raw ``str``
25

36
## 0.9.2 (June 28, 2018)
47
* Bug Fixes

cmd2/cmd2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,14 +1901,19 @@ def _func_named(self, arg: str) -> str:
19011901
result = target
19021902
return result
19031903

1904-
def onecmd(self, statement: Statement) -> Optional[bool]:
1904+
def onecmd(self, statement: Union[Statement, str]) -> Optional[bool]:
19051905
""" This executes the actual do_* method for a command.
19061906
19071907
If the command provided doesn't exist, then it executes _default() instead.
19081908
1909-
:param statement: Command - a parsed command from the input stream
1909+
:param statement: Command - intended to be a Statement instance parsed command from the input stream,
1910+
alternative acceptance of a str is present only for backward compatibility with cmd
19101911
:return: a flag indicating whether the interpretation of commands should stop
19111912
"""
1913+
# For backwards compatibility with cmd, allow a str to be passed in
1914+
if not isinstance(statement, Statement):
1915+
statement = self._complete_statement(statement)
1916+
19121917
funcname = self._func_named(statement.command)
19131918
if not funcname:
19141919
self.default(statement)

tests/test_cmd2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,3 +1787,18 @@ def test_readline_remove_history_item(base_app):
17871787
assert readline.get_current_history_length() == 1
17881788
readline.remove_history_item(0)
17891789
assert readline.get_current_history_length() == 0
1790+
1791+
def test_onecmd_raw_str_continue(base_app):
1792+
line = "help"
1793+
stop = base_app.onecmd(line)
1794+
out = base_app.stdout.buffer
1795+
assert not stop
1796+
assert out.strip() == BASE_HELP.strip()
1797+
1798+
def test_onecmd_raw_str_quit(base_app):
1799+
line = "quit"
1800+
stop = base_app.onecmd(line)
1801+
out = base_app.stdout.buffer
1802+
assert stop
1803+
assert out == ''
1804+

0 commit comments

Comments
 (0)