Skip to content

Commit 4b033ec

Browse files
committed
Fixed issue where default help category wasn't displaying in help
output when there were categorized commands.
1 parent aa24538 commit 4b033ec

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

cmd2/cmd2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,8 @@ def __init__(
492492
# Set header for table listing commands that have no help info.
493493
self.undoc_header = "Undocumented Commands"
494494

495-
# If any command has been categorized, then all other commands that haven't been categorized
496-
# will display under this section in the help output.
495+
# If any command has been categorized, then all other documented commands that
496+
# haven't been categorized will display under this section in the help output.
497497
self.default_category = "Uncategorized Commands"
498498

499499
# The error that prints when no help information can be found
@@ -4072,10 +4072,13 @@ def do_help(self, args: argparse.Namespace) -> None:
40724072
self.poutput(Text(self.doc_leader, style=Cmd2Style.HELP_LEADER))
40734073
self.poutput()
40744074

4075-
# Print any categories first and then the default category.
4075+
# Print any categories first and then the remaining documented commands.
40764076
sorted_categories = sorted(cmds_cats.keys(), key=self.default_sort_key)
40774077
all_cmds = {category: cmds_cats[category] for category in sorted_categories}
4078-
all_cmds[self.doc_header] = cmds_doc
4078+
if all_cmds:
4079+
all_cmds[self.default_category] = cmds_doc
4080+
else:
4081+
all_cmds[self.doc_header] = cmds_doc
40794082

40804083
# Used to provide verbose table separation for better readability.
40814084
previous_table_printed = False

tests/test_cmd2.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,9 @@ def test_columnize(capsys: pytest.CaptureFixture[str]) -> None:
13651365
class HelpCategoriesApp(cmd2.Cmd):
13661366
"""Class for testing custom help_* methods which override docstring help."""
13671367

1368+
SOME_CATEGORY = "Some Category"
1369+
CUSTOM_CATEGORY = "Custom Category"
1370+
13681371
def __init__(self, *args, **kwargs) -> None:
13691372
super().__init__(*args, **kwargs)
13701373

@@ -1373,10 +1376,11 @@ def do_diddly(self, arg) -> None:
13731376
"""This command does diddly"""
13741377

13751378
# This command will be in the "Some Category" section of the help menu even though it has no docstring
1376-
@cmd2.with_category("Some Category")
1379+
@cmd2.with_category(SOME_CATEGORY)
13771380
def do_cat_nodoc(self, arg) -> None:
13781381
pass
13791382

1383+
# This command will show in the category labeled with self.default_category
13801384
def do_squat(self, arg) -> None:
13811385
"""This docstring help will never be shown because the help_squat method overrides it."""
13821386

@@ -1386,7 +1390,7 @@ def help_squat(self) -> None:
13861390
def do_edit(self, arg) -> None:
13871391
"""This overrides the edit command and does nothing."""
13881392

1389-
cmd2.categorize((do_squat, do_edit), 'Custom Category')
1393+
cmd2.categorize((do_squat, do_edit), CUSTOM_CATEGORY)
13901394

13911395
# This command will be in the "undocumented" section of the help menu
13921396
def do_undoc(self, arg) -> None:
@@ -1403,12 +1407,23 @@ def test_help_cat_base(helpcat_app) -> None:
14031407
assert helpcat_app.last_result is True
14041408
verify_help_text(helpcat_app, out)
14051409

1410+
help_text = ''.join(out)
1411+
assert helpcat_app.CUSTOM_CATEGORY in help_text
1412+
assert helpcat_app.SOME_CATEGORY in help_text
1413+
assert helpcat_app.default_category in help_text
1414+
1415+
14061416

14071417
def test_help_cat_verbose(helpcat_app) -> None:
14081418
out, err = run_cmd(helpcat_app, 'help --verbose')
14091419
assert helpcat_app.last_result is True
14101420
verify_help_text(helpcat_app, out)
14111421

1422+
help_text = ''.join(out)
1423+
assert helpcat_app.CUSTOM_CATEGORY in help_text
1424+
assert helpcat_app.SOME_CATEGORY in help_text
1425+
assert helpcat_app.default_category in help_text
1426+
14121427

14131428
class SelectApp(cmd2.Cmd):
14141429
def do_eat(self, arg) -> None:

0 commit comments

Comments
 (0)