diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-05-07 21:20:11 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-05-07 21:20:11 -0400 |
commit | e52852be4d9270040d4fcb4eeabe2ca15521f48d (patch) | |
tree | 0c94818fca6e86de1c0d968ca1846f8d67792be5 /cmd2/cmd2.py | |
parent | 358ff2b017aff098a82fb1f41dc021c850779daf (diff) | |
parent | 46f0aed0b66f45d08ef7fa8b16f787dc79ea32b1 (diff) | |
download | cmd2-git-e52852be4d9270040d4fcb4eeabe2ca15521f48d.tar.gz |
Merged master and resolved conflicts in CHANGELOG
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 0cc99589..431c51ae 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.""" |