diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-05-07 21:11:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 21:11:44 -0400 |
commit | 46f0aed0b66f45d08ef7fa8b16f787dc79ea32b1 (patch) | |
tree | 02ba0cea776cb23651353314e572f74e543e8acd /cmd2/argparse_completer.py | |
parent | 673d8a1bebcada7f0182758acfe7f65637113286 (diff) | |
parent | 47999ffd250eab4875747b32af2e7b7505212d67 (diff) | |
download | cmd2-git-46f0aed0b66f45d08ef7fa8b16f787dc79ea32b1.tar.gz |
Merge pull request #671 from python-cmd2/completion_exceptions
Improved tab completion error feedback
Diffstat (limited to 'cmd2/argparse_completer.py')
-rw-r--r-- | cmd2/argparse_completer.py | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index ac65185b..edfaeec4 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -667,7 +667,7 @@ class AutoCompleter(object): if callable(arg_choices[0]): completer = arg_choices[0] - elif isinstance(arg_choices[0], str) and callable(getattr(self._cmd2_app, arg_choices[0])): + else: completer = getattr(self._cmd2_app, arg_choices[0]) # extract the positional and keyword arguments from the tuple @@ -678,19 +678,16 @@ class AutoCompleter(object): list_args = arg_choices[index] elif isinstance(arg_choices[index], dict): kw_args = arg_choices[index] - try: - # call the provided function differently depending on the provided positional and keyword arguments - if list_args is not None and kw_args is not None: - return completer(text, line, begidx, endidx, *list_args, **kw_args) - elif list_args is not None: - return completer(text, line, begidx, endidx, *list_args) - elif kw_args is not None: - return completer(text, line, begidx, endidx, **kw_args) - else: - return completer(text, line, begidx, endidx) - except TypeError: - # assume this is due to an incorrect function signature, return nothing. - return [] + + # call the provided function differently depending on the provided positional and keyword arguments + if list_args is not None and kw_args is not None: + return completer(text, line, begidx, endidx, *list_args, **kw_args) + elif list_args is not None: + return completer(text, line, begidx, endidx, *list_args) + elif kw_args is not None: + return completer(text, line, begidx, endidx, **kw_args) + else: + return completer(text, line, begidx, endidx) else: return self._cmd2_app.basic_complete(text, line, begidx, endidx, self._resolve_choices_for_arg(action, used_values)) @@ -704,32 +701,17 @@ class AutoCompleter(object): # is the argument a string? If so, see if we can find an attribute in the # application matching the string. if isinstance(args, str): - try: - args = getattr(self._cmd2_app, args) - except AttributeError: - # Couldn't find anything matching the name - return [] + args = getattr(self._cmd2_app, args) # is the provided argument a callable. If so, call it if callable(args): try: - try: - args = args(self._cmd2_app) - except TypeError: - args = args() + args = args(self._cmd2_app) except TypeError: - return [] - - try: - iter(args) - except TypeError: - pass - else: - # filter out arguments we already used - args = [arg for arg in args if arg not in used_values] + args = args() - if len(args) > 0: - return args + # filter out arguments we already used + return [arg for arg in args if arg not in used_values] return [] |