From fa227e3fd4ea1c81e4215c366685b71efacd200c Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Fri, 4 Dec 2026 02:43:28 +0530 Subject: [PATCH] Fix argparse completion ignoring flags given as --flag=value ArgparseCompleter only recognized a flag when the whole token matched a flag name, so --flag=value was skipped. It was never marked as used, mutually exclusive group members were not excluded, and the next positional's hint was wrong. Split the token at '=' the way argparse does and treat the attached value as the flag's argument. --- cmd2/argparse_completer.py | 20 +++++++++++++++----- tests/test_argparse_completer.py | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 7644acaf1..38671f09e 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -305,11 +305,17 @@ def consume_argument(arg_state: _ArgumentState, arg_token: str) -> None: flag_arg_state = None action = None + # A flag can carry its value in the same token (--flag=value) + flag_name = token + explicit_arg: str | None = None + if token not in self._flag_to_action and "=" in token: + flag_name, _, explicit_arg = token.partition("=") + # Does the token match a known flag? - if token in self._flag_to_action: - action = self._flag_to_action[token] + if flag_name in self._flag_to_action: + action = self._flag_to_action[flag_name] elif self._parser.allow_abbrev: - candidates_flags = [flag for flag in self._flag_to_action if flag.startswith(token)] + candidates_flags = [flag for flag in self._flag_to_action if flag.startswith(flag_name)] if len(candidates_flags) == 1: action = self._flag_to_action[candidates_flags[0]] @@ -339,8 +345,12 @@ def consume_argument(arg_state: _ArgumentState, arg_token: str) -> None: # Keep track of this flag if it can receive arguments if new_arg_state.max > 0: - flag_arg_state = new_arg_state - skip_remaining_flags = flag_arg_state.is_remainder + if explicit_arg is not None: + # The value was in the same token, so the flag is finished + consume_argument(new_arg_state, explicit_arg) + else: + flag_arg_state = new_arg_state + skip_remaining_flags = flag_arg_state.is_remainder # Check if token is a flag's argument elif flag_arg_state is not None: diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 522d62670..b2c5eaa99 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -1071,6 +1071,33 @@ def test_complete_mutex_group(ac_app, command_and_args, text, output_contains, f assert output_contains in completions.error +@pytest.mark.parametrize( + ("command_and_args", "text", "output_contains", "first_match"), + [ + # A flag with its value attached completes the mutex group, so no other flag name will complete. + ("mutex --flag=flag_val", "--oth", "", None), + # The flag name is abbreviated. + ("mutex --fl=flag_val", "--oth", "", None), + # The other flag is rejected as it is with a separate value. + ("mutex --flag=flag_val --other", "", "-o/--other_flag: not allowed with argument -f/--flag", None), + # The attached value finishes the flag, so the optional positional is skipped and last_arg shows its hint. + ("mutex --flag=flag_val", "", "the last arg", None), + ], +) +def test_complete_flag_with_equals_value(ac_app, command_and_args, text, output_contains, first_match) -> None: + line = f"{command_and_args} {text}" + endidx = len(line) + begidx = endidx - len(text) + + completions = ac_app.complete(text, line, begidx, endidx) + if first_match is None: + assert not completions + else: + assert first_match == completions[0].text + + assert output_contains in completions.error or output_contains in completions.hint + + def test_single_prefix_char() -> None: from cmd2.argparse_completer import ( _single_prefix_char,