|
32 | 32 | COMPLETER_FUNC_PREFIX = 'complete_' |
33 | 33 |
|
34 | 34 | # Prefix for private attributes injected by cmd2 |
35 | | -CMD2_ATTR_PREFIX = '_cmd2_' |
| 35 | +PRIVATE_ATTR_PREFIX = '_cmd2_' |
36 | 36 |
|
| 37 | +# Prefix for public attributes injected by cmd2 |
| 38 | +PUBLIC_ATTR_PREFIX = 'cmd2_' |
37 | 39 |
|
38 | | -def cmd2_attr_name(name: str) -> str: |
39 | | - """Build an attribute name with the cmd2 prefix. |
| 40 | + |
| 41 | +def cmd2_private_attr_name(name: str) -> str: |
| 42 | + """Build a private attribute name with the _cmd2_ prefix. |
| 43 | +
|
| 44 | + :param name: the name of the attribute |
| 45 | + :return: the prefixed attribute name |
| 46 | + """ |
| 47 | + return f'{PRIVATE_ATTR_PREFIX}{name}' |
| 48 | + |
| 49 | + |
| 50 | +def cmd2_public_attr_name(name: str) -> str: |
| 51 | + """Build a public attribute name with the cmd2_ prefix. |
40 | 52 |
|
41 | 53 | :param name: the name of the attribute |
42 | 54 | :return: the prefixed attribute name |
43 | 55 | """ |
44 | | - return f'{CMD2_ATTR_PREFIX}{name}' |
| 56 | + return f'{PUBLIC_ATTR_PREFIX}{name}' |
| 57 | + |
| 58 | + |
| 59 | +################################################################################################## |
| 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. |
| 68 | +################################################################################################## |
| 69 | + |
| 70 | +# --- Private Internal Attributes --- |
| 71 | + |
| 72 | +# Attached to a command function; defines its argument parser |
| 73 | +CMD_ATTR_ARGPARSER = cmd2_private_attr_name('argparser') |
| 74 | + |
| 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 |
| 79 | +CMD_ATTR_PRESERVE_QUOTES = cmd2_private_attr_name('preserve_quotes') |
| 80 | + |
| 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") |
| 85 | +SUBCMD_ATTR_COMMAND = cmd2_private_attr_name('parent_command') |
45 | 86 |
|
| 87 | +# Attached to a subcommand function; defines the name of this specific subcommand (e.g., "bar") |
| 88 | +SUBCMD_ATTR_NAME = cmd2_private_attr_name('subcommand_name') |
46 | 89 |
|
47 | | -# The custom help category a command belongs to |
48 | | -CMD_ATTR_HELP_CATEGORY = cmd2_attr_name('help_category') |
49 | | -CLASS_ATTR_DEFAULT_HELP_CATEGORY = cmd2_attr_name('default_help_category') |
| 90 | +# Attached to a subcommand function; specifies kwargs passed to add_parser() |
| 91 | +SUBCMD_ATTR_ADD_PARSER_KWARGS = cmd2_private_attr_name('subcommand_add_parser_kwargs') |
50 | 92 |
|
51 | | -# The argparse parser for the command |
52 | | -CMD_ATTR_ARGPARSER = cmd2_attr_name('argparser') |
| 93 | +# Attached to an argparse parser; identifies the CommandSet instance it belongs to |
| 94 | +PARSER_ATTR_COMMANDSET_ID = cmd2_private_attr_name('command_set_id') |
53 | 95 |
|
54 | | -# Whether or not tokens are unquoted before sending to argparse |
55 | | -CMD_ATTR_PRESERVE_QUOTES = cmd2_attr_name('preserve_quotes') |
56 | 96 |
|
57 | | -# subcommand attributes for the base command name and the subcommand name |
58 | | -SUBCMD_ATTR_COMMAND = cmd2_attr_name('parent_command') |
59 | | -SUBCMD_ATTR_NAME = cmd2_attr_name('subcommand_name') |
60 | | -SUBCMD_ATTR_ADD_PARSER_KWARGS = cmd2_attr_name('subcommand_add_parser_kwargs') |
| 97 | +# --- Public Developer Attributes --- |
61 | 98 |
|
62 | | -# argparse attribute uniquely identifying the command set instance |
63 | | -PARSER_ATTR_COMMANDSET_ID = cmd2_attr_name('command_set_id') |
| 99 | +# Attached to an argparse Namespace; contains the Statement object created during parsing |
| 100 | +NS_ATTR_STATEMENT = cmd2_public_attr_name('statement') |
64 | 101 |
|
65 | | -# custom attributes added to argparse Namespaces |
66 | | -NS_ATTR_SUBCMD_HANDLER = cmd2_attr_name('subcmd_handler') |
| 102 | +# Attached to an argparse Namespace; the function to handle the subcommand (or None) |
| 103 | +NS_ATTR_SUBCMD_HANDLER = cmd2_public_attr_name('subcmd_handler') |
0 commit comments