Skip to content

Commit 2d2319b

Browse files
committed
Made common prefix for all custom cmd2 attributes.
1 parent c516fa6 commit 2d2319b

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

cmd2/argparse_custom.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ def set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:
357357
############################################################################################################
358358

359359
CUSTOM_ACTION_ATTRIBS: set[str] = set()
360-
_CUSTOM_ATTRIB_PREFIX = '_cmd2_'
361360

362361

363362
def register_argparse_argument_parameter(
@@ -374,7 +373,7 @@ def register_argparse_argument_parameter(
374373
if not param_name.isidentifier():
375374
raise KeyError(f'Invalid parameter name {param_name} - cannot be used as a python identifier')
376375

377-
attr_name = f'{_CUSTOM_ATTRIB_PREFIX}{param_name}'
376+
attr_name = constants.cmd2_attr_name(param_name)
378377
if param_name in CUSTOM_ACTION_ATTRIBS or hasattr(argparse.Action, attr_name):
379378
raise KeyError(f'Custom parameter {param_name} already exists')
380379

cmd2/constants.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,36 @@
3131
# All command completer functions start with this
3232
COMPLETER_FUNC_PREFIX = 'complete_'
3333

34+
# Prefix for private attributes injected by cmd2
35+
CMD2_ATTR_PREFIX = '_cmd2_'
36+
37+
38+
def cmd2_attr_name(name: str) -> str:
39+
"""Build an attribute name with the cmd2 prefix.
40+
41+
:param name: the name of the attribute
42+
:return: the prefixed attribute name
43+
"""
44+
return f'{CMD2_ATTR_PREFIX}{name}'
45+
46+
3447
# The custom help category a command belongs to
35-
CMD_ATTR_HELP_CATEGORY = 'help_category'
36-
CLASS_ATTR_DEFAULT_HELP_CATEGORY = 'cmd2_default_help_category'
48+
CMD_ATTR_HELP_CATEGORY = cmd2_attr_name('help_category')
49+
CLASS_ATTR_DEFAULT_HELP_CATEGORY = cmd2_attr_name('default_help_category')
3750

3851
# The argparse parser for the command
39-
CMD_ATTR_ARGPARSER = 'argparser'
52+
CMD_ATTR_ARGPARSER = cmd2_attr_name('argparser')
4053

4154
# Whether or not tokens are unquoted before sending to argparse
42-
CMD_ATTR_PRESERVE_QUOTES = 'preserve_quotes'
55+
CMD_ATTR_PRESERVE_QUOTES = cmd2_attr_name('preserve_quotes')
4356

4457
# subcommand attributes for the base command name and the subcommand name
45-
SUBCMD_ATTR_COMMAND = 'parent_command'
46-
SUBCMD_ATTR_NAME = 'subcommand_name'
47-
SUBCMD_ATTR_ADD_PARSER_KWARGS = 'subcommand_add_parser_kwargs'
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')
4861

49-
# arpparse attribute uniquely identifying the command set instance
50-
PARSER_ATTR_COMMANDSET_ID = 'command_set_id'
62+
# argparse attribute uniquely identifying the command set instance
63+
PARSER_ATTR_COMMANDSET_ID = cmd2_attr_name('command_set_id')
5164

5265
# custom attributes added to argparse Namespaces
53-
NS_ATTR_SUBCMD_HANDLER = '__subcmd_handler__'
66+
NS_ATTR_SUBCMD_HANDLER = cmd2_attr_name('subcmd_handler')

0 commit comments

Comments
 (0)