diff --git a/src/click/parser.py b/src/click/parser.py index 8ea69acda..f9ec1a20d 100644 --- a/src/click/parser.py +++ b/src/click/parser.py @@ -398,7 +398,7 @@ def _match_short_opt(self, arg: str, state: _ParsingState) -> None: if self.ignore_unknown_options: unknown_options.append(ch) continue - raise NoSuchOption(opt, ctx=self.ctx) + raise NoSuchOption(arg, ctx=self.ctx) if option.takes_value: # Any characters left in arg? Pretend they're the # next arg, and stop consuming characters of arg. diff --git a/tests/test_options.py b/tests/test_options.py index 50992e2d1..b4d7ae4af 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -144,6 +144,19 @@ def cli(): assert f"No such option '{unknown_flag}'." in result.output +def test_unknown_multichar_short_option_shows_full_name(runner): + """Error message should show the full option string, not just the first char.""" + + @click.command() + def cli(): + pass + + result = runner.invoke(cli, ["-dbg"]) + assert result.exception + assert "No such option '-dbg'." in result.output + assert "No such option '-d'." not in result.output + + @pytest.mark.parametrize( ("value", "expect"), [