Skip to content

Commit ebd6668

Browse files
committed
Fixed issue where flag at beginning of REMAINDER section was tab completing
1 parent 9b43502 commit ebd6668

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

cmd2/argparse_completer.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,16 +440,6 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
440440
# Only start at the start token index
441441
if idx >= self._token_start_index:
442442

443-
# all args after -- are non-flags
444-
if remainder['arg'] is None and token == '--':
445-
flag_action = None
446-
flag_arg.reset()
447-
if is_last_token:
448-
break
449-
else:
450-
skip_remaining_flags = True
451-
continue
452-
453443
# If a remainder action is found, force all future tokens to go to that
454444
if remainder['arg'] is not None:
455445
if remainder['action'] == pos_action:
@@ -482,6 +472,14 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
482472
# don't reset positional tracking because flags can be interspersed anywhere between positionals
483473
flag_action = None
484474

475+
if token == '--':
476+
if is_last_token:
477+
# Exit loop and see if -- can be completed into a flag
478+
break
479+
else:
480+
# In argparse, all args after -- are non-flags
481+
skip_remaining_flags = True
482+
485483
# does the token fully match a known flag?
486484
if token in self._flag_to_action:
487485
flag_action = self._flag_to_action[token]

tests/test_autocompletion.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,29 @@ def test_autcomp_custom_func_list_and_dict_arg(cmd2_app):
279279
cmd2_app.completion_matches == ['S01E02', 'S01E03', 'S02E01', 'S02E03']
280280

281281

282-
def test_argparse_remainder_completion(cmd2_app):
282+
def test_argparse_remainder_flag_completion(cmd2_app):
283283
import cmd2
284284
import argparse
285285

286-
# First test a positional with nargs=argparse.REMAINDER
286+
# Test flag completion as first arg of positional with nargs=argparse.REMAINDER
287+
text = '--h'
288+
line = 'help command {}'.format(text)
289+
endidx = len(line)
290+
begidx = endidx - len(text)
291+
292+
# --h should not complete into --help because we are in the argparse.REMAINDER section
293+
assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
294+
295+
# Test flag completion within an already started positional with nargs=argparse.REMAINDER
287296
text = '--h'
288297
line = 'help command subcommand {}'.format(text)
289298
endidx = len(line)
290299
begidx = endidx - len(text)
291300

292-
# --h should not complete into --help because we are in the argparse.REMAINDER sections
301+
# --h should not complete into --help because we are in the argparse.REMAINDER section
293302
assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
294303

295-
# Now test a flag with nargs=argparse.REMAINDER
304+
# Test a flag with nargs=argparse.REMAINDER
296305
parser = argparse.ArgumentParser()
297306
parser.add_argument('-f', nargs=argparse.REMAINDER)
298307

@@ -304,18 +313,24 @@ def test_argparse_remainder_completion(cmd2_app):
304313
endidx = len(line)
305314
begidx = endidx - len(text)
306315

307-
# --h should not complete into --help because we are in the argparse.REMAINDER sections
316+
# --h should not complete into --help because we are in the argparse.REMAINDER section
308317
assert complete_tester(text, line, begidx, endidx, cmd2_app) is None
309318

310319

311320
def test_completion_after_double_dash(cmd2_app):
312-
# Test -- as the last token before an argparse.REMAINDER sections
321+
"""
322+
Test completion after --, which argparse says (all args after -- are non-options)
323+
All of these tests occur outside of an argparse.REMAINDER section since those tests
324+
are handled in test_argparse_remainder_flag_completion
325+
"""
326+
327+
# Test -- as the last token
313328
text = '--'
314329
line = 'help {}'.format(text)
315330
endidx = len(line)
316331
begidx = endidx - len(text)
317332

318-
# Since -- is the last token in a non-remainder section, then it should show flag choices
333+
# Since -- is the last token, then it should show flag choices
319334
first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
320335
assert first_match is not None and '--help' in cmd2_app.completion_matches
321336

@@ -325,5 +340,5 @@ def test_completion_after_double_dash(cmd2_app):
325340
endidx = len(line)
326341
begidx = endidx - len(text)
327342

328-
# Since -- appeared before the -- being completed, no more flags should be completed
343+
# Since -- appeared before the -- being completed, nothing should be completed
329344
assert complete_tester(text, line, begidx, endidx, cmd2_app) is None

0 commit comments

Comments
 (0)