2014-10-16 20:40:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"], terminal_width=60)
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"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.",
|
2014-10-16 20:40:34 +02:00
|
|
|
]
|
2015-07-16 14:26:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("first")
|
|
|
|
@click.argument("second")
|
|
|
|
@click.argument("third")
|
|
|
|
@click.argument("fourth")
|
|
|
|
@click.argument("fifth")
|
|
|
|
@click.argument("sixth")
|
2015-07-16 14:26:14 +02:00
|
|
|
def command():
|
|
|
|
"""A command.
|
|
|
|
"""
|
|
|
|
|
2015-08-23 03:10:31 +02:00
|
|
|
# 54 is chosen as a length where the second line is one character
|
2015-07-16 14:26:14 +02:00
|
|
|
# longer than the maximum length.
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)
|
2015-07-16 14:26:14 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cli a-very-long command [OPTIONS] FIRST SECOND",
|
|
|
|
" THIRD FOURTH FIFTH",
|
|
|
|
" SIXTH",
|
|
|
|
"",
|
|
|
|
" A command.",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --help Show this message and exit.",
|
2015-07-16 14:26:14 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2015-08-23 03:10:31 +02:00
|
|
|
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()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("first")
|
|
|
|
@click.argument("second")
|
|
|
|
@click.argument("third")
|
|
|
|
@click.argument("fourth")
|
|
|
|
@click.argument("fifth")
|
|
|
|
@click.argument("sixth")
|
2015-08-23 03:10:31 +02:00
|
|
|
def command():
|
|
|
|
"""A command.
|
|
|
|
"""
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54
|
|
|
|
)
|
2015-08-23 03:10:31 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cli a-very-very-very-long command ",
|
|
|
|
" [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH",
|
|
|
|
" SIXTH",
|
|
|
|
"",
|
|
|
|
" A command.",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --help Show this message and exit.",
|
2015-08-23 03:10:31 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2015-07-16 14:26:14 +02:00
|
|
|
def test_formatting_empty_help_lines(runner):
|
|
|
|
@click.command()
|
|
|
|
def cli():
|
|
|
|
"""Top level command
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["--help"])
|
2015-07-16 14:26:14 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cli [OPTIONS]",
|
|
|
|
"",
|
|
|
|
" Top level command",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --help Show this message and exit.",
|
2015-07-16 14:26:14 +02:00
|
|
|
]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_error(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("arg")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cmd(arg):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("arg:{}".format(arg))
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cmd, [])
|
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd [OPTIONS] ARG",
|
|
|
|
"Try 'cmd --help' for help.",
|
|
|
|
"",
|
|
|
|
"Error: Missing argument 'ARG'.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_error_metavar_missing_arg(runner):
|
|
|
|
"""
|
|
|
|
:author: @r-m-n
|
|
|
|
Including attribution to #612
|
|
|
|
"""
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("arg", metavar="metavar")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cmd(arg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
result = runner.invoke(cmd, [])
|
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd [OPTIONS] metavar",
|
|
|
|
"Try 'cmd --help' for help.",
|
|
|
|
"",
|
|
|
|
"Error: Missing argument 'metavar'.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_error_metavar_bad_arg(runner):
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("arg", type=click.INT, metavar="metavar")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cmd(arg):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cmd, ["3.14"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd [OPTIONS] metavar",
|
|
|
|
"Try 'cmd --help' for help.",
|
|
|
|
"",
|
|
|
|
"Error: Invalid value for 'metavar': 3.14 is not a valid integer",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_error_nested(runner):
|
|
|
|
@click.group()
|
|
|
|
def cmd():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cmd.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("bar")
|
2018-09-06 20:55:10 +02:00
|
|
|
def foo(bar):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("foo:{}".format(bar))
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cmd, ["foo"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd foo [OPTIONS] BAR",
|
|
|
|
"Try 'cmd foo --help' for help.",
|
|
|
|
"",
|
|
|
|
"Error: Missing argument 'BAR'.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_error_no_help(runner):
|
|
|
|
@click.command(add_help_option=False)
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("arg")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cmd(arg):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("arg:{}".format(arg))
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cmd, [])
|
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd [OPTIONS] ARG",
|
|
|
|
"",
|
|
|
|
"Error: Missing argument 'ARG'.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_custom_help(runner):
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.command(context_settings=dict(help_option_names=["--man"]))
|
|
|
|
@click.argument("arg")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cmd(arg):
|
2020-07-21 08:23:42 +02:00
|
|
|
click.echo("arg:{}".format(arg))
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
result = runner.invoke(cmd, [])
|
|
|
|
assert result.exit_code == 2
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: cmd [OPTIONS] ARG",
|
|
|
|
"Try 'cmd --man' for help.",
|
|
|
|
"",
|
|
|
|
"Error: Missing argument 'ARG'.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
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
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cmd, "--help")
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: foo [OPTIONS] MY_TYPE",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --help Show this message and exit.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
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_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"],)
|
|
|
|
assert result.output.splitlines() == [
|
|
|
|
"Usage: cli [OPTIONS]",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" -f TEXT Output file name [default: out.txt]",
|
|
|
|
" --help Show this message and exit. [default: False]",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_multiline_option_padding(runner):
|
|
|
|
@click.command("foo")
|
|
|
|
@click.option("--bar", help="This help message will be padded if it wraps.")
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
result = runner.invoke(cli, "--help", terminal_width=45)
|
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
|
|
|
"Usage: foo [OPTIONS]",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --bar TEXT This help message will be",
|
|
|
|
" padded if it wraps.",
|
|
|
|
"",
|
|
|
|
" --help Show this message and exit.",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_formatting_usage_no_option_padding(runner):
|
|
|
|
@click.command("foo")
|
|
|
|
@click.option("--bar", help="This help message will be padded if it wraps.")
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
result = runner.invoke(cli, "--help", terminal_width=80)
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
|
|
|
assert result.output.splitlines() == [
|
2020-07-21 08:23:42 +02:00
|
|
|
"Usage: foo [OPTIONS]",
|
|
|
|
"",
|
|
|
|
"Options:",
|
|
|
|
" --bar TEXT This help message will be padded if it wraps.",
|
|
|
|
" --help Show this message and exit.",
|
2018-09-06 20:55:10 +02:00
|
|
|
]
|