Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Give exclusive control over completions to actions that are unsatisfi…

…ed. Fixes #16 (for real this time)
  • Loading branch information...
commit 8389014484f43a089190629dd9c0d190989ebc7c 1 parent d5cba62
@kislyuk authored
Showing with 21 additions and 3 deletions.
  1. +19 −3 argcomplete/__init__.py
  2. +2 −0  argcomplete/my_argparse.py
View
22 argcomplete/__init__.py
@@ -46,6 +46,16 @@ def mute_stderr():
sys.stderr.close()
sys.stderr = stderr
+def action_is_satisfied(action):
+ num_consumed_args = getattr(action, 'num_consumed_args', 0)
+ if action.nargs == argparse.ONE_OR_MORE and num_consumed_args < 1:
+ return False
+ else:
+ try:
+ return num_consumed_args < action.nargs
+ except:
+ return True
+
class ArgcompleteException(Exception):
pass
@@ -208,6 +218,10 @@ def __call__(self, parser, namespace, values, option_string=None):
completer = completers.ChoicesCompleter(active_action.choices)
if completer:
+ if not action_is_satisfied(active_action):
+ # This means the current action will fail to parse if the word under the cursor is not given
+ # to it, so give it exclusive control over completions (flush previous completions)
+ completions = []
try:
completions += [c for c in completer(prefix=cword_prefix,
parser=parser,
@@ -237,9 +251,11 @@ def __call__(self, parser, namespace, values, option_string=None):
# If cword_prefix contains a char present in COMP_WORDBREAKS, strip from each completion the portion of
# cword_prefix up to the last such occurrence.
- last_wb_match = re.search("[{wb}][^{wb}]*$".format(wb=comp_wordbreaks), cword_prefix)
- if last_wb_match:
- completions = [c[last_wb_match.start()+1:] for c in completions]
+ cword_break_loc = -1
+ for wordbreak_char in comp_wordbreaks:
+ cword_break_loc = max(cword_break_loc, cword_prefix.rfind(wordbreak_char))
+ if cword_break_loc != -1:
+ completions = [c[cword_break_loc+1:] for c in completions]
continuation_chars = '=/:'
# If there's only one completion, and it doesn't end with a continuation char, add a space
View
2  argcomplete/my_argparse.py
@@ -138,6 +138,7 @@ def consume_optional(start_index):
start = start_index + 1
selected_patterns = arg_strings_pattern[start:]
self.active_actions = [action] # Added by argcomplete
+ action.num_consumed_args = 0 # Added by argcomplete
arg_count = match_argument(action, selected_patterns)
stop = start + arg_count
args = arg_strings[start:stop]
@@ -149,6 +150,7 @@ def consume_optional(start_index):
self.active_actions.remove(action)
elif action.nargs == OPTIONAL and len(args) == 1:
self.active_actions.remove(action)
+ action.num_consumed_args = len(args)
# End added by argcomplete
action_tuples.append((action, args, option_string))
Please sign in to comment.
Something went wrong with that request. Please try again.