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
12 changes: 12 additions & 0 deletions src/click/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ def _match_short_opt(self, arg: str, state: _ParsingState) -> None:
if self.ignore_unknown_options:
unknown_options.append(ch)
continue

# If a registered single-dash long option (like -dbg)
# is a prefix of the original argument, report the
# error using the full argument instead of just the
# single character that failed to match.
norm_arg = _normalize_opt(arg, self.ctx)
for long_opt_name in self._long_opt:
if norm_arg.startswith(long_opt_name) and len(
long_opt_name
) > len(opt):
raise NoSuchOption(norm_arg, ctx=self.ctx)

raise NoSuchOption(opt, ctx=self.ctx)
if option.takes_value:
# Any characters left in arg? Pretend they're the
Expand Down
20 changes: 20 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ def cli():
assert f"No such option: {unknown_flag}" in result.output


def test_multicharacter_short_option_wrong_option_error(runner):
"""When a wrong multi-character short option like -dbgwrong is passed,
the error should reference the full argument, not just the first char."""

@click.command()
@click.option("-dbg", is_flag=True)
def cli(dbg):
click.echo(f"dbg={dbg}")

# Correct usage should work.
result = runner.invoke(cli, ["-dbg"])
assert not result.exception
assert "dbg=True" in result.output

# Wrong option should mention the full argument, not just '-d'.
result = runner.invoke(cli, ["-dbgwrong"])
assert result.exit_code != 0
assert "No such option: -dbgwrong" in result.output


@pytest.mark.parametrize(
("value", "expect"),
[
Expand Down
Loading