From fc500f124ab4bde2e9ec55e78e6bc1743a8f9e13 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 21 Mar 2018 21:08:41 -0400 Subject: Added better analysis of tab completing shell commands --- cmd2.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'cmd2.py') diff --git a/cmd2.py b/cmd2.py index 84e06b82..4139217e 100755 --- a/cmd2.py +++ b/cmd2.py @@ -1777,32 +1777,30 @@ class Cmd(cmd.Cmd): self.completion_matches = [' '] return self.completion_matches[state] - # Otherwise select a completer function - if command == '': - compfunc = self.completedefault - else: + # Check if a valid command was entered + if command not in self.get_command_names(): + # Check if this command should be run as a shell command + if self.default_to_shell and command in self._get_exes_in_path(command): + compfunc = functools.partial(path_complete) + else: + compfunc = self.completedefault + # A valid command was entered + else: # Get the completer function for this command - is_shell_command = False try: compfunc = getattr(self, 'complete_' + command) except AttributeError: - # Check if this command should be run as a shell command - if self.default_to_shell and command in self._get_exes_in_path(command): - compfunc = functools.partial(path_complete) - is_shell_command = True - else: - compfunc = self.completedefault - - if not is_shell_command: - # If there are subcommands, then try completing those if the cursor is in - # the token at index 1, otherwise default to using compfunc - subcommands = self.get_subcommands(command) - if subcommands is not None: - index_dict = {1: subcommands} - compfunc = functools.partial(index_based_complete, - index_dict=index_dict, - all_else=compfunc) + compfunc = self.completedefault + + # If there are subcommands, then try completing those if the cursor is in + # the token at index 1, otherwise default to using compfunc + subcommands = self.get_subcommands(command) + if subcommands is not None: + index_dict = {1: subcommands} + compfunc = functools.partial(index_based_complete, + index_dict=index_dict, + all_else=compfunc) # Call the completer function self.completion_matches = compfunc(text, line, begidx, endidx) -- cgit v1.2.1