From 0be9e03c6251b78825823010f88e8a8a6d652e6b Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:15:23 -0500 Subject: [PATCH 1/2] Make a command's help category public as Cmd.get_command_category() --- CHANGELOG.md | 6 +++++ cmd2/cmd2.py | 26 +++++++++++++------- docs/features/help.md | 4 ++++ tests/test_categories.py | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 212bcb8a3..1bf162110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 4.5.0 (TBD) + +- Enhancements + - Added `Cmd.get_command_category()`, which returns the help category a command is listed under. + It was the private `_get_command_category()`. + ## 4.4.5 (TBD) - Bug Fixes diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 653942444..b5b042922 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -1041,7 +1041,7 @@ def register_command_set(self, cmdset: CommandSet[Any]) -> None: self._cmd_to_command_sets[command] = cmdset # If this command is in a disabled category, then disable it - command_category = self._get_command_category(command_method) + command_category = self.get_command_category(command_method) if command_category in self.disabled_categories: message_to_print = self.disabled_categories[command_category] self.disable_command(command, message_to_print) @@ -3643,11 +3643,21 @@ def get_command_func(self, command: str) -> BoundCommandFunc[...] | None: command_func = getattr(self, command_func_name, None) return cast(BoundCommandFunc[...], command_func) if callable(command_func) else None - def _get_command_category(self, func: BoundCommandFunc[...]) -> str: - """Determine the category for a command. + def get_command_category(self, func: BoundCommandFunc[...]) -> str: + """Return the help category of a command. - :param func: the do_* function implementing the command - :return: category name + This is the heading which ``help`` lists the command under. It is, in this order: + + 1. the category given to the command with [@with_category][cmd2.with_category] or + [categorize()][cmd2.categorize] + 2. the ``DEFAULT_CATEGORY`` of the class which defines the command, which may be a + [CommandSet][cmd2.CommandSet] or a class your application inherits from + 3. this application's own ``DEFAULT_CATEGORY``, when the defining class has none, as with a + plain mixin + + :param func: the bound ``do_*`` method of the command, as returned by + [get_command_func()][cmd2.Cmd.get_command_func] + :return: the name of the category """ # Check if the command function has a category. if hasattr(func, constants.COMMAND_ATTR_HELP_CATEGORY): @@ -4610,7 +4620,7 @@ def _build_command_info(self) -> tuple[dict[str, list[str]], list[str]]: # Store the command within its category command_func = self.get_command_func(command) if command_func is not None: - category = self._get_command_category(command_func) + category = self.get_command_category(command_func) cmds_cats.setdefault(category, []).append(command) return cmds_cats, help_topics @@ -6251,7 +6261,7 @@ def enable_category(self, category: str) -> None: for command in list(self.disabled_commands): command_func = self.disabled_commands[command].command_func - if self._get_command_category(command_func) == category: + if self.get_command_category(command_func) == category: self.enable_command(command) del self.disabled_categories[category] @@ -6332,7 +6342,7 @@ def disable_category(self, category: str, message_to_print: str) -> None: for command in all_commands: command_func = self.get_command_func(command) - if command_func is not None and self._get_command_category(command_func) == category: + if command_func is not None and self.get_command_category(command_func) == category: self.disable_command(command, message_to_print) self.disabled_categories[category] = message_to_print diff --git a/docs/features/help.md b/docs/features/help.md index 8dee6fba5..d0f413b4e 100644 --- a/docs/features/help.md +++ b/docs/features/help.md @@ -159,6 +159,10 @@ def do_findleakers(self, _): categorize((do_undeploy, do_stop, do_findleakers), CMD_CAT_APP_MGMT) ``` +To find out which category a command is listed under, call +[get_command_category()][cmd2.Cmd.get_command_category] as `self.get_command_category(func)`. The +`func` argument comes from `self.get_command_func("name")`. + The `help` command also has a verbose option (`help -v` or `help --verbose`) that combines the help categories with per-command help messages: diff --git a/tests/test_categories.py b/tests/test_categories.py index eaf05641b..4bfdc12e1 100644 --- a/tests/test_categories.py +++ b/tests/test_categories.py @@ -128,3 +128,54 @@ def test_category_command_set() -> None: app.register_command_set(CategoryCommandSet()) cmds_cats, _help_topics = app._build_command_info() assert "cmdset_command" in cmds_cats[CategoryCommandSet.DEFAULT_CATEGORY] + + +class _PlainMixin: + """Defines a command and has no DEFAULT_CATEGORY of its own.""" + + def do_from_mixin(self, _: cmd2.Statement) -> None: + """A command from a plain mixin.""" + + +class _CategorySet(cmd2.CommandSet): + DEFAULT_CATEGORY = "Set Commands" + + def do_from_set(self, _: cmd2.Statement) -> None: + """A command from a command set.""" + + +class _CategoryApp(_PlainMixin, cmd2.Cmd): + DEFAULT_CATEGORY = "App Commands" + + @cmd2.with_category("Chosen") + def do_chosen(self, _: cmd2.Statement) -> None: + """A command with a category of its own.""" + + def do_own(self, _: cmd2.Statement) -> None: + """A command of the application's class.""" + + +def _category(app: cmd2.Cmd, command: str) -> str: + func = app.get_command_func(command) + assert func is not None + return app.get_command_category(func) + + +def test_get_command_category_given_by_the_command() -> None: + assert _category(_CategoryApp(), "chosen") == "Chosen" + + +def test_get_command_category_of_the_defining_class() -> None: + app = _CategoryApp(command_sets=[_CategorySet()]) + assert _category(app, "own") == "App Commands" + assert _category(app, "help") == "Cmd2 Commands" + assert _category(app, "from_set") == "Set Commands" + + +def test_get_command_category_falls_back_to_the_application() -> None: + # _PlainMixin has no DEFAULT_CATEGORY, so the application's class decides. + assert _category(_CategoryApp(), "from_mixin") == "App Commands" + + +def test_the_private_name_is_gone() -> None: + assert not hasattr(cmd2.Cmd, "_get_command_category") From 95b62be6049d2a7fb640bdbaea2f41fae83721d0 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt <2812720+tleonhardt@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:05:25 -0500 Subject: [PATCH 2/2] Show finding a command's category from the visible commands, since a disabled command's function is its replacement --- cmd2/cmd2.py | 4 +++- docs/features/help.md | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index b5b042922..89fbe315d 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3656,7 +3656,9 @@ def get_command_category(self, func: BoundCommandFunc[...]) -> str: plain mixin :param func: the bound ``do_*`` method of the command, as returned by - [get_command_func()][cmd2.Cmd.get_command_func] + [get_command_func()][cmd2.Cmd.get_command_func] for a command which is not + disabled. While a command is disabled, ``get_command_func()`` returns the + function which reports that, not the command's own. :return: the name of the category """ # Check if the command function has a category. diff --git a/docs/features/help.md b/docs/features/help.md index d0f413b4e..983d93b78 100644 --- a/docs/features/help.md +++ b/docs/features/help.md @@ -159,9 +159,20 @@ def do_findleakers(self, _): categorize((do_undeploy, do_stop, do_findleakers), CMD_CAT_APP_MGMT) ``` -To find out which category a command is listed under, call -[get_command_category()][cmd2.Cmd.get_command_category] as `self.get_command_category(func)`. The -`func` argument comes from `self.get_command_func("name")`. +To find out which category a command is listed under, pass its function to +[get_command_category()][cmd2.Cmd.get_command_category]. Take the command's name from +[get_visible_commands()][cmd2.Cmd.get_visible_commands], as `help` does. Get its function from +[get_command_func()][cmd2.Cmd.get_command_func], and skip a name for which there is none: + +```py +for name in self.get_visible_commands(): + func = self.get_command_func(name) + if func is not None: + category = self.get_command_category(func) +``` + +A disabled command is not among the visible ones. While it is disabled, `get_command_func()` returns +the function which reports that, so the category found from it may not be the command's own. The `help` command also has a verbose option (`help -v` or `help --verbose`) that combines the help categories with per-command help messages: