summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-07 21:11:44 -0400
committerGitHub <noreply@github.com>2019-05-07 21:11:44 -0400
commit46f0aed0b66f45d08ef7fa8b16f787dc79ea32b1 (patch)
tree02ba0cea776cb23651353314e572f74e543e8acd /cmd2/cmd2.py
parent673d8a1bebcada7f0182758acfe7f65637113286 (diff)
parent47999ffd250eab4875747b32af2e7b7505212d67 (diff)
downloadcmd2-git-46f0aed0b66f45d08ef7fa8b16f787dc79ea32b1.tar.gz
Merge pull request #671 from python-cmd2/completion_exceptions
Improved tab completion error feedback
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index a7b60b1a..3c1c8d2c 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1362,16 +1362,13 @@ class Cmd(cmd.Cmd):
# Display matches using actual display function. This also redraws the prompt and line.
orig_pyreadline_display(matches_to_display)
- # ----- Methods which override stuff in cmd -----
-
- def complete(self, text: str, state: int) -> Optional[str]:
- """Override of command method which returns the next possible completion for 'text'.
+ def _complete_worker(self, text: str, state: int) -> Optional[str]:
+ """The actual worker function for tab completion which is called by complete() and returns
+ the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
- This method gets called directly by readline because it is set as the tab-completion function.
-
This completer function is called as complete(text, state), for state in 0, 1, 2, …, until it returns a
non-string value. It should return the next possible completion starting with text.
@@ -1581,6 +1578,24 @@ class Cmd(cmd.Cmd):
except IndexError:
return None
+ def complete(self, text: str, state: int) -> Optional[str]:
+ """Override of cmd2's complete method which returns the next possible completion for 'text'
+
+ This method gets called directly by readline. Since readline suppresses any exception raised
+ in completer functions, they can be difficult to debug. Therefore this function wraps the
+ actual tab completion logic and prints to stderr any exception that occurs before returning
+ control to readline.
+
+ :param text: the current word that user is typing
+ :param state: non-negative integer
+ """
+ # noinspection PyBroadException
+ try:
+ return self._complete_worker(text, state)
+ except Exception as e:
+ self.perror(e)
+ return None
+
def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int,
argparser: argparse.ArgumentParser) -> List[str]:
"""Default completion function for argparse commands."""