From 878601bc07e5298d50fbf1bd6a8fc2062fef5ed4 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 17 Feb 2020 12:45:20 -0500 Subject: Renamed AutoCompleter to ArgparseCompleter for clarity --- cmd2/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index e324c2f1..b307e0d2 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -109,8 +109,8 @@ class Settable: for this argument (See note below) Note: - For choices_method and completer_method, do not set them to a bound method. This is because AutoCompleter - passes the self argument explicitly to these functions. + For choices_method and completer_method, do not set them to a bound method. This is because + ArgparseCompleter passes the self argument explicitly to these functions. Therefore instead of passing something like self.path_complete, pass cmd2.Cmd.path_complete. """ -- cgit v1.2.1 From d80b27f8e259ffe3b14cef88e8ed9cde350ba397 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 17 Feb 2020 14:13:28 -0500 Subject: Made CompletionError exception available to non-argparse tab completion --- cmd2/utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index b307e0d2..b6b45891 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -72,6 +72,17 @@ def str_to_bool(val: str) -> bool: raise ValueError("must be True or False (case-insensitive)") +class CompletionError(Exception): + """ + Raised during tab completion operations to report any sort of error you want printed by the ArgparseCompleter + + Example use cases + - Reading a database to retrieve a tab completion data set failed + - A previous command line argument that determines the data set being completed is invalid + """ + pass + + class Settable: """Used to configure a cmd2 instance member to be settable via the set command in the CLI""" def __init__(self, name: str, val_type: Callable, description: str, *, -- cgit v1.2.1 From d3deca3c99c299149a30aa3ec760dc17f8abc456 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 18 Feb 2020 12:59:46 -0500 Subject: Added apply_style to CompletionError Simplified error class structure in argparse_completer.py --- cmd2/utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index b6b45891..6a67c43f 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -75,12 +75,26 @@ def str_to_bool(val: str) -> bool: class CompletionError(Exception): """ Raised during tab completion operations to report any sort of error you want printed by the ArgparseCompleter + This can also be used just to display a message, even if it's not an error. ArgparseCompleter raises + CompletionErrors to display tab completion hints and sets apply_style to False so hints aren't colored + like error text. Example use cases - Reading a database to retrieve a tab completion data set failed - A previous command line argument that determines the data set being completed is invalid + - Tab completion hints """ - pass + def __init__(self, *args, apply_style: bool = True, **kwargs): + """ + Initializer for CompletionError + :param apply_style: If True, then ansi.style_error will be applied to the message text when printed. + Set to False in cases where the message text already has the desired style. + Defaults to True. + """ + self.apply_style = apply_style + + # noinspection PyArgumentList + super().__init__(*args, **kwargs) class Settable: -- cgit v1.2.1 From 157efc589cae43e5fb37ac38575d8cdfcd11c9b8 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 20 Feb 2020 12:33:23 -0500 Subject: Moved categorize() to utils.py and made set_parser_prog() non-public --- cmd2/utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'cmd2/utils.py') 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) -- cgit v1.2.1