Skip to content

Commit a2061ba

Browse files
authored
Merge pull request #895 from python-cmd2/relocate_functions
Moved categorize() to utils.py and made set_parser_prog() non-public
2 parents 660d41b + a62622a commit a2061ba

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

cmd2/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .argparse_custom import DEFAULT_ARGUMENT_PARSER
2525
from .cmd2 import Cmd
2626
from .constants import COMMAND_NAME, DEFAULT_SHORTCUTS
27-
from .decorators import categorize, with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
27+
from .decorators import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
2828
from .parsing import Statement
2929
from .py_bridge import CommandResult
30-
from .utils import CompletionError, Settable
30+
from .utils import categorize, CompletionError, Settable

cmd2/decorators.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
# coding=utf-8
22
"""Decorators for cmd2 commands"""
33
import argparse
4-
from typing import Callable, Iterable, List, Optional, Union
4+
from typing import Callable, List, Optional, Union
55

66
from . import constants
77
from .parsing import Statement
88

99

10-
def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None:
11-
"""Categorize a function.
12-
13-
The help command output will group this function under the specified category heading
14-
15-
:param func: function or list of functions to categorize
16-
:param category: category to put it in
17-
"""
18-
if isinstance(func, Iterable):
19-
for item in func:
20-
setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category)
21-
else:
22-
setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)
23-
24-
2510
def with_category(category: str) -> Callable:
2611
"""A decorator to apply a category to a command function."""
2712
def cat_decorator(func):
13+
from .utils import categorize
2814
categorize(func, category)
2915
return func
3016
return cat_decorator
@@ -62,7 +48,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):
6248

6349

6450
# noinspection PyProtectedMember
65-
def set_parser_prog(parser: argparse.ArgumentParser, prog: str):
51+
def _set_parser_prog(parser: argparse.ArgumentParser, prog: str):
6652
"""
6753
Recursively set prog attribute of a parser and all of its subparsers so that the root command
6854
is a command name and not sys.argv[0].
@@ -79,7 +65,7 @@ def set_parser_prog(parser: argparse.ArgumentParser, prog: str):
7965
# Set the prog value for each subcommand
8066
for sub_cmd, sub_cmd_parser in action.choices.items():
8167
sub_cmd_prog = parser.prog + ' ' + sub_cmd
82-
set_parser_prog(sub_cmd_parser, sub_cmd_prog)
68+
_set_parser_prog(sub_cmd_parser, sub_cmd_prog)
8369

8470
# We can break since argparse only allows 1 group of subcommands per level
8571
break
@@ -126,7 +112,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):
126112

127113
# argparser defaults the program name to sys.argv[0], but we want it to be the name of our command
128114
command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX):]
129-
set_parser_prog(parser, command_name)
115+
_set_parser_prog(parser, command_name)
130116

131117
# If the description has not been set, then use the method docstring if one exists
132118
if parser.description is None and func.__doc__:
@@ -184,7 +170,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):
184170

185171
# argparser defaults the program name to sys.argv[0], but we want it to be the name of our command
186172
command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX):]
187-
set_parser_prog(parser, command_name)
173+
_set_parser_prog(parser, command_name)
188174

189175
# If the description has not been set, then use the method docstring if one exists
190176
if parser.description is None and func.__doc__:

cmd2/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,18 @@ def get_styles_in_text(text: str) -> Dict[int, str]:
964964
start += len(match.group())
965965

966966
return styles
967+
968+
969+
def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None:
970+
"""Categorize a function.
971+
972+
The help command output will group this function under the specified category heading
973+
974+
:param func: function or list of functions to categorize
975+
:param category: category to put it in
976+
"""
977+
if isinstance(func, Iterable):
978+
for item in func:
979+
setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category)
980+
else:
981+
setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category)

docs/api/utility_functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Utility Functions
77

88
.. autofunction:: cmd2.utils.strip_quotes
99

10-
.. autofunction:: cmd2.decorators.categorize
10+
.. autofunction:: cmd2.utils.categorize
1111

1212
.. autofunction:: cmd2.utils.align_text
1313

0 commit comments

Comments
 (0)