Skip to content

Commit 02327aa

Browse files
committed
Documented how to override the build-in command category.
1 parent 9914407 commit 02327aa

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

cmd2/cmd2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ class Cmd:
337337
# not been explicitly categorized with the @with_category decorator.
338338
# This value is inherited by subclasses but they can set their own
339339
# DEFAULT_CATEGORY to place their commands into a custom category.
340+
# Subclasses can also reassign cmd2.Cmd.DEFAULT_CATEGORY to rename
341+
# the category used for the framework's built-in commands.
340342
DEFAULT_CATEGORY: ClassVar[str] = "Cmd2 Commands"
341343

342344
# Header for table listing help topics not related to a command.

docs/features/help.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,19 @@ class Plugin(cmd2.CommandSet):
9090

9191
When using inheritance, `cmd2` uses the `DEFAULT_CATEGORY` of the class where the command was
9292
actually defined. This means built-in commands (like `help`, `history`, and `quit`) stay in the
93-
`"cmd2 Commands"` category, while your commands move to your custom category.
93+
`"Cmd2 Commands"` category, while your commands move to your custom category.
94+
95+
If you want to rename the built-in category itself, you can do so by reassigning
96+
`cmd2.Cmd.DEFAULT_CATEGORY` at the class level within your `Cmd` subclass:
97+
98+
```py
99+
class MyApp(cmd2.Cmd):
100+
# Rename the framework's built-in category
101+
cmd2.Cmd.DEFAULT_CATEGORY = 'Shell Commands'
102+
103+
# Set the category for your own commands
104+
DEFAULT_CATEGORY = 'Application Commands'
105+
```
94106

95107
For a complete demonstration of this functionality, see the
96108
[default_categories.py](https://github.com/python-cmd2/cmd2/blob/main/examples/default_categories.py)

examples/default_categories.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class CategoryApp(cmd2.Cmd):
3636
# This sets the default category for all commands defined in this class
3737
DEFAULT_CATEGORY = "Application Commands"
3838

39+
# This overrides the category for the cmd2 built-in commands
40+
cmd2.Cmd.DEFAULT_CATEGORY = "Cmd2 Shell Commands"
41+
3942
def __init__(self) -> None:
4043
super().__init__()
4144
# Register a command set to show how its categories integrate

0 commit comments

Comments
 (0)