Skip to content
Open
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
8 changes: 4 additions & 4 deletions click_repl/_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .exceptions import ClickExit # type: ignore[attr-defined]
from .exceptions import CommandLineParserError, ExitReplException, InvalidGroupFormat
from .globals_ import ISATTY, get_current_repl_ctx
from .utils import _execute_internal_and_sys_cmds
from .utils import _execute_internal_and_sys_cmds, _get_protected_args, _set_protected_args

__all__ = ["bootstrap_prompt", "register_repl", "repl"]

Expand Down Expand Up @@ -147,12 +147,12 @@ def get_command() -> str:

try:
# The group command will dispatch based on args.
old_protected_args = group_ctx.protected_args
old_protected_args = _get_protected_args(group_ctx)
try:
group_ctx.protected_args = args
_set_protected_args(group_ctx, args)
group.invoke(group_ctx)
finally:
group_ctx.protected_args = old_protected_args
_set_protected_args(group_ctx, old_protected_args)
except click.ClickException as e:
e.show()
except (ClickExit, SystemExit):
Expand Down
18 changes: 16 additions & 2 deletions click_repl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
]


def _get_protected_args(ctx: click.Context) -> list[str]:
# In click >= 8.2, protected_args is a read-only property over _protected_args.
if hasattr(ctx, "_protected_args"):
return ctx._protected_args
return ctx.protected_args


def _set_protected_args(ctx: click.Context, args: list[str]) -> None:
if hasattr(ctx, "_protected_args"):
ctx._protected_args = args
else:
ctx.protected_args = args


def _resolve_context(args: list[str], ctx: click.Context) -> click.Context:
"""Produce the context hierarchy starting with the command and
traversing the complete arguments. This only follows the commands,
Expand All @@ -49,7 +63,7 @@ def _resolve_context(args: list[str], ctx: click.Context) -> click.Context:
return ctx

ctx = cmd.make_context(name, args, parent=ctx, resilient_parsing=True)
args = ctx.protected_args + ctx.args
args = _get_protected_args(ctx) + ctx.args
else:
while args:
name, cmd, args = command.resolve_command(ctx, args)
Expand All @@ -68,7 +82,7 @@ def _resolve_context(args: list[str], ctx: click.Context) -> click.Context:
args = sub_ctx.args

ctx = sub_ctx
args = [*sub_ctx.protected_args, *sub_ctx.args]
args = [*_get_protected_args(sub_ctx), *sub_ctx.args]
else:
break

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ packages=
click_repl

install_requires =
click>=7.0,<8.2.0
click>=7.0,<9.0
prompt_toolkit>=3.0.36
typing-extensions>=4.7.0

Expand Down
4 changes: 2 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def root_command():


def test_arg_completion():
@root_command.command()
@root_command.command(name="arg-cmd")
@click.argument("handler", type=click.Choice(("foo", "bar")))
def arg_cmd(handler):
pass
Expand All @@ -22,7 +22,7 @@ def arg_cmd(handler):
assert {x.text for x in completions} == {"foo", "bar"}


@root_command.command()
@root_command.command(name="option-cmd")
@click.option("--handler", "-h", type=click.Choice(("foo", "bar")), help="Demo option")
def option_cmd(handler):
pass
Expand Down
4 changes: 2 additions & 2 deletions tests/test_command_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def test_command_collection():
def foo_group():
pass

@foo_group.command()
@foo_group.command(name="foo-cmd")
def foo_cmd():
pass

@click.group()
def foobar_group():
pass

@foobar_group.command()
@foobar_group.command(name="foobar-cmd")
def foobar_cmd():
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def shell_complete(self, ctx, param, incomplete):
if name.startswith(incomplete)
]

@root_command.command()
@root_command.command(name="autocompletion-arg-cmd")
@click.argument("handler", type=MyVar())
def autocompletion_arg_cmd(handler):
pass

completions = list(c.get_completions(Document("autocompletion-cmd ")))
completions = list(c.get_completions(Document("autocompletion-arg-cmd ")))
assert {x.text for x in completions} == {"foo", "bar"}


Expand Down Expand Up @@ -77,7 +77,7 @@ def return_type_tuple_shell_complete(ctx, param, incomplete):
if i[1].startswith(incomplete)
]

@root_command.command()
@root_command.command(name="tuple-type-autocompletion-cmd")
@click.argument("foo", shell_complete=return_type_tuple_shell_complete)
def tuple_type_autocompletion_cmd(foo):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def shell_complete(self, ctx, param, incomplete):
if name.startswith(incomplete)
]

@root_command.command()
@root_command.command(name="autocompletion-opt-cmd")
@click.option("--handler", "-h", type=MyVar())
def autocompletion_opt_cmd(handler):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def return_type_tuple_shell_complete(ctx, args, incomplete):
if i[1].startswith(incomplete)
]

@root_command.command()
@root_command.command(name="tuple-type-autocompletion-cmd")
@click.argument("foo", autocompletion=return_type_tuple_shell_complete)
def tuple_type_autocompletion_cmd(foo):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def root_command():


def test_hidden_cmd():
@root_command.command(hidden=True)
@root_command.command(name="hidden-cmd", hidden=True)
@click.option("--handler", "-h")
def hidden_cmd(handler):
pass
Expand All @@ -22,7 +22,7 @@ def hidden_cmd(handler):


def test_hidden_option():
@root_command.command()
@root_command.command(name="hidden-option-cmd")
@click.option("--handler", "-h", hidden=True)
def hidden_option_cmd(handler):
pass
Expand All @@ -32,7 +32,7 @@ def hidden_option_cmd(handler):


def test_args_of_hidden_command():
@root_command.command(hidden=True)
@root_command.command(name="args-choices-hidden-cmd", hidden=True)
@click.argument("handler1", type=click.Choice(("foo", "bar")))
@click.option("--handler2", type=click.Choice(("foo", "bar")))
def args_choices_hidden_cmd(handler):
Expand All @@ -55,7 +55,7 @@ def test_completion_multilevel_command():
def root_group():
pass

@root_group.group()
@root_group.group(name="first-level-command")
def first_level_command():
pass

Expand Down