From aa968f0bdad4aa8aae20976108491b449ed0ea9c Mon Sep 17 00:00:00 2001 From: Ghost Ops <72981180+GhostOps77@users.noreply.github.com> Date: Sat, 4 Feb 2023 10:01:18 +0530 Subject: [PATCH] Merged PR #62: click_repl/__init__.py Added some test cases: tests/test_command_collection.py --- click_repl/__init__.py | 7 ++++++- tests/test_command_collection.py | 27 +++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/click_repl/__init__.py b/click_repl/__init__.py index b6ab5b1..37d1b56 100644 --- a/click_repl/__init__.py +++ b/click_repl/__init__.py @@ -265,7 +265,12 @@ def get_command(): continue try: - with group.make_context(None, args, parent=group_ctx) as ctx: + # default_map passes the top-level params to the new group to + # support top-level required params that would reject the + # invocation if missing. + with group.make_context( + None, args, parent=group_ctx, default_map=old_ctx.params + ) as ctx: group.invoke(ctx) ctx.exit() except click.ClickException as e: diff --git a/tests/test_command_collection.py b/tests/test_command_collection.py index c237783..ed0c15f 100644 --- a/tests/test_command_collection.py +++ b/tests/test_command_collection.py @@ -1,9 +1,9 @@ import click -from click_repl import ClickCompleter +from click_repl import ClickCompleter, repl from prompt_toolkit.document import Document -def test_completion(): +def test_command_collection(): @click.group() def foo_group(): pass @@ -24,3 +24,26 @@ def foobar_cmd(): completions = list(c.get_completions(Document("foo"))) assert set(x.text for x in completions) == set(["foo-cmd", "foobar-cmd"]) + + +def test_subcommand_invocation(): + @click.group(invoke_without_command=True) + @click.option("--user", required=True) + @click.pass_context + def cli(ctx, user): + if ctx.invoked_subcommand is None: + click.echo("Top-level user: {}".format(user)) + repl(ctx) + + @cli.command() + @click.option("--user") + def c1(user): + click.echo("Executed C1 with {}!".format(user)) + + c = ClickCompleter(cli) + + completions = list(c.get_completions(Document(" "))) + assert set(x.text for x in completions) == set(["--user", "c1"]) + + completions = list(c.get_completions(Document("c1 "))) + assert set(x.text for x in completions) == set(["--user"])