diff --git a/CHANGELOG.md b/CHANGELOG.md index a2a556b69..d6f878940 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 (2026-12-02) - Bug Fixes diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index eed8ed117..ee2d16eff 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) @@ -3647,11 +3647,23 @@ 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] 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. if hasattr(func, constants.COMMAND_ATTR_HELP_CATEGORY): @@ -4614,7 +4626,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 @@ -6255,7 +6267,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] @@ -6336,7 +6348,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..983d93b78 100644 --- a/docs/features/help.md +++ b/docs/features/help.md @@ -159,6 +159,21 @@ 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, 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: 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")