From 925b04713d61f3232826a292b87fb81a094cd1df Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Tue, 4 Aug 2026 21:42:49 -0700 Subject: [PATCH 1/3] Apply show_only_unused to arguments (fixes #125) ClickCompleter(show_only_unused=True) only hid options that were already present on the line; arguments were completed again even after a value had been supplied. _get_completion_for_cmd_args now skips a click.Argument whose value is already bound in the completion context, mirroring the existing option handling. Variadic arguments (nargs=-1) accept further values and are never hidden. Adds regression tests for both the plain and the variadic argument case. --- Changelog.rst | 2 ++ click_repl/_completer.py | 10 ++++++ .../test_common_tests/test_arg_completion.py | 34 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Changelog.rst b/Changelog.rst index df09155..93c0f07 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -12,6 +12,8 @@ Unreleased - Do not offer true/false value completions for boolean flags (``is_flag=True``, including ``--foo/--no-foo`` style options), which previously inserted a value into the positional-argument slot (#116). +- ``show_only_unused`` now also applies to arguments, so an argument that has + already been given is no longer completed again (#125). .. _version-0.3.0: diff --git a/click_repl/_completer.py b/click_repl/_completer.py index 5556607..bbdb08f 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -262,6 +262,16 @@ def _get_completion_for_cmd_args( break elif isinstance(param, click.Argument): + # Show only unused arguments. A variadic argument (nargs=-1) + # always accepts more values, so it is never hidden. + already_present = autocomplete_ctx.params.get(param.name) not in ( + None, + (), + [], + ) + if self.show_only_unused and already_present and param.nargs != -1: + continue + choices.extend( self._get_completion_from_params( autocomplete_ctx, args, param, incomplete diff --git a/tests/test_completion/test_common_tests/test_arg_completion.py b/tests/test_completion/test_common_tests/test_arg_completion.py index ba0742f..83cda50 100644 --- a/tests/test_completion/test_common_tests/test_arg_completion.py +++ b/tests/test_completion/test_common_tests/test_arg_completion.py @@ -32,3 +32,37 @@ def arg_choices(handler): completions = list(c.get_completions(Document("arg-choices "))) assert {x.text for x in completions} == {"foo", "bar"} + + +def test_only_unused_with_arg(): + @root_command.command() + @click.argument("handler", type=click.Choice(("foo", "bar"))) + def unused_arg(handler): + pass + + c.show_only_unused = True + + completions = list(c.get_completions(Document("unused-arg "))) + assert {x.text for x in completions} == {"foo", "bar"} + + completions = list(c.get_completions(Document("unused-arg foo "))) + assert {x.text for x in completions} == set() + + c.show_only_unused = False + + completions = list(c.get_completions(Document("unused-arg foo "))) + assert {x.text for x in completions} == {"foo", "bar"} + + +def test_only_unused_with_variadic_arg(): + @root_command.command() + @click.argument("handlers", type=click.Choice(("foo", "bar")), nargs=-1) + def unused_variadic_arg(handlers): + pass + + c.show_only_unused = True + + completions = list(c.get_completions(Document("unused-variadic-arg foo "))) + assert {x.text for x in completions} == {"foo", "bar"} + + c.show_only_unused = False From cc8820249d33682817e302ca2b0ee44fdb316d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asif=20Saif=20Uddin=20=7B=22Auvi=22=3A=22=E0=A6=85?= =?UTF-8?q?=E0=A6=AD=E0=A6=BF=22=7D?= Date: Sat, 5 Sep 2026 13:49:07 +0600 Subject: [PATCH 2/3] Improve handling of unused arguments in completer Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- click_repl/_completer.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/click_repl/_completer.py b/click_repl/_completer.py index bbdb08f..f7745e2 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -264,11 +264,22 @@ def _get_completion_for_cmd_args( elif isinstance(param, click.Argument): # Show only unused arguments. A variadic argument (nargs=-1) # always accepts more values, so it is never hidden. - already_present = autocomplete_ctx.params.get(param.name) not in ( - None, - (), - [], - ) + already_present = False + get_source = getattr(autocomplete_ctx, "get_parameter_source", None) + if get_source is not None: + try: + source = get_source(param.name) + except Exception: + source = None + already_present = getattr(source, "name", None) == "COMMANDLINE" + + if not already_present: + already_present = autocomplete_ctx.params.get(param.name) not in ( + None, + (), + [], + ) + if self.show_only_unused and already_present and param.nargs != -1: continue From 216348bad6ce9f08c692907f9a2c55f699f75c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asif=20Saif=20Uddin=20=7B=22Auvi=22=3A=22=E0=A6=85?= =?UTF-8?q?=E0=A6=AD=E0=A6=BF=22=7D?= Date: Sat, 5 Sep 2026 14:47:41 +0600 Subject: [PATCH 3/3] Improve source handling in autocomplete context Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- click_repl/_completer.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/click_repl/_completer.py b/click_repl/_completer.py index f7745e2..b7e7743 100644 --- a/click_repl/_completer.py +++ b/click_repl/_completer.py @@ -266,14 +266,16 @@ def _get_completion_for_cmd_args( # always accepts more values, so it is never hidden. already_present = False get_source = getattr(autocomplete_ctx, "get_parameter_source", None) + source = None if get_source is not None: try: source = get_source(param.name) except Exception: - source = None - already_present = getattr(source, "name", None) == "COMMANDLINE" + pass - if not already_present: + if source is not None: + already_present = getattr(source, "name", None) == "COMMANDLINE" + else: already_present = autocomplete_ctx.params.get(param.name) not in ( None, (),