Skip to content

Commit e017028

Browse files
authored
Merge pull request #755 from python-cmd2/verify_command_names
Verifying command names in __init__
2 parents cc3d5cd + 933e328 commit e017028

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
448448
multiline_commands=multiline_commands,
449449
shortcuts=shortcuts)
450450

451+
# Verify commands don't have invalid names (like starting with a shortcut)
452+
for cur_cmd in self.get_all_commands():
453+
valid, errmsg = self.statement_parser.is_valid_command(cur_cmd)
454+
if not valid:
455+
raise ValueError("Invalid command name {!r}: {}".format(cur_cmd, errmsg))
456+
451457
# Stores results from the last command run to enable usage of results in a Python script or interactive console
452458
# Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
453459
self.last_result = None

docs/features/shortcuts_aliases_macros.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ To define more shortcuts, update the dict ``App.shortcuts`` with the
3838
updating the ``shortcuts`` attribute This warning applies in general to many
3939
other attributes which are not settable at runtime.
4040

41+
Note: Command, alias, and macro names cannot start with a shortcut
4142

4243
Aliases
4344
-------

tests/test_cmd2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def test_base_shortcuts(base_app):
8787
expected = normalize(SHORTCUTS_TXT)
8888
assert out == expected
8989

90+
def test_command_starts_with_shortcut():
91+
with pytest.raises(ValueError) as excinfo:
92+
app = cmd2.Cmd(shortcuts={'help': 'fake'})
93+
assert "Invalid command name 'help'" in str(excinfo.value)
9094

9195
def test_base_show(base_app):
9296
# force editor to be 'vim' so test is repeatable across platforms

0 commit comments

Comments
 (0)