python-click/tests/test_options.py

559 lines
16 KiB
Python
Raw Normal View History

2014-10-16 20:40:34 +02:00
# -*- coding: utf-8 -*-
import os
2020-07-21 08:23:42 +02:00
import re
2014-10-16 20:40:34 +02:00
import pytest
2020-07-21 08:23:42 +02:00
import click
2015-12-04 16:51:02 +01:00
from click._compat import text_type
2014-10-16 20:40:34 +02:00
def test_prefixes(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("++foo", is_flag=True, help="das foo")
@click.option("--bar", is_flag=True, help="das bar")
2014-10-16 20:40:34 +02:00
def cli(foo, bar):
2020-07-21 08:23:42 +02:00
click.echo("foo={} bar={}".format(foo, bar))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["++foo", "--bar"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo=True bar=True\n"
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["--help"])
assert re.search(r"\+\+foo\s+das foo", result.output) is not None
assert re.search(r"--bar\s+das bar", result.output) is not None
2014-10-16 20:40:34 +02:00
def test_invalid_option(runner):
2020-07-21 08:23:42 +02:00
with pytest.raises(TypeError, match="name was passed"):
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("foo")
2014-10-16 20:40:34 +02:00
def cli(foo):
pass
2015-07-16 14:26:14 +02:00
def test_invalid_nargs(runner):
2020-07-21 08:23:42 +02:00
with pytest.raises(TypeError, match="nargs < 0"):
2015-07-16 14:26:14 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", nargs=-1)
2015-07-16 14:26:14 +02:00
def cli(foo):
pass
def test_nargs_tup_composite_mult(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--item", type=(str, int), multiple=True)
2015-07-16 14:26:14 +02:00
def copy(item):
for item in item:
2020-07-21 08:23:42 +02:00
click.echo("name={0[0]} id={0[1]:d}".format(item))
2015-07-16 14:26:14 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(copy, ["--item", "peter", "1", "--item", "max", "2"])
2015-07-16 14:26:14 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output.splitlines() == ["name=peter id=1", "name=max id=2"]
2015-07-16 14:26:14 +02:00
2014-10-16 20:40:34 +02:00
def test_counting(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("-v", count=True, help="Verbosity", type=click.IntRange(0, 3))
2014-10-16 20:40:34 +02:00
def cli(v):
2020-07-21 08:23:42 +02:00
click.echo("verbosity={:d}".format(v))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-vvv"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "verbosity=3\n"
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-vvvv"])
2014-10-16 20:40:34 +02:00
assert result.exception
2020-07-21 08:23:42 +02:00
assert (
"Invalid value for '-v': 4 is not in the valid range of 0 to 3."
2014-10-16 20:40:34 +02:00
in result.output
2020-07-21 08:23:42 +02:00
)
2014-10-16 20:40:34 +02:00
result = runner.invoke(cli, [])
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "verbosity=0\n"
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["--help"])
assert re.search(r"-v\s+Verbosity", result.output) is not None
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@pytest.mark.parametrize("unknown_flag", ["--foo", "-f"])
2014-10-16 20:40:34 +02:00
def test_unknown_options(runner, unknown_flag):
@click.command()
def cli():
pass
result = runner.invoke(cli, [unknown_flag])
assert result.exception
2020-07-21 08:23:42 +02:00
assert "no such option: {}".format(unknown_flag) in result.output
2014-10-16 20:40:34 +02:00
def test_multiple_required(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("-m", "--message", multiple=True, required=True)
2014-10-16 20:40:34 +02:00
def cli(message):
2020-07-21 08:23:42 +02:00
click.echo("\n".join(message))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["-m", "foo", "-mbar"])
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo\nbar\n"
2014-10-16 20:40:34 +02:00
result = runner.invoke(cli, [])
assert result.exception
2020-07-21 08:23:42 +02:00
assert "Error: Missing option '-m' / '--message'." in result.output
def test_empty_envvar(runner):
@click.command()
@click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
def cli(mypath):
click.echo("mypath: {}".format(mypath))
result = runner.invoke(cli, [], env={"MYPATH": ""})
assert result.exit_code == 0
assert result.output == "mypath: None\n"
2014-10-16 20:40:34 +02:00
def test_multiple_envvar(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg", multiple=True)
2014-10-16 20:40:34 +02:00
def cmd(arg):
2020-07-21 08:23:42 +02:00
click.echo("|".join(arg))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar baz"}
)
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo|bar|baz\n"
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg", multiple=True, envvar="X")
2014-10-16 20:40:34 +02:00
def cmd(arg):
2020-07-21 08:23:42 +02:00
click.echo("|".join(arg))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, [], env={"X": "foo bar baz"})
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo|bar|baz\n"
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg", multiple=True, type=click.Path())
2014-10-16 20:40:34 +02:00
def cmd(arg):
2020-07-21 08:23:42 +02:00
click.echo("|".join(arg))
result = runner.invoke(
cmd,
[],
auto_envvar_prefix="TEST",
env={"TEST_ARG": "foo{}bar".format(os.path.pathsep)},
)
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo|bar\n"
2014-10-16 20:40:34 +02:00
2015-07-16 14:26:14 +02:00
def test_multiple_default_help(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg1", multiple=True, default=("foo", "bar"), show_default=True)
@click.option("--arg2", multiple=True, default=(1, 2), type=int, show_default=True)
2015-07-16 14:26:14 +02:00
def cmd(arg, arg2):
pass
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"])
2015-07-16 14:26:14 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "foo, bar" in result.output
assert "1, 2" in result.output
2015-12-04 16:51:02 +01:00
2015-07-16 14:26:14 +02:00
def test_multiple_default_type(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg1", multiple=True, default=("foo", "bar"))
@click.option("--arg2", multiple=True, default=(1, "a"))
2015-07-16 14:26:14 +02:00
def cmd(arg1, arg2):
2015-12-04 16:51:02 +01:00
assert all(isinstance(e[0], text_type) for e in arg1)
assert all(isinstance(e[1], text_type) for e in arg1)
2015-07-16 14:26:14 +02:00
2015-12-04 16:51:02 +01:00
assert all(isinstance(e[0], int) for e in arg2)
assert all(isinstance(e[1], text_type) for e in arg2)
2015-07-16 14:26:14 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(
cmd, "--arg1 a b --arg1 test 1 --arg2 2 two --arg2 4 four".split()
)
2015-07-16 14:26:14 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
2018-09-06 20:55:10 +02:00
def test_dynamic_default_help_unset(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option(
"--username",
prompt=True,
default=lambda: os.environ.get("USER", ""),
show_default=True,
)
2018-09-06 20:55:10 +02:00
def cmd(username):
print("Hello,", username)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert "--username" in result.output
assert "lambda" not in result.output
assert "(dynamic)" in result.output
2018-09-06 20:55:10 +02:00
def test_dynamic_default_help_text(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option(
"--username",
prompt=True,
default=lambda: os.environ.get("USER", ""),
show_default="current user",
)
2018-09-06 20:55:10 +02:00
def cmd(username):
print("Hello,", username)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert "--username" in result.output
assert "lambda" not in result.output
assert "(current user)" in result.output
2015-12-04 16:51:02 +01:00
2019-01-07 17:51:19 +01:00
def test_toupper_envvar_prefix(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg")
2019-01-07 17:51:19 +01:00
def cmd(arg):
click.echo(arg)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, [], auto_envvar_prefix="test", env={"TEST_ARG": "foo"})
2019-01-07 17:51:19 +01:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo\n"
2019-01-07 17:51:19 +01:00
2014-10-16 20:40:34 +02:00
def test_nargs_envvar(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg", nargs=2)
2014-10-16 20:40:34 +02:00
def cmd(arg):
2020-07-21 08:23:42 +02:00
click.echo("|".join(arg))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}
)
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "foo|bar\n"
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg", nargs=2, multiple=True)
2014-10-16 20:40:34 +02:00
def cmd(arg):
for item in arg:
2020-07-21 08:23:42 +02:00
click.echo("|".join(item))
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(
cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "x 1 y 2"}
)
2014-10-16 20:40:34 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert result.output == "x|1\ny|2\n"
2014-10-16 20:40:34 +02:00
2018-09-06 20:55:10 +02:00
def test_show_envvar(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg1", envvar="ARG1", show_envvar=True)
2018-09-06 20:55:10 +02:00
def cmd(arg):
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
2020-07-21 08:23:42 +02:00
assert "ARG1" in result.output
2018-09-06 20:55:10 +02:00
def test_show_envvar_auto_prefix(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--arg1", show_envvar=True)
2018-09-06 20:55:10 +02:00
def cmd(arg):
pass
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"], auto_envvar_prefix="TEST")
assert not result.exception
assert "TEST_ARG1" in result.output
def test_show_envvar_auto_prefix_dash_in_command(runner):
@click.group()
def cli():
pass
@cli.command()
@click.option("--baz", show_envvar=True)
def foo_bar(baz):
pass
result = runner.invoke(cli, ["foo-bar", "--help"], auto_envvar_prefix="TEST")
2018-09-06 20:55:10 +02:00
assert not result.exception
2020-07-21 08:23:42 +02:00
assert "TEST_FOO_BAR_BAZ" in result.output
2018-09-06 20:55:10 +02:00
2014-10-16 20:40:34 +02:00
def test_custom_validation(runner):
2018-09-06 20:55:10 +02:00
def validate_pos_int(ctx, param, value):
2014-10-16 20:40:34 +02:00
if value < 0:
2020-07-21 08:23:42 +02:00
raise click.BadParameter("Value needs to be positive")
2014-10-16 20:40:34 +02:00
return value
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", callback=validate_pos_int, default=1)
2014-10-16 20:40:34 +02:00
def cmd(foo):
click.echo(foo)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "-1"])
assert "Invalid value for '--foo': Value needs to be positive" in result.output
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "42"])
assert result.output == "42\n"
2014-10-16 20:40:34 +02:00
def test_winstyle_options(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("/debug;/no-debug", help="Enables or disables debug mode.")
2014-10-16 20:40:34 +02:00
def cmd(debug):
click.echo(debug)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["/debug"], help_option_names=["/?"])
assert result.output == "True\n"
result = runner.invoke(cmd, ["/no-debug"], help_option_names=["/?"])
assert result.output == "False\n"
result = runner.invoke(cmd, [], help_option_names=["/?"])
assert result.output == "False\n"
result = runner.invoke(cmd, ["/?"], help_option_names=["/?"])
assert "/debug; /no-debug Enables or disables debug mode." in result.output
assert "/? Show this message and exit." in result.output
2014-10-16 20:40:34 +02:00
def test_legacy_options(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("-whatever")
2014-10-16 20:40:34 +02:00
def cmd(whatever):
click.echo(whatever)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["-whatever", "42"])
assert result.output == "42\n"
result = runner.invoke(cmd, ["-whatever=23"])
assert result.output == "23\n"
def test_missing_option_string_cast():
ctx = click.Context(click.Command(""))
with pytest.raises(click.MissingParameter) as excinfo:
click.Option(["-a"], required=True).full_process_value(ctx, None)
assert str(excinfo.value) == "missing parameter: a"
2014-10-16 20:40:34 +02:00
def test_missing_choice(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)
2014-10-16 20:40:34 +02:00
def cmd(foo):
click.echo(foo)
result = runner.invoke(cmd)
assert result.exit_code == 2
2020-07-21 08:23:42 +02:00
error, separator, choices = result.output.partition("Choose from")
assert "Error: Missing option '--foo'. " in error
assert "Choose from" in separator
assert "foo" in choices
assert "bar" in choices
2018-09-06 20:55:10 +02:00
def test_case_insensitive_choice(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
2018-09-06 20:55:10 +02:00
def cmd(foo):
click.echo(foo)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "apple"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert result.output == "Apple\n"
2018-09-06 20:55:10 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "oRANGe"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert result.output == "Orange\n"
2018-09-06 20:55:10 +02:00
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "Apple"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert result.output == "Apple\n"
2018-09-06 20:55:10 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", type=click.Choice(["Orange", "Apple"]))
2018-09-06 20:55:10 +02:00
def cmd2(foo):
click.echo(foo)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd2, ["--foo", "apple"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 2
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd2, ["--foo", "oRANGe"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 2
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd2, ["--foo", "Apple"])
2018-09-06 20:55:10 +02:00
assert result.exit_code == 0
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
def test_case_insensitive_choice_returned_exactly(runner):
2014-10-16 20:40:34 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
2014-10-16 20:40:34 +02:00
def cmd(foo):
click.echo(foo)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--foo", "apple"])
assert result.exit_code == 0
assert result.output == "Apple\n"
def test_option_help_preserve_paragraphs(runner):
@click.command()
@click.option(
"-C",
"--config",
type=click.Path(),
help="""Configuration file to use.
If not given, the environment variable CONFIG_FILE is consulted
and used if set. If neither are given, a default configuration
file is loaded.""",
)
def cmd(config):
pass
result = runner.invoke(cmd, ["--help"],)
2014-10-16 20:40:34 +02:00
assert result.exit_code == 0
2020-07-21 08:23:42 +02:00
assert (
" -C, --config PATH Configuration file to use.\n"
"{i}\n"
"{i}If not given, the environment variable CONFIG_FILE is\n"
"{i}consulted and used if set. If neither are given, a default\n"
"{i}configuration file is loaded.".format(i=" " * 21)
) in result.output
2015-07-16 14:26:14 +02:00
def test_argument_custom_class(runner):
class CustomArgument(click.Argument):
def get_default(self, ctx):
2020-07-21 08:23:42 +02:00
"""a dumb override of a default value for testing"""
return "I am a default"
2015-07-16 14:26:14 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.argument("testarg", cls=CustomArgument, default="you wont see me")
2015-07-16 14:26:14 +02:00
def cmd(testarg):
click.echo(testarg)
result = runner.invoke(cmd)
2020-07-21 08:23:42 +02:00
assert "I am a default" in result.output
assert "you wont see me" not in result.output
2015-07-16 14:26:14 +02:00
def test_option_custom_class(runner):
class CustomOption(click.Option):
def get_help_record(self, ctx):
2020-07-21 08:23:42 +02:00
"""a dumb override of a help text for testing"""
return ("--help", "I am a help text")
2015-07-16 14:26:14 +02:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--testoption", cls=CustomOption, help="you wont see me")
2015-07-16 14:26:14 +02:00
def cmd(testoption):
click.echo(testoption)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"])
assert "I am a help text" in result.output
assert "you wont see me" not in result.output
2015-12-04 16:51:02 +01:00
2018-09-06 20:55:10 +02:00
def test_option_custom_class_reusable(runner):
"""Ensure we can reuse a custom class option. See Issue #926"""
class CustomOption(click.Option):
def get_help_record(self, ctx):
2020-07-21 08:23:42 +02:00
"""a dumb override of a help text for testing"""
return ("--help", "I am a help text")
2018-09-06 20:55:10 +02:00
# Assign to a variable to re-use the decorator.
2020-07-21 08:23:42 +02:00
testoption = click.option("--testoption", cls=CustomOption, help="you wont see me")
2018-09-06 20:55:10 +02:00
@click.command()
@testoption
def cmd1(testoption):
click.echo(testoption)
@click.command()
@testoption
def cmd2(testoption):
click.echo(testoption)
# Both of the commands should have the --help option now.
for cmd in (cmd1, cmd2):
2020-07-21 08:23:42 +02:00
result = runner.invoke(cmd, ["--help"])
assert "I am a help text" in result.output
assert "you wont see me" not in result.output
def test_bool_flag_with_type(runner):
@click.command()
@click.option("--shout/--no-shout", default=False, type=bool)
def cmd(shout):
pass
result = runner.invoke(cmd)
assert not result.exception
2018-09-06 20:55:10 +02:00
2015-12-04 16:51:02 +01:00
def test_aliases_for_flags(runner):
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--warnings/--no-warnings", " /-W", default=True)
2015-12-04 16:51:02 +01:00
def cli(warnings):
click.echo(warnings)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli, ["--warnings"])
assert result.output == "True\n"
result = runner.invoke(cli, ["--no-warnings"])
assert result.output == "False\n"
result = runner.invoke(cli, ["-W"])
assert result.output == "False\n"
2015-12-04 16:51:02 +01:00
@click.command()
2020-07-21 08:23:42 +02:00
@click.option("--warnings/--no-warnings", "-w", default=True)
2015-12-04 16:51:02 +01:00
def cli_alt(warnings):
click.echo(warnings)
2020-07-21 08:23:42 +02:00
result = runner.invoke(cli_alt, ["--warnings"])
assert result.output == "True\n"
result = runner.invoke(cli_alt, ["--no-warnings"])
assert result.output == "False\n"
result = runner.invoke(cli_alt, ["-w"])
assert result.output == "True\n"
@pytest.mark.parametrize(
"option_args,expected",
[
(["--aggressive", "--all", "-a"], "aggressive"),
(["--first", "--second", "--third", "-a", "-b", "-c"], "first"),
(["--apple", "--banana", "--cantaloupe", "-a", "-b", "-c"], "apple"),
(["--cantaloupe", "--banana", "--apple", "-c", "-b", "-a"], "cantaloupe"),
(["-a", "-b", "-c"], "a"),
(["-c", "-b", "-a"], "c"),
(["-a", "--apple", "-b", "--banana", "-c", "--cantaloupe"], "apple"),
(["-c", "-a", "--cantaloupe", "-b", "--banana", "--apple"], "cantaloupe"),
(["--from", "-f", "_from"], "_from"),
(["--return", "-r", "_ret"], "_ret"),
],
)
2018-09-06 20:55:10 +02:00
def test_option_names(runner, option_args, expected):
@click.command()
@click.option(*option_args, is_flag=True)
def cmd(**kwargs):
click.echo(str(kwargs[expected]))
assert cmd.params[0].name == expected
for form in option_args:
2020-07-21 08:23:42 +02:00
if form.startswith("-"):
2018-09-06 20:55:10 +02:00
result = runner.invoke(cmd, [form])
2020-07-21 08:23:42 +02:00
assert result.output == "True\n"