Skip to content

Commit 967a8ba

Browse files
committed
Argparse tab completer will complete remaining flag names if there are no more positionals to complete.
1 parent ceacf22 commit 967a8ba

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Tab completion of `CompletionItems` now includes divider row comprised of `Cmd.ruler` character.
2121
* Removed `--verbose` flag from set command since descriptions always show now.
2222
* All cmd2 built-in commands now populate `self.last_result`.
23+
* Argparse tab completer will complete remaining flag names if there are no more positionals to complete.
2324
* Deletions (potentially breaking changes)
2425
* Deleted ``set_choices_provider()`` and ``set_completer()`` which were deprecated in 2.1.2
2526

cmd2/argparse_completer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
504504
elif not _single_prefix_char(text, self._parser) or skip_remaining_flags:
505505
raise _NoResultsError(self._parser, pos_arg_state.action)
506506

507-
# Handle case in which text is a single flag prefix character that
508-
# didn't complete against any argument values.
509-
if _single_prefix_char(text, self._parser) and not skip_remaining_flags:
507+
# If we aren't skipping remaining flags, then complete flag names if either is True:
508+
# 1. text is a single flag prefix character that didn't complete against any argument values
509+
# 2. there are no more positionals to complete
510+
if not skip_remaining_flags and (_single_prefix_char(text, self._parser) or not remaining_positionals):
510511
return self._complete_flags(text, line, begidx, endidx, matched_flags)
511512

512513
return completion_results

tests/test_argparse_completer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def do_flag(self, args: argparse.Namespace) -> None:
9898
def do_plus_flag(self, args: argparse.Namespace) -> None:
9999
pass
100100

101+
# A parser with a positional and flags. Used to test that remaining flag names are completed when all positionals are done.
102+
pos_and_flag_parser = Cmd2ArgumentParser()
103+
pos_and_flag_parser.add_argument("positional", choices=["a", "choice"])
104+
pos_and_flag_parser.add_argument("-f", "--flag", action='store_true')
105+
106+
@with_argparser(pos_and_flag_parser)
107+
def do_pos_and_flag(self, args: argparse.Namespace) -> None:
108+
pass
109+
101110
############################################################################################################
102111
# Begin code related to testing choices and choices_provider parameters
103112
############################################################################################################
@@ -519,6 +528,11 @@ def test_subcommand_completions(ac_app, subcommand, text, completions):
519528
('flag --help --', '--', [], []),
520529
('plus_flag --', '++', [], []),
521530
('plus_flag ++help --', '++', [], []),
531+
# Test remaining flag names complete after all positionals are complete
532+
('pos_and_flag', '', ['a', 'choice'], ['a', 'choice']),
533+
('pos_and_flag choice ', '', ['--flag', '--help', '-f', '-h'], ['[-f, --flag]', '[-h, --help]']),
534+
('pos_and_flag choice -f ', '', ['--help', '-h'], ['[-h, --help]']),
535+
('pos_and_flag choice -f -h ', '', [], []),
522536
],
523537
)
524538
def test_autcomp_flag_completion(ac_app, command_and_args, text, completion_matches, display_matches):

0 commit comments

Comments
 (0)