diff options
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/__init__.py | 4 | ||||
-rw-r--r-- | cmd2/decorators.py | 24 | ||||
-rw-r--r-- | cmd2/utils.py | 15 |
3 files changed, 22 insertions, 21 deletions
diff --git a/cmd2/__init__.py b/cmd2/__init__.py index 43578e46..27a0e96e 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, EmptyStatement 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 Settable +from .utils import Settable, categorize diff --git a/cmd2/decorators.py b/cmd2/decorators.py index f46e1423..42c54c56 100644 --- a/cmd2/decorators.py +++ b/cmd2/decorators.py @@ -5,21 +5,7 @@ from typing import Callable, Iterable, 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) +from .utils import categorize def with_category(category: str) -> Callable: @@ -72,7 +58,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]. @@ -89,7 +75,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 @@ -136,7 +122,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__: @@ -194,7 +180,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 e324c2f1..74df7444 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -404,6 +404,21 @@ def get_exes_in_path(starts_with: str) -> List[str]: return list(exes_set) +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) + + class StdSim: """ Class to simulate behavior of sys.stdout or sys.stderr. |