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

multi_short_opt = max(
(
short_opt
for short_opt in self._short_opt
if len(short_opt) > 2 and arg.startswith(short_opt)
),
key=len,
default=None,
)

if multi_short_opt is not None:
raise NoSuchOption(multi_short_opt, ctx=self.ctx)

raise NoSuchOption(opt, ctx=self.ctx)
if option.takes_value:
# Any characters left in arg? Pretend they're the
Expand Down Expand Up @@ -483,6 +497,21 @@ def _process_opts(self, arg: str, state: _ParsingState) -> None:
try:
self._match_long_opt(norm_long_opt, explicit_value, state)
except NoSuchOption:
single_prefix_long_opt = max(
(
opt
for opt in self._long_opt
if len(opt) > 2
and opt[1] not in self._opt_prefixes
and arg.startswith(opt)
),
key=len,
default=None,
)

if single_prefix_long_opt is not None:
raise NoSuchOption(single_prefix_long_opt, ctx=self.ctx)

# At this point the long option matching failed, and we need
# to try with short options. However there is a special rule
# which says, that if we have a two character options prefix
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_option_for_multichar_short_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