Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading