Skip to content

Commit f3694c7

Browse files
flacosteauvipy
authored andcommitted
Delegate command dispatching to the actual group command.
1 parent 369405d commit f3694c7

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

click_repl/_repl.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ def get_command():
109109
break
110110

111111
try:
112-
# default_map passes the top-level params to the new group to
113-
# support top-level required params that would reject the
114-
# invocation if missing.
115-
with group.make_context(
116-
None, args, parent=group_ctx, default_map=old_ctx.params
117-
) as ctx:
118-
group.invoke(ctx)
119-
ctx.exit()
120-
112+
# The group command will dispatch based on args.
113+
old_protected_args = group_ctx.protected_args
114+
try:
115+
group_ctx.protected_args = args
116+
group.invoke(group_ctx)
117+
finally:
118+
group_ctx.protected_args = old_protected_args
121119
except click.ClickException as e:
122120
e.show()
123121
except (ClickExit, SystemExit):

tests/test_repl.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ def foo():
5656
assert capsys.readouterr().out == "Foo!\n"
5757

5858

59-
def test_group_command_called_once(capsys):
59+
def test_group_command_called_on_each_subcommands(capsys):
6060
@click.group(invoke_without_command=True)
6161
@click.pass_context
6262
def cli(ctx):
63-
print("cli()")
6463
if ctx.invoked_subcommand is None:
6564
click_repl.repl(ctx)
65+
else:
66+
print("cli()")
6667

6768
@cli.command()
6869
def foo():
@@ -74,27 +75,52 @@ def bar():
7475

7576
with mock_stdin("foo\nbar\n"):
7677
with pytest.raises(SystemExit):
77-
cli(args=[], prog_name="test_group_called_once")
78-
assert capsys.readouterr().out == "cli()\nFoo!\nBar!\n"
78+
cli(args=[], prog_name="test_group_command_called_on_each_subcommands")
79+
assert capsys.readouterr().out == "cli()\nFoo!\ncli()\nBar!\n"
7980

8081

81-
def test_independant_args(capsys):
82+
def test_group_argument_are_preserved(capsys):
8283
@click.group(invoke_without_command=True)
8384
@click.argument("argument")
8485
@click.pass_context
8586
def cli(ctx, argument):
86-
print("cli(%s)" % argument)
8787
if ctx.invoked_subcommand is None:
8888
click_repl.repl(ctx)
89+
else:
90+
print("cli(%s)" % argument)
91+
92+
@cli.command()
93+
@click.argument("argument")
94+
def foo(argument):
95+
print("Foo: %s!" % argument)
96+
97+
with mock_stdin("foo bar\n"):
98+
with pytest.raises(SystemExit):
99+
cli(args=["arg"], prog_name="test_group_argument_are_preserved")
100+
assert capsys.readouterr().out == "cli(arg)\nFoo: bar!\n"
101+
102+
103+
def test_chain_commands(capsys):
104+
@click.group(invoke_without_command=True, chain=True)
105+
@click.pass_context
106+
def cli(ctx):
107+
if ctx.invoked_subcommand is None:
108+
click_repl.repl(ctx)
109+
else:
110+
print("cli()")
89111

90112
@cli.command()
91113
def foo():
92114
print("Foo!")
93115

94-
with mock_stdin("foo\n"):
116+
@cli.command()
117+
def bar():
118+
print("Bar!")
119+
120+
with mock_stdin("foo bar\n"):
95121
with pytest.raises(SystemExit):
96-
cli(args=["command-line-argument"], prog_name="test_group_called_once")
97-
assert capsys.readouterr().out == "cli(command-line-argument)\nFoo!\n"
122+
cli(args=[], prog_name="test_chain_commands")
123+
assert capsys.readouterr().out == "cli()\nFoo!\nBar!\n"
98124

99125

100126
def test_exit_repl_function():

0 commit comments

Comments
 (0)