1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
import sys
import pytest
import click
def debug():
click.echo(
"{}={}".format(
sys._getframe(1).f_code.co_name, "|".join(click.get_current_context().args)
)
)
def test_basic_chaining(runner):
@click.group(chain=True)
def cli():
pass
@cli.command("sdist")
def sdist():
click.echo("sdist called")
@cli.command("bdist")
def bdist():
click.echo("bdist called")
result = runner.invoke(cli, ["bdist", "sdist", "bdist"])
assert not result.exception
assert result.output.splitlines() == [
"bdist called",
"sdist called",
"bdist called",
]
def test_chaining_help(runner):
@click.group(chain=True)
def cli():
"""ROOT HELP"""
pass
@cli.command("sdist")
def sdist():
"""SDIST HELP"""
click.echo("sdist called")
@cli.command("bdist")
def bdist():
"""BDIST HELP"""
click.echo("bdist called")
result = runner.invoke(cli, ["--help"])
assert not result.exception
assert "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..." in result.output
assert "ROOT HELP" in result.output
result = runner.invoke(cli, ["sdist", "--help"])
assert not result.exception
assert "SDIST HELP" in result.output
result = runner.invoke(cli, ["bdist", "--help"])
assert not result.exception
assert "BDIST HELP" in result.output
result = runner.invoke(cli, ["bdist", "sdist", "--help"])
assert not result.exception
assert "SDIST HELP" in result.output
def test_chaining_with_options(runner):
@click.group(chain=True)
def cli():
pass
@cli.command("sdist")
@click.option("--format")
def sdist(format):
click.echo("sdist called {}".format(format))
@cli.command("bdist")
@click.option("--format")
def bdist(format):
click.echo("bdist called {}".format(format))
result = runner.invoke(cli, ["bdist", "--format=1", "sdist", "--format=2"])
assert not result.exception
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
def test_chaining_with_arguments(runner):
@click.group(chain=True)
def cli():
pass
@cli.command("sdist")
@click.argument("format")
def sdist(format):
click.echo("sdist called {}".format(format))
@cli.command("bdist")
@click.argument("format")
def bdist(format):
click.echo("bdist called {}".format(format))
result = runner.invoke(cli, ["bdist", "1", "sdist", "2"])
assert not result.exception
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
def test_pipeline(runner):
@click.group(chain=True, invoke_without_command=True)
@click.option("-i", "--input", type=click.File("r"))
def cli(input):
pass
@cli.resultcallback()
def process_pipeline(processors, input):
iterator = (x.rstrip("\r\n") for x in input)
for processor in processors:
iterator = processor(iterator)
for item in iterator:
click.echo(item)
@cli.command("uppercase")
def make_uppercase():
def processor(iterator):
for line in iterator:
yield line.upper()
return processor
@cli.command("strip")
def make_strip():
def processor(iterator):
for line in iterator:
yield line.strip()
return processor
result = runner.invoke(cli, ["-i", "-"], input="foo\nbar")
assert not result.exception
assert result.output.splitlines() == ["foo", "bar"]
result = runner.invoke(cli, ["-i", "-", "strip"], input="foo \n bar")
assert not result.exception
assert result.output.splitlines() == ["foo", "bar"]
result = runner.invoke(cli, ["-i", "-", "strip", "uppercase"], input="foo \n bar")
assert not result.exception
assert result.output.splitlines() == ["FOO", "BAR"]
def test_args_and_chain(runner):
@click.group(chain=True)
def cli():
debug()
@cli.command()
def a():
debug()
@cli.command()
def b():
debug()
@cli.command()
def c():
debug()
result = runner.invoke(cli, ["a", "b", "c"])
assert not result.exception
assert result.output.splitlines() == ["cli=", "a=", "b=", "c="]
def test_multicommand_arg_behavior(runner):
with pytest.raises(RuntimeError):
@click.group(chain=True)
@click.argument("forbidden", required=False)
def bad_cli():
pass
with pytest.raises(RuntimeError):
@click.group(chain=True)
@click.argument("forbidden", nargs=-1)
def bad_cli2():
pass
@click.group(chain=True)
@click.argument("arg")
def cli(arg):
click.echo("cli:{}".format(arg))
@cli.command()
def a():
click.echo("a")
result = runner.invoke(cli, ["foo", "a"])
assert not result.exception
assert result.output.splitlines() == ["cli:foo", "a"]
@pytest.mark.xfail
def test_multicommand_chaining(runner):
@click.group(chain=True)
def cli():
debug()
@cli.group()
def l1a():
debug()
@l1a.command()
def l2a():
debug()
@l1a.command()
def l2b():
debug()
@cli.command()
def l1b():
debug()
result = runner.invoke(cli, ["l1a", "l2a", "l1b"])
assert not result.exception
assert result.output.splitlines() == ["cli=", "l1a=", "l2a=", "l1b="]
|