Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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
26 changes: 6 additions & 20 deletions cmd2/decorators.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -62,7 +48,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):


# 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].
Expand All @@ -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
Expand Down Expand Up @@ -126,7 +112,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):

# 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__:
Expand Down Expand Up @@ -184,7 +170,7 @@ def cmd_wrapper(cmd2_app, statement: Union[Statement, str]):

# 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__:
Expand Down
15 changes: 15 additions & 0 deletions cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion docs/api/utility_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Utility Functions

.. autofunction:: cmd2.utils.strip_quotes

.. autofunction:: cmd2.decorators.categorize
.. autofunction:: cmd2.utils.categorize

.. autofunction:: cmd2.utils.align_text

Expand Down