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
13 changes: 13 additions & 0 deletions src/click/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,19 @@ def _process_opts(self, arg: str, state: _ParsingState) -> None:
# short option code and will instead raise the no option
# error.
if arg[:2] not in self._opt_prefixes:
multichar_opt = max(
(
opt
for opt in self._long_opt
if _split_opt(opt)[0] == arg[:1] and arg.startswith(opt)
),
default=None,
key=len,
)

if multichar_opt is not None and multichar_opt != arg:
raise NoSuchOption(multichar_opt, ctx=self.ctx)

self._match_short_opt(arg, state)
return

Expand Down
11 changes: 11 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ def cli():
assert f"No such option: {unknown_flag}" in result.output


def test_unknown_multichar_short_option_reports_full_option(runner):
@click.command()
@click.option("-dbg", is_flag=True)
def cli(dbg):
pass

result = runner.invoke(cli, ["-dbgwrong"])
assert result.exception
assert "No such option: -dbg" in result.output


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