@@ -1362,16 +1362,13 @@ def _display_matches_pyreadline(self, matches: List[str]) -> None: # pragma: no
13621362 # Display matches using actual display function. This also redraws the prompt and line.
13631363 orig_pyreadline_display (matches_to_display )
13641364
1365- # ----- Methods which override stuff in cmd -----
1366-
1367- def complete (self , text : str , state : int ) -> Optional [str ]:
1368- """Override of command method which returns the next possible completion for 'text'.
1365+ def _complete_worker (self , text : str , state : int ) -> Optional [str ]:
1366+ """The actual worker function for tab completion which is called by complete() and returns
1367+ the next possible completion for 'text'.
13691368
13701369 If a command has not been entered, then complete against command list.
13711370 Otherwise try to call complete_<command> to get list of completions.
13721371
1373- This method gets called directly by readline because it is set as the tab-completion function.
1374-
13751372 This completer function is called as complete(text, state), for state in 0, 1, 2, …, until it returns a
13761373 non-string value. It should return the next possible completion starting with text.
13771374
@@ -1581,6 +1578,24 @@ def complete(self, text: str, state: int) -> Optional[str]:
15811578 except IndexError :
15821579 return None
15831580
1581+ def complete (self , text : str , state : int ) -> Optional [str ]:
1582+ """Override of cmd2's complete method which returns the next possible completion for 'text'
1583+
1584+ This method gets called directly by readline. Since readline suppresses any exception raised
1585+ in completer functions, they can be difficult to debug. Therefore this function wraps the
1586+ actual tab completion logic and prints to stderr any exception that occurs before returning
1587+ control to readline.
1588+
1589+ :param text: the current word that user is typing
1590+ :param state: non-negative integer
1591+ """
1592+ # noinspection PyBroadException
1593+ try :
1594+ return self ._complete_worker (text , state )
1595+ except Exception as e :
1596+ self .perror (e )
1597+ return None
1598+
15841599 def _autocomplete_default (self , text : str , line : str , begidx : int , endidx : int ,
15851600 argparser : argparse .ArgumentParser ) -> List [str ]:
15861601 """Default completion function for argparse commands."""
0 commit comments