11# coding=utf-8
22"""Decorators for cmd2 commands"""
33import argparse
4- from typing import Callable , Iterable , List , Optional , Union
4+ from typing import Callable , List , Optional , Union
55
66from . import constants
77from .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-
2510def 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__ :
0 commit comments