diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-13 14:19:05 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-13 14:20:31 -0400 |
commit | e6da8596c433f46bc337c7e8a14c7de1b0310e4c (patch) | |
tree | 09f5a3225376e26dcb03419d6243c8fc52433b07 /cmd2/argparse_completer.py | |
parent | 5dd2d03ef35a3d33ff53d82c8039d68e263246ee (diff) | |
download | cmd2-git-e6da8596c433f46bc337c7e8a14c7de1b0310e4c.tar.gz |
Replaced choices_function / choices_method with choices_provider.
Replaced completer_function / completer_method with completer.
ArgparseCompleter now always passes cmd2.Cmd or CommandSet instance as the self
argument to choices_provider and completer functions.
Moved basic_complete from utils into cmd2.Cmd class.
Moved CompletionError to exceptions.py
Diffstat (limited to 'cmd2/argparse_completer.py')
-rw-r--r-- | cmd2/argparse_completer.py | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 0efaebe9..8bfaec80 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -24,8 +24,8 @@ from .argparse_custom import ( generate_range_error, ) from .command_definition import CommandSet +from .exceptions import CompletionError from .table_creator import Column, SimpleTable -from .utils import CompletionError, basic_complete # If no descriptive header is supplied, then this will be used instead DEFAULT_DESCRIPTIVE_HEADER = 'Description' @@ -459,7 +459,7 @@ class ArgparseCompleter: if action.help != argparse.SUPPRESS: match_against.append(flag) - return basic_complete(text, line, begidx, endidx, match_against) + return self._cmd2_app.basic_complete(text, line, begidx, endidx, match_against) def _format_completions(self, arg_state: _ArgumentState, completions: List[Union[str, CompletionItem]]) -> List[str]: # Check if the results are CompletionItems and that there aren't too many to display @@ -533,7 +533,7 @@ class ArgparseCompleter: return completer.complete_subcommand_help(tokens[token_index:], text, line, begidx, endidx) elif token_index == len(tokens) - 1: # Since this is the last token, we will attempt to complete it - return basic_complete(text, line, begidx, endidx, self._subcommand_action.choices) + return self._cmd2_app.basic_complete(text, line, begidx, endidx, self._subcommand_action.choices) else: break return [] @@ -577,16 +577,15 @@ class ArgparseCompleter: args = [] kwargs = {} if isinstance(arg_choices, ChoicesCallable): - if arg_choices.is_method: - # The completer may or may not be defined in the same class as the command. Since completer - # functions are registered with the command argparser before anything is instantiated, we - # need to find an instance at runtime that matches the types during declaration - cmd_set = self._cmd2_app._resolve_func_self(arg_choices.to_call, cmd_set) - if cmd_set is None: - # No cases matched, raise an error - raise CompletionError('Could not find CommandSet instance matching defining type for completer') + # The completer may or may not be defined in the same class as the command. Since completer + # functions are registered with the command argparser before anything is instantiated, we + # need to find an instance at runtime that matches the types during declaration + self_arg = self._cmd2_app._resolve_func_self(arg_choices.to_call, cmd_set) + if self_arg is None: + # No cases matched, raise an error + raise CompletionError('Could not find CommandSet instance matching defining type for completer') - args.append(cmd_set) + args.append(self_arg) # Check if arg_choices.to_call expects arg_tokens to_call_params = inspect.signature(arg_choices.to_call).parameters @@ -630,6 +629,6 @@ class ArgparseCompleter: arg_choices = [choice for choice in arg_choices if choice not in used_values] # Do tab completion on the choices - results = basic_complete(text, line, begidx, endidx, arg_choices) + results = self._cmd2_app.basic_complete(text, line, begidx, endidx, arg_choices) return self._format_completions(arg_state, results) |