Skip to content

Commit b7cf069

Browse files
authored
fix shell completion for nested groups (#2935)
2 parents 884af5c + 7c575d6 commit b7cf069

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Version 8.2.1
55

66
- Fix flag value handling for flag options with a provided type. :issue:`2894`
77
:issue:`2897` :pr:`2930`
8+
- Fix shell completion for nested groups. :issue:`2906` :pr:`2907`
89

910
Version 8.2.0
1011
-------------

src/click/shell_completion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,8 @@ def _resolve_context(
567567
with cmd.make_context(
568568
name, args, parent=ctx, resilient_parsing=True
569569
) as sub_ctx:
570-
args = ctx._protected_args + ctx.args
571570
ctx = sub_ctx
571+
args = ctx._protected_args + ctx.args
572572
else:
573573
sub_ctx = ctx
574574

@@ -586,8 +586,8 @@ def _resolve_context(
586586
allow_interspersed_args=False,
587587
resilient_parsing=True,
588588
) as sub_sub_ctx:
589-
args = sub_ctx.args
590589
sub_ctx = sub_sub_ctx
590+
args = sub_ctx.args
591591

592592
ctx = sub_ctx
593593
args = [*sub_ctx._protected_args, *sub_ctx.args]

tests/test_shell_completion.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ def test_group():
4040
assert _get_words(cli, [], "-") == ["-a", "--help"]
4141

4242

43+
@pytest.mark.parametrize(
44+
("args", "word", "expect"),
45+
[
46+
([], "", ["get"]),
47+
(["get"], "", ["full"]),
48+
(["get", "full"], "", ["data"]),
49+
(["get", "full"], "-", ["--verbose", "--help"]),
50+
(["get", "full", "data"], "", []),
51+
(["get", "full", "data"], "-", ["-a", "--help"]),
52+
],
53+
)
54+
def test_nested_group(args: list[str], word: str, expect: list[str]) -> None:
55+
cli = Group(
56+
"cli",
57+
commands=[
58+
Group(
59+
"get",
60+
commands=[
61+
Group(
62+
"full",
63+
params=[Option(["--verbose"])],
64+
commands=[Command("data", params=[Option(["-a"])])],
65+
)
66+
],
67+
)
68+
],
69+
)
70+
assert _get_words(cli, args, word) == expect
71+
72+
4373
def test_group_command_same_option():
4474
cli = Group(
4575
"cli", params=[Option(["-a"])], commands=[Command("x", params=[Option(["-a"])])]

0 commit comments

Comments
 (0)