diff options
-rw-r--r-- | cmd2/__init__.py | 4 | ||||
-rw-r--r-- | cmd2/decorators.py | 26 | ||||
-rw-r--r-- | cmd2/utils.py | 15 | ||||
-rw-r--r-- | docs/api/utility_functions.rst | 2 |
4 files changed, 24 insertions, 23 deletions
diff --git a/cmd2/__init__.py b/cmd2/__init__.py index 63e27812..eb5c275d 100644 --- a/cmd2/__init__.py +++ b/cmd2/__init__.py @@ -24,7 +24,7 @@ if cmd2_parser_module is not None: from .argparse_custom import DEFAULT_ARGUMENT_PARSER from .cmd2 import Cmd from .constants import COMMAND_NAME, DEFAULT_SHORTCUTS -from .decorators import categorize, with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category +from .decorators import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category from .parsing import Statement from .py_bridge import CommandResult -from .utils import CompletionError, Settable +from .utils import categorize, CompletionError, Settable diff --git a/cmd2/decorators.py b/cmd2/decorators.py index 2c812345..ee5db140 100644 --- a/cmd2/decorators.py +++ b/cmd2/decorators.py @@ -1,30 +1,16 @@ # coding=utf-8 """Decorators for cmd2 commands""" import argparse -from typing import Callable, Iterable, List, Optional, Union +from typing import Callable, List, Optional, Union from . import constants from .parsing import Statement -def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None: - """Categorize a function. - - The help command output will group this function under the specified category heading - - :param func: function or list of functions to categorize - :param category: category to put it in - """ - if isinstance(func, Iterable): - for item in func: - setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category) - else: - setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category) - - def with_category(category: str) -> Callable: """A decorator to apply a category to a command function.""" def cat_decorator(func): + from .utils import categorize categorize(func, category) return func return cat_decorator @@ -62,7 +48,7 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) -> # noinspection PyProtectedMember -def set_parser_prog(parser: argparse.ArgumentParser, prog: str): +def _set_parser_prog(parser: argparse.ArgumentParser, prog: str): """ Recursively set prog attribute of a parser and all of its subparsers so that the root command is a command name and not sys.argv[0]. @@ -79,7 +65,7 @@ def set_parser_prog(parser: argparse.ArgumentParser, prog: str): # Set the prog value for each subcommand for sub_cmd, sub_cmd_parser in action.choices.items(): sub_cmd_prog = parser.prog + ' ' + sub_cmd - set_parser_prog(sub_cmd_parser, sub_cmd_prog) + _set_parser_prog(sub_cmd_parser, sub_cmd_prog) # We can break since argparse only allows 1 group of subcommands per level break @@ -126,7 +112,7 @@ def with_argparser_and_unknown_args(parser: argparse.ArgumentParser, *, # argparser defaults the program name to sys.argv[0], but we want it to be the name of our command command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX):] - set_parser_prog(parser, command_name) + _set_parser_prog(parser, command_name) # If the description has not been set, then use the method docstring if one exists if parser.description is None and func.__doc__: @@ -184,7 +170,7 @@ def with_argparser(parser: argparse.ArgumentParser, *, # argparser defaults the program name to sys.argv[0], but we want it to be the name of our command command_name = func.__name__[len(constants.COMMAND_FUNC_PREFIX):] - set_parser_prog(parser, command_name) + _set_parser_prog(parser, command_name) # If the description has not been set, then use the method docstring if one exists if parser.description is None and func.__doc__: diff --git a/cmd2/utils.py b/cmd2/utils.py index 6a67c43f..971a22ce 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -964,3 +964,18 @@ def get_styles_in_text(text: str) -> Dict[int, str]: start += len(match.group()) return styles + + +def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None: + """Categorize a function. + + The help command output will group this function under the specified category heading + + :param func: function or list of functions to categorize + :param category: category to put it in + """ + if isinstance(func, Iterable): + for item in func: + setattr(item, constants.CMD_ATTR_HELP_CATEGORY, category) + else: + setattr(func, constants.CMD_ATTR_HELP_CATEGORY, category) diff --git a/docs/api/utility_functions.rst b/docs/api/utility_functions.rst index 4f788e3d..b348ac1c 100644 --- a/docs/api/utility_functions.rst +++ b/docs/api/utility_functions.rst @@ -7,7 +7,7 @@ Utility Functions .. autofunction:: cmd2.utils.strip_quotes -.. autofunction:: cmd2.decorators.categorize +.. autofunction:: cmd2.utils.categorize .. autofunction:: cmd2.utils.align_text |