Skip to content

Commit b3408bc

Browse files
authored
Merge pull request #783 from python-cmd2/help_fix
Help fix
2 parents 393b242 + 68bcd0a commit b3408bc

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.9.18 (TBD, 2019)
2+
* Bug Fixes
3+
* Fixed bug introduced in 0.9.17 where help functions for hidden and disabled commands were not being filtered
4+
out as help topics
5+
16
## 0.9.17 (September 23, 2019)
27
* Bug Fixes
38
* Fixed a bug when using WSL when all Windows paths have been removed from $PATH

cmd2/cmd2.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,19 +1625,8 @@ def get_all_commands(self) -> List[str]:
16251625

16261626
def get_visible_commands(self) -> List[str]:
16271627
"""Return a list of commands that have not been hidden or disabled"""
1628-
commands = self.get_all_commands()
1629-
1630-
# Remove the hidden commands
1631-
for name in self.hidden_commands:
1632-
if name in commands:
1633-
commands.remove(name)
1634-
1635-
# Remove the disabled commands
1636-
for name in self.disabled_commands:
1637-
if name in commands:
1638-
commands.remove(name)
1639-
1640-
return commands
1628+
return [command for command in self.get_all_commands()
1629+
if command not in self.hidden_commands and command not in self.disabled_commands]
16411630

16421631
def _get_alias_completion_items(self) -> List[CompletionItem]:
16431632
"""Return list of current alias names and values as CompletionItems"""
@@ -1659,9 +1648,13 @@ def _get_commands_aliases_and_macros_for_completion(self) -> List[str]:
16591648
return list(visible_commands | alias_names | macro_names)
16601649

16611650
def get_help_topics(self) -> List[str]:
1662-
""" Returns a list of help topics """
1663-
return [name[len(HELP_FUNC_PREFIX):] for name in self.get_names()
1664-
if name.startswith(HELP_FUNC_PREFIX) and callable(getattr(self, name))]
1651+
"""Return a list of help topics"""
1652+
all_topics = [name[len(HELP_FUNC_PREFIX):] for name in self.get_names()
1653+
if name.startswith(HELP_FUNC_PREFIX) and callable(getattr(self, name))]
1654+
1655+
# Filter out hidden and disabled commands
1656+
return [topic for topic in all_topics
1657+
if topic not in self.hidden_commands and topic not in self.disabled_commands]
16651658

16661659
# noinspection PyUnusedLocal
16671660
def sigint_handler(self, signum: int, frame) -> None:

tests/test_cmd2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,23 @@ def test_get_help_topics(base_app):
19811981
custom_help = base_app.get_help_topics()
19821982
assert len(custom_help) == 0
19831983

1984+
def test_get_help_topics_hidden():
1985+
# Verify get_help_topics() filters out hidden commands
1986+
class TestApp(cmd2.Cmd):
1987+
def __init__(self, *args, **kwargs):
1988+
super().__init__(*args, **kwargs)
1989+
1990+
def do_my_cmd(self, args):
1991+
pass
1992+
1993+
def help_my_cmd(self, args):
1994+
pass
1995+
1996+
app = TestApp()
1997+
assert 'my_cmd' in app.get_help_topics()
1998+
1999+
app.hidden_commands.append('my_cmd')
2000+
assert 'my_cmd' not in app.get_help_topics()
19842001

19852002
class ReplWithExitCode(cmd2.Cmd):
19862003
""" Example cmd2 application where we can specify an exit code when existing."""
@@ -2240,6 +2257,10 @@ def test_disable_and_enable_category(disable_commands_app):
22402257
assert 'has_helper_funcs' not in visible_commands
22412258
assert 'has_no_helper_funcs' not in visible_commands
22422259

2260+
# Make sure get_help_topics() filters out disabled commands
2261+
help_topics = disable_commands_app.get_help_topics()
2262+
assert 'has_helper_funcs' not in help_topics
2263+
22432264
##########################################################################
22442265
# Enable the category
22452266
##########################################################################
@@ -2281,6 +2302,10 @@ def test_disable_and_enable_category(disable_commands_app):
22812302
assert 'has_helper_funcs' in visible_commands
22822303
assert 'has_no_helper_funcs' in visible_commands
22832304

2305+
# Make sure get_help_topics() contains our help function
2306+
help_topics = disable_commands_app.get_help_topics()
2307+
assert 'has_helper_funcs' in help_topics
2308+
22842309
def test_enable_enabled_command(disable_commands_app):
22852310
# Test enabling a command that is not disabled
22862311
saved_len = len(disable_commands_app.disabled_commands)

0 commit comments

Comments
 (0)