Skip to content

Commit ebc28dc

Browse files
committed
Improved comments in constants.py.
1 parent 5645783 commit ebc28dc

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

cmd2/cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
Completions,
124124
)
125125
from .constants import (
126-
CLASS_ATTR_DEFAULT_HELP_CATEGORY,
126+
CMDSET_ATTR_DEFAULT_HELP_CATEGORY,
127127
COMMAND_FUNC_PREFIX,
128128
COMPLETER_FUNC_PREFIX,
129129
HELP_FUNC_PREFIX,
@@ -840,7 +840,7 @@ def register_command_set(self, cmdset: CommandSet) -> None:
840840
),
841841
)
842842

843-
default_category = getattr(cmdset, CLASS_ATTR_DEFAULT_HELP_CATEGORY, None)
843+
default_category = getattr(cmdset, CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None)
844844

845845
installed_attributes = []
846846
try:

cmd2/command_definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
)
1212

1313
from .constants import (
14-
CLASS_ATTR_DEFAULT_HELP_CATEGORY,
14+
CMDSET_ATTR_DEFAULT_HELP_CATEGORY,
1515
COMMAND_FUNC_PREFIX,
1616
)
1717
from .exceptions import CommandSetRegistrationError
@@ -46,7 +46,7 @@ def with_default_category(category: str, *, heritable: bool = True) -> Callable[
4646

4747
def decorate_class(cls: CommandSetType) -> CommandSetType:
4848
if heritable:
49-
setattr(cls, CLASS_ATTR_DEFAULT_HELP_CATEGORY, category)
49+
setattr(cls, CMDSET_ATTR_DEFAULT_HELP_CATEGORY, category)
5050

5151
import inspect
5252

cmd2/constants.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,47 @@ def cmd2_public_attr_name(name: str) -> str:
5757

5858

5959
##################################################################################################
60-
# Private cmd2-specific attributes for internal use
60+
# Attribute Injection Constants
61+
#
62+
# cmd2 attaches custom attributes to various objects (functions, classes, and parsers) to
63+
# track metadata and manage command state.
64+
#
65+
# Private attributes (_cmd2_ prefix) are for internal framework logic.
66+
# Public attributes (cmd2_ prefix) are available for developer use, typically within
67+
# argparse Namespaces.
6168
##################################################################################################
62-
CMD_ATTR_HELP_CATEGORY = cmd2_private_attr_name('help_category')
63-
CLASS_ATTR_DEFAULT_HELP_CATEGORY = cmd2_private_attr_name('default_help_category')
6469

65-
# The parser for a command
70+
# --- Private Internal Attributes ---
71+
72+
# Attached to a command function; defines its argument parser
6673
CMD_ATTR_ARGPARSER = cmd2_private_attr_name('argparser')
6774

68-
# Whether or not tokens are unquoted before sending to argparse
75+
# Attached to a command function; defines its help section category
76+
CMD_ATTR_HELP_CATEGORY = cmd2_private_attr_name('help_category')
77+
78+
# Attached to a command function; defines whether tokens are unquoted before reaching argparse
6979
CMD_ATTR_PRESERVE_QUOTES = cmd2_private_attr_name('preserve_quotes')
7080

71-
# Subcommand attributes for the base command name and the subcommand name
81+
# Attached to a CommandSet class; defines a default help category for its member functions
82+
CMDSET_ATTR_DEFAULT_HELP_CATEGORY = cmd2_private_attr_name('default_help_category')
83+
84+
# Attached to a subcommand function; defines the full command path to the parent (e.g., "foo" or "foo bar")
7285
SUBCMD_ATTR_COMMAND = cmd2_private_attr_name('parent_command')
86+
87+
# Attached to a subcommand function; defines the name of this specific subcommand (e.g., "bar")
7388
SUBCMD_ATTR_NAME = cmd2_private_attr_name('subcommand_name')
89+
90+
# Attached to a subcommand function; specifies kwargs passed to add_parser()
7491
SUBCMD_ATTR_ADD_PARSER_KWARGS = cmd2_private_attr_name('subcommand_add_parser_kwargs')
7592

76-
# Attribute added to a parser which uniquely identifies its command set instance
93+
# Attached to an argparse parser; identifies the CommandSet instance it belongs to
7794
PARSER_ATTR_COMMANDSET_ID = cmd2_private_attr_name('command_set_id')
7895

79-
##################################################################################################
80-
# Public cmd2-specific attributes for use by developers
81-
##################################################################################################
8296

83-
# Namespace attribute: Statement object that was created when parsing the command line
97+
# --- Public Developer Attributes ---
98+
99+
# Attached to an argparse Namespace; contains the Statement object created during parsing
84100
NS_ATTR_STATEMENT = cmd2_public_attr_name('statement')
85101

86-
# Namespace attribute: subcommand handler function or None if one was not set
102+
# Attached to an argparse Namespace; the function to handle the subcommand (or None)
87103
NS_ATTR_SUBCMD_HANDLER = cmd2_public_attr_name('subcmd_handler')

tests/test_categories.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,31 @@ def test_heritable_categories() -> None:
7979
app = ExampleApp()
8080

8181
base_cs = MyBaseCommandSet(0)
82-
assert getattr(base_cs, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
82+
assert getattr(base_cs, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
8383

8484
child1 = ChildInheritsParentCategories(1)
85-
assert getattr(child1, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
85+
assert getattr(child1, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
8686
app.register_command_set(child1)
8787
assert getattr(app.cmd_func('hello').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Default Category'
8888
app.unregister_command_set(child1)
8989

9090
child_nonheritable = ChildOverridesParentCategoriesNonHeritable(2)
91-
assert getattr(child_nonheritable, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) != 'Non-Heritable Category'
91+
assert getattr(child_nonheritable, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) != 'Non-Heritable Category'
9292
app.register_command_set(child_nonheritable)
9393
assert getattr(app.cmd_func('goodbye').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Non-Heritable Category'
9494
app.unregister_command_set(child_nonheritable)
9595

9696
grandchild1 = GrandchildInheritsGrandparentCategory(3)
97-
assert getattr(grandchild1, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
97+
assert getattr(grandchild1, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category'
9898
app.register_command_set(grandchild1)
9999
assert getattr(app.cmd_func('aloha').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Default Category'
100100
app.unregister_command_set(grandchild1)
101101

102102
child_overrides = ChildOverridesParentCategories(4)
103-
assert getattr(child_overrides, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category'
103+
assert getattr(child_overrides, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category'
104104
app.register_command_set(child_overrides)
105105
assert getattr(app.cmd_func('bonjour').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Heritable Category'
106106
app.unregister_command_set(child_overrides)
107107

108108
grandchild2 = GrandchildInheritsHeritable(5)
109-
assert getattr(grandchild2, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category'
109+
assert getattr(grandchild2, cmd2.constants.CMDSET_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category'

0 commit comments

Comments
 (0)