From 697db5af9b1a3d444c094d63f88d141dd84d4f24 Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Fri, 4 Sep 2026 23:51:16 -0700 Subject: [PATCH] Fix shortest-only completion at an empty root prompt --- click_repl/_completer.py | 2 +- .../test_common_tests/test_option_completion.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/click_repl/_completer.py b/click_repl/_completer.py index 5556607..cf0ffe7 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -234,7 +234,7 @@ def _get_completion_for_cmd_args( self.shortest_only and not incomplete # just typed a space # not selecting a value for a longer version of this option - and args[-1] not in opts + and (not args or args[-1] not in opts) ): opts = [min(opts, key=len)] diff --git a/tests/test_completion/test_common_tests/test_option_completion.py b/tests/test_completion/test_common_tests/test_option_completion.py index ff8b89b..4e81418 100644 --- a/tests/test_completion/test_common_tests/test_option_completion.py +++ b/tests/test_completion/test_common_tests/test_option_completion.py @@ -133,3 +133,19 @@ def shortest_only(foo, bar, foobar): completions = list(c.get_completions(Document("shortest-only "))) assert {x.text for x in completions} == {"-f", "--foo", "-b", "--bar", "--foobar"} + + +def test_shortest_only_at_empty_root_prompt(capsys): + @click.group() + @click.option("--verbose", "-v", is_flag=True) + def root(verbose): + pass + + @root.command() + def child(): + pass + + completer = ClickCompleter(root, click.Context(root), shortest_only=True) + completions = list(completer.get_completions(Document(""))) + assert {item.text for item in completions} == {"-v", "child"} + assert capsys.readouterr().out == ""