summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py71
1 files changed, 39 insertions, 32 deletions
diff --git a/cmd2.py b/cmd2.py
index fe59da1b..6d42b730 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -193,7 +193,7 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer
return []
completions = []
- flag_processed = False
+ flag_present = False
# Must have at least the command and one argument for a flag to be present
if len(tokens) > 1:
@@ -204,9 +204,10 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer
# Check if the flag is in the dictionary
if flag in flag_dict:
+ flag_present = True
+
# Check if this flag does completions using an Iterable
if isinstance(flag_dict[flag], collections.Iterable):
- flag_processed = True
strs_to_match = flag_dict[flag]
completions = [cur_str for cur_str in strs_to_match if cur_str.startswith(text)]
@@ -216,12 +217,11 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer
# Otherwise check if this flag does completions with a function
elif callable(flag_dict[flag]):
- flag_processed = True
completer_func = flag_dict[flag]
completions = completer_func(text, line, begidx, endidx)
# Check if we need to run the default completer
- if default_completer is not None and not flag_processed:
+ if default_completer is not None and not flag_present:
completions = default_completer(text, line, begidx, endidx)
completions.sort()
@@ -254,34 +254,34 @@ def index_based_complete(text, line, begidx, endidx, index_dict, default_complet
# Invalid syntax for shlex (Probably due to missing closing quote)
return []
- completions = []
+ if len(tokens) == 0:
+ return []
- # Must have at least the command
- if len(tokens) > 0:
+ completions = []
- # Get the index of the token being completed
- index = len(tokens)
+ # Get the index of the token being completed
+ index = len(tokens)
- # Check if the index is in the dictionary
- if index in index_dict:
+ # Check if the index is in the dictionary
+ if index in index_dict:
- # Check if this index does completions using an Iterable
- if isinstance(index_dict[index], collections.Iterable):
- strs_to_match = index_dict[index]
- completions = [cur_str for cur_str in strs_to_match if cur_str.startswith(text)]
+ # Check if this index does completions using an Iterable
+ if isinstance(index_dict[index], collections.Iterable):
+ strs_to_match = index_dict[index]
+ completions = [cur_str for cur_str in strs_to_match if cur_str.startswith(text)]
- # If there is only 1 match and it's at the end of the line, then add a space
- if len(completions) == 1 and endidx == len(line):
- completions[0] += ' '
+ # If there is only 1 match and it's at the end of the line, then add a space
+ if len(completions) == 1 and endidx == len(line):
+ completions[0] += ' '
- # Otherwise check if this index does completions with a function
- elif callable(index_dict[index]):
- completer_func = index_dict[index]
- completions = completer_func(text, line, begidx, endidx)
+ # Otherwise check if this index does completions with a function
+ elif callable(index_dict[index]):
+ completer_func = index_dict[index]
+ completions = completer_func(text, line, begidx, endidx)
- # Otherwise check if there is a default completer
- elif default_completer is not None:
- completions = default_completer(text, line, begidx, endidx)
+ # Otherwise check if there is a default completer
+ elif default_completer is not None:
+ completions = default_completer(text, line, begidx, endidx)
completions.sort()
return completions
@@ -1350,6 +1350,7 @@ class Cmd(cmd.Cmd):
# Overwrite line to pass into completers
line = expanded_line
+ got_matches = False
if command == '':
compfunc = self.completedefault
else:
@@ -1361,16 +1362,22 @@ class Cmd(cmd.Cmd):
compfunc = self.completedefault
# If there are subcommands, then try completing those if the cursor is in
- # the correct position, otherwise default to using compfunc
+ # index 1 of the command tokens
subcommands = self.get_subcommands(command)
if subcommands is not None:
index_dict = {1: subcommands}
- compfunc = functools.partial(index_based_complete,
- index_dict=index_dict,
- default_completer=compfunc)
-
- # Call the completer function
- self.completion_matches = compfunc(text, line, begidx, endidx)
+ tmp_matches = index_based_complete(text, line, begidx, endidx, index_dict)
+
+ # If we got sumcommand matches, then save them. Otherwise the cursor isn't in index 1
+ # or there is something else there like a flag. The command specific complete function
+ # will handle those cases.
+ if len(tmp_matches) > 0:
+ got_matches = True
+ self.completion_matches = tmp_matches
+
+ # Call the command specific completer function
+ if not got_matches:
+ self.completion_matches = compfunc(text, line, begidx, endidx)
else:
# Complete the command against command names and shortcuts. By design, shortcuts that start with