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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
import click
def test_basic_functionality(runner):
@click.command()
def cli():
"""First paragraph.
This is a very long second
paragraph and not correctly
wrapped but it will be rewrapped.
\b
This is
a paragraph
without rewrapping.
\b
1
2
3
And this is a paragraph
that will be rewrapped again.
"""
result = runner.invoke(cli, ["--help"], terminal_width=60)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
" First paragraph.",
"",
" This is a very long second paragraph and not correctly",
" wrapped but it will be rewrapped.",
"",
" This is",
" a paragraph",
" without rewrapping.",
"",
" 1",
" 2",
" 3",
"",
" And this is a paragraph that will be rewrapped again.",
"",
"Options:",
" --help Show this message and exit.",
]
def test_wrapping_long_options_strings(runner):
@click.group()
def cli():
"""Top level command"""
@cli.group()
def a_very_long():
"""Second level"""
@a_very_long.command()
@click.argument("first")
@click.argument("second")
@click.argument("third")
@click.argument("fourth")
@click.argument("fifth")
@click.argument("sixth")
def command():
"""A command."""
# 54 is chosen as a length where the second line is one character
# longer than the maximum length.
result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli a-very-long command [OPTIONS] FIRST SECOND",
" THIRD FOURTH FIFTH",
" SIXTH",
"",
" A command.",
"",
"Options:",
" --help Show this message and exit.",
]
def test_wrapping_long_command_name(runner):
@click.group()
def cli():
"""Top level command"""
@cli.group()
def a_very_very_very_long():
"""Second level"""
@a_very_very_very_long.command()
@click.argument("first")
@click.argument("second")
@click.argument("third")
@click.argument("fourth")
@click.argument("fifth")
@click.argument("sixth")
def command():
"""A command."""
result = runner.invoke(
cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54
)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli a-very-very-very-long command ",
" [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH",
" SIXTH",
"",
" A command.",
"",
"Options:",
" --help Show this message and exit.",
]
def test_formatting_empty_help_lines(runner):
@click.command()
def cli():
# fmt: off
"""Top level command
"""
# fmt: on
result = runner.invoke(cli, ["--help"])
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
" Top level command",
"",
"",
"",
"Options:",
" --help Show this message and exit.",
]
def test_formatting_usage_error(runner):
@click.command()
@click.argument("arg")
def cmd(arg):
click.echo(f"arg:{arg}")
result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd [OPTIONS] ARG",
"Try 'cmd --help' for help.",
"",
"Error: Missing argument 'ARG'.",
]
def test_formatting_usage_error_metavar_missing_arg(runner):
"""
:author: @r-m-n
Including attribution to #612
"""
@click.command()
@click.argument("arg", metavar="metavar")
def cmd(arg):
pass
result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd [OPTIONS] metavar",
"Try 'cmd --help' for help.",
"",
"Error: Missing argument 'metavar'.",
]
def test_formatting_usage_error_metavar_bad_arg(runner):
@click.command()
@click.argument("arg", type=click.INT, metavar="metavar")
def cmd(arg):
pass
result = runner.invoke(cmd, ["3.14"])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd [OPTIONS] metavar",
"Try 'cmd --help' for help.",
"",
"Error: Invalid value for 'metavar': '3.14' is not a valid integer.",
]
def test_formatting_usage_error_nested(runner):
@click.group()
def cmd():
pass
@cmd.command()
@click.argument("bar")
def foo(bar):
click.echo(f"foo:{bar}")
result = runner.invoke(cmd, ["foo"])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd foo [OPTIONS] BAR",
"Try 'cmd foo --help' for help.",
"",
"Error: Missing argument 'BAR'.",
]
def test_formatting_usage_error_no_help(runner):
@click.command(add_help_option=False)
@click.argument("arg")
def cmd(arg):
click.echo(f"arg:{arg}")
result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd [OPTIONS] ARG",
"",
"Error: Missing argument 'ARG'.",
]
def test_formatting_usage_custom_help(runner):
@click.command(context_settings=dict(help_option_names=["--man"]))
@click.argument("arg")
def cmd(arg):
click.echo(f"arg:{arg}")
result = runner.invoke(cmd, [])
assert result.exit_code == 2
assert result.output.splitlines() == [
"Usage: cmd [OPTIONS] ARG",
"Try 'cmd --man' for help.",
"",
"Error: Missing argument 'ARG'.",
]
def test_formatting_custom_type_metavar(runner):
class MyType(click.ParamType):
def get_metavar(self, param):
return "MY_TYPE"
@click.command("foo")
@click.help_option()
@click.argument("param", type=MyType())
def cmd(param):
pass
result = runner.invoke(cmd, "--help")
assert not result.exception
assert result.output.splitlines() == [
"Usage: foo [OPTIONS] MY_TYPE",
"",
"Options:",
" --help Show this message and exit.",
]
def test_truncating_docstring(runner):
@click.command()
@click.pass_context
def cli(ctx):
"""First paragraph.
This is a very long second
paragraph and not correctly
wrapped but it will be rewrapped.
\f
:param click.core.Context ctx: Click context.
"""
result = runner.invoke(cli, ["--help"], terminal_width=60)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
" First paragraph.",
"",
" This is a very long second paragraph and not correctly",
" wrapped but it will be rewrapped.",
"",
"Options:",
" --help Show this message and exit.",
]
def test_truncating_docstring_no_help(runner):
@click.command()
@click.pass_context
def cli(ctx):
"""
\f
This text should be truncated.
"""
result = runner.invoke(cli, ["--help"], terminal_width=60)
assert not result.exception
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
"Options:",
" --help Show this message and exit.",
]
def test_removing_multiline_marker(runner):
@click.group()
def cli():
pass
@cli.command()
def cmd1():
"""\b
This is command with a multiline help text
which should not be rewrapped.
The output of the short help text should
not contain the multiline marker.
"""
pass
result = runner.invoke(cli, ["--help"])
assert "\b" not in result.output
def test_global_show_default(runner):
@click.command(context_settings=dict(show_default=True))
@click.option("-f", "in_file", default="out.txt", help="Output file name")
def cli():
pass
result = runner.invoke(cli, ["--help"])
# the default to "--help" is not shown because it is False
assert result.output.splitlines() == [
"Usage: cli [OPTIONS]",
"",
"Options:",
" -f TEXT Output file name [default: out.txt]",
" --help Show this message and exit.",
]
def test_formatting_with_options_metavar_empty(runner):
cli = click.Command("cli", options_metavar="", params=[click.Argument(["var"])])
result = runner.invoke(cli, ["--help"])
assert "Usage: cli VAR\n" in result.output
def test_help_formatter_write_text():
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
formatter = click.HelpFormatter(width=len(" Lorem ipsum dolor sit amet,"))
formatter.current_indent = 2
formatter.write_text(text)
actual = formatter.getvalue()
expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n"
assert actual == expected
|