2016-04-06 18:13:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-07-21 08:23:42 +02:00
|
|
|
import pytest
|
2016-04-06 18:13:57 +02:00
|
|
|
|
|
|
|
import click
|
|
|
|
from click._bashcomplete import get_choices
|
|
|
|
|
2017-07-19 20:06:01 +02:00
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
def choices_without_help(cli, args, incomplete):
|
2020-07-21 08:23:42 +02:00
|
|
|
completions = get_choices(cli, "dummy", args, incomplete)
|
2018-09-06 20:55:10 +02:00
|
|
|
return [c[0] for c in completions]
|
|
|
|
|
|
|
|
|
|
|
|
def choices_with_help(cli, args, incomplete):
|
2020-07-21 08:23:42 +02:00
|
|
|
return list(get_choices(cli, "dummy", args, incomplete))
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
2017-07-19 20:06:01 +02:00
|
|
|
def test_single_command():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--local-opt")
|
2017-07-19 20:06:01 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--local-opt"]
|
|
|
|
assert choices_without_help(cli, [], "") == []
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_boolean_flag():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--shout/--no-shout", default=False)
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_value_option():
|
|
|
|
@click.group()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--pos", nargs=2, type=float)
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--local-opt")
|
2018-09-06 20:55:10 +02:00
|
|
|
def sub(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--pos"]
|
|
|
|
assert choices_without_help(cli, ["--pos"], "") == []
|
|
|
|
assert choices_without_help(cli, ["--pos", "1.0"], "") == []
|
|
|
|
assert choices_without_help(cli, ["--pos", "1.0", "1.0"], "") == ["sub"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_option():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--message", "-m", multiple=True)
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--message", "-m"]
|
|
|
|
assert choices_without_help(cli, ["-m"], "") == []
|
2017-07-19 20:06:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_small_chain():
|
2016-04-06 18:13:57 +02:00
|
|
|
@click.group()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--global-opt")
|
2016-04-06 18:13:57 +02:00
|
|
|
def cli(global_opt):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--local-opt")
|
2016-04-06 18:13:57 +02:00
|
|
|
def sub(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "") == ["sub"]
|
|
|
|
assert choices_without_help(cli, [], "-") == ["--global-opt"]
|
|
|
|
assert choices_without_help(cli, ["sub"], "") == []
|
|
|
|
assert choices_without_help(cli, ["sub"], "-") == ["--local-opt"]
|
2017-07-19 20:06:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_long_chain():
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.group("cli")
|
|
|
|
@click.option("--cli-opt")
|
2017-07-19 20:06:01 +02:00
|
|
|
def cli(cli_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
@cli.group("asub")
|
|
|
|
@click.option("--asub-opt")
|
2017-07-19 20:06:01 +02:00
|
|
|
def asub(asub_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
@asub.group("bsub")
|
|
|
|
@click.option("--bsub-opt")
|
2017-07-19 20:06:01 +02:00
|
|
|
def bsub(bsub_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
COLORS = ["red", "green", "blue"]
|
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
def get_colors(ctx, args, incomplete):
|
|
|
|
for c in COLORS:
|
|
|
|
if c.startswith(incomplete):
|
|
|
|
yield c
|
|
|
|
|
|
|
|
def search_colors(ctx, args, incomplete):
|
|
|
|
for c in COLORS:
|
|
|
|
if incomplete in c:
|
|
|
|
yield c
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
CSUB_OPT_CHOICES = ["foo", "bar"]
|
|
|
|
CSUB_CHOICES = ["bar", "baz"]
|
|
|
|
|
|
|
|
@bsub.command("csub")
|
|
|
|
@click.option("--csub-opt", type=click.Choice(CSUB_OPT_CHOICES))
|
|
|
|
@click.option("--csub", type=click.Choice(CSUB_CHOICES))
|
|
|
|
@click.option("--search-color", autocompletion=search_colors)
|
|
|
|
@click.argument("color", autocompletion=get_colors)
|
2018-09-06 20:55:10 +02:00
|
|
|
def csub(csub_opt, color):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--cli-opt"]
|
|
|
|
assert choices_without_help(cli, [], "") == ["asub"]
|
|
|
|
assert choices_without_help(cli, ["asub"], "-") == ["--asub-opt"]
|
|
|
|
assert choices_without_help(cli, ["asub"], "") == ["bsub"]
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub"], "-") == ["--bsub-opt"]
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub"], "") == ["csub"]
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub", "csub"], "-") == [
|
|
|
|
"--csub-opt",
|
|
|
|
"--csub",
|
|
|
|
"--search-color",
|
|
|
|
]
|
|
|
|
assert (
|
|
|
|
choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "")
|
|
|
|
== CSUB_OPT_CHOICES
|
|
|
|
)
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub", "csub"], "--csub") == [
|
|
|
|
"--csub-opt",
|
|
|
|
"--csub",
|
|
|
|
]
|
|
|
|
assert (
|
|
|
|
choices_without_help(cli, ["asub", "bsub", "csub", "--csub"], "")
|
|
|
|
== CSUB_CHOICES
|
|
|
|
)
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "f") == [
|
|
|
|
"foo"
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub", "csub"], "") == COLORS
|
|
|
|
assert choices_without_help(cli, ["asub", "bsub", "csub"], "b") == ["blue"]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["asub", "bsub", "csub", "--search-color"], "een"
|
|
|
|
) == ["green"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_chaining():
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.group("cli", chain=True)
|
|
|
|
@click.option("--cli-opt")
|
|
|
|
@click.argument("arg", type=click.Choice(["cliarg1", "cliarg2"]))
|
2019-01-07 17:51:19 +01:00
|
|
|
def cli(cli_opt, arg):
|
2018-09-06 20:55:10 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--asub-opt")
|
2018-09-06 20:55:10 +02:00
|
|
|
def asub(asub_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
@cli.command(help="bsub help")
|
|
|
|
@click.option("--bsub-opt")
|
|
|
|
@click.argument("arg", type=click.Choice(["arg1", "arg2"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def bsub(bsub_opt, arg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--csub-opt")
|
|
|
|
@click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")
|
2018-09-06 20:55:10 +02:00
|
|
|
def csub(csub_opt, arg):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--cli-opt"]
|
|
|
|
assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub"], "-") == ["--asub-opt"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"
|
|
|
|
) == ["--bsub-opt"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == ["--bsub-opt"]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [
|
|
|
|
"carg1",
|
|
|
|
"carg2",
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [
|
|
|
|
"carg1",
|
|
|
|
"carg2",
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == ["--csub-opt"]
|
|
|
|
assert choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_argument_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.argument("arg1", required=True, type=click.Choice(["arg11", "arg12"]))
|
|
|
|
@click.argument("arg2", type=click.Choice(["arg21", "arg22"]), default="arg21")
|
|
|
|
@click.argument("arg3", type=click.Choice(["arg", "argument"]), default="arg")
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "") == ["arg11", "arg12"]
|
|
|
|
assert choices_without_help(cli, [], "arg") == ["arg11", "arg12"]
|
|
|
|
assert choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]
|
|
|
|
assert choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]
|
|
|
|
assert choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_option_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--opt1", type=click.Choice(["opt11", "opt12"]), help="opt1 help")
|
|
|
|
@click.option("--opt2", type=click.Choice(["opt21", "opt22"]), default="opt21")
|
|
|
|
@click.option("--opt3", type=click.Choice(["opt", "option"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_with_help(cli, [], "-") == [
|
|
|
|
("--opt1", "opt1 help"),
|
|
|
|
("--opt2", None),
|
|
|
|
("--opt3", None),
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]
|
|
|
|
assert choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]
|
|
|
|
assert choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]
|
|
|
|
assert choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]
|
|
|
|
assert choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]
|
|
|
|
assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]
|
|
|
|
assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]
|
|
|
|
assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [
|
|
|
|
"opt21",
|
|
|
|
"opt22",
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, ["--opt2", "opt21"], "-") == ["--opt1", "--opt3"]
|
|
|
|
assert choices_without_help(cli, ["--opt1", "opt11"], "-") == ["--opt2", "--opt3"]
|
|
|
|
assert choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]
|
|
|
|
assert choices_without_help(cli, ["--opt3"], "opti") == ["option"]
|
|
|
|
|
|
|
|
assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [
|
|
|
|
"--opt2",
|
|
|
|
"--opt3",
|
|
|
|
]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_option_and_arg_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--opt1", type=click.Choice(["opt11", "opt12"]))
|
|
|
|
@click.argument("arg1", required=False, type=click.Choice(["arg11", "arg12"]))
|
|
|
|
@click.option("--opt2", type=click.Choice(["opt21", "opt22"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]
|
|
|
|
assert choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]
|
|
|
|
assert choices_without_help(cli, [], "") == ["arg11", "arg12"]
|
|
|
|
assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]
|
|
|
|
assert choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]
|
|
|
|
assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_boolean_flag_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--shout/--no-shout", default=False)
|
|
|
|
@click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]
|
|
|
|
assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_value_option_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))
|
|
|
|
@click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]
|
|
|
|
assert choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]
|
|
|
|
assert choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]
|
|
|
|
assert choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_option_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))
|
|
|
|
@click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["-m"], "") == ["m1", "m2"]
|
|
|
|
assert choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]
|
|
|
|
assert choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_variadic_argument_choice():
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--opt", type=click.Choice(["opt1", "opt2"]))
|
|
|
|
@click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))
|
2018-09-06 20:55:10 +02:00
|
|
|
def cli(local_opt):
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]
|
|
|
|
assert choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]
|
|
|
|
assert choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]
|
|
|
|
assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_variadic_argument_complete():
|
|
|
|
def _complete(ctx, args, incomplete):
|
2020-07-21 08:23:42 +02:00
|
|
|
return ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"]
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def entrypoint():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@click.command()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--opt", autocompletion=_complete)
|
|
|
|
@click.argument("arg", nargs=-1)
|
2019-01-07 17:51:19 +01:00
|
|
|
def subcommand(opt, arg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
entrypoint.add_command(subcommand)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(entrypoint, ["subcommand", "--opt"], "") == _complete(
|
|
|
|
0, 0, 0
|
|
|
|
)
|
|
|
|
assert choices_without_help(
|
|
|
|
entrypoint, ["subcommand", "whatever", "--opt"], ""
|
|
|
|
) == _complete(0, 0, 0)
|
|
|
|
assert (
|
|
|
|
choices_without_help(entrypoint, ["subcommand", "whatever", "--opt", "abc"], "")
|
|
|
|
== []
|
|
|
|
)
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_long_chain_choice():
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.group()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--sub-opt", type=click.Choice(["subopt1", "subopt2"]))
|
|
|
|
@click.argument(
|
|
|
|
"sub-arg", required=False, type=click.Choice(["subarg1", "subarg2"])
|
|
|
|
)
|
2019-01-07 17:51:19 +01:00
|
|
|
def sub(sub_opt, sub_arg):
|
2018-09-06 20:55:10 +02:00
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
@sub.command(short_help="bsub help")
|
|
|
|
@click.option("--bsub-opt", type=click.Choice(["bsubopt1", "bsubopt2"]))
|
|
|
|
@click.argument(
|
|
|
|
"bsub-arg1", required=False, type=click.Choice(["bsubarg1", "bsubarg2"])
|
|
|
|
)
|
|
|
|
@click.argument(
|
|
|
|
"bbsub-arg2", required=False, type=click.Choice(["bbsubarg1", "bbsubarg2"])
|
|
|
|
)
|
2018-09-06 20:55:10 +02:00
|
|
|
def bsub(bsub_opt):
|
2017-07-19 20:06:01 +02:00
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
@sub.group("csub")
|
2019-01-07 17:51:19 +01:00
|
|
|
def csub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@csub.command()
|
|
|
|
def dsub():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_with_help(cli, ["sub", "subarg1"], "") == [
|
|
|
|
("bsub", "bsub help"),
|
|
|
|
("csub", ""),
|
|
|
|
]
|
|
|
|
assert choices_without_help(cli, ["sub"], "") == ["subarg1", "subarg2"]
|
|
|
|
assert choices_without_help(cli, ["sub", "--sub-opt"], "") == ["subopt1", "subopt2"]
|
|
|
|
assert choices_without_help(cli, ["sub", "--sub-opt", "subopt1"], "") == [
|
|
|
|
"subarg1",
|
|
|
|
"subarg2",
|
|
|
|
]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"
|
|
|
|
) == ["--bsub-opt"]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], ""
|
|
|
|
) == ["bsubarg1", "bsubarg2"]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub", "--bsub-opt"], ""
|
|
|
|
) == ["bsubopt1", "bsubopt2"]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"sub",
|
|
|
|
"--sub-opt",
|
|
|
|
"subopt1",
|
|
|
|
"subarg1",
|
|
|
|
"bsub",
|
|
|
|
"--bsub-opt",
|
|
|
|
"bsubopt1",
|
|
|
|
"bsubarg1",
|
|
|
|
],
|
|
|
|
"",
|
|
|
|
) == ["bbsubarg1", "bbsubarg2"]
|
|
|
|
assert choices_without_help(
|
|
|
|
cli, ["sub", "--sub-opt", "subopt1", "subarg1", "csub"], ""
|
|
|
|
) == ["dsub"]
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_chained_multi():
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.group()
|
|
|
|
def sub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@sub.group()
|
|
|
|
def bsub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@sub.group(chain=True)
|
|
|
|
def csub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@csub.command()
|
|
|
|
def dsub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@csub.command()
|
|
|
|
def esub():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["sub"], "") == ["bsub", "csub"]
|
|
|
|
assert choices_without_help(cli, ["sub"], "c") == ["csub"]
|
|
|
|
assert choices_without_help(cli, ["sub", "csub"], "") == ["dsub", "esub"]
|
|
|
|
assert choices_without_help(cli, ["sub", "csub", "dsub"], "") == ["esub"]
|
2019-01-07 17:51:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_hidden():
|
|
|
|
@click.group()
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--name", hidden=True)
|
|
|
|
@click.option("--choices", type=click.Choice([1, 2]), hidden=True)
|
2019-01-07 17:51:19 +01:00
|
|
|
def cli(name):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.group(hidden=True)
|
|
|
|
def hgroup():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@hgroup.group()
|
|
|
|
def hgroupsub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
def asub():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command(hidden=True)
|
2020-07-21 08:23:42 +02:00
|
|
|
@click.option("--hname")
|
2019-01-07 17:51:19 +01:00
|
|
|
def hsub():
|
|
|
|
pass
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, [], "--n") == []
|
|
|
|
assert choices_without_help(cli, [], "--c") == []
|
2019-01-07 17:51:19 +01:00
|
|
|
# If the user exactly types out the hidden param, complete its options.
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["--choices"], "") == [1, 2]
|
|
|
|
assert choices_without_help(cli, [], "") == ["asub"]
|
|
|
|
assert choices_without_help(cli, [], "") == ["asub"]
|
|
|
|
assert choices_without_help(cli, [], "h") == []
|
2019-01-07 17:51:19 +01:00
|
|
|
# If the user exactly types out the hidden command, complete its subcommands.
|
2020-07-21 08:23:42 +02:00
|
|
|
assert choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]
|
|
|
|
assert choices_without_help(cli, ["hsub"], "--h") == ["--hname"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("args", "part", "expect"),
|
|
|
|
[
|
|
|
|
([], "-", ["--opt"]),
|
|
|
|
(["value"], "--", ["--opt"]),
|
|
|
|
([], "-o", []),
|
|
|
|
(["--opt"], "-o", []),
|
|
|
|
(["--"], "", ["name", "-o", "--opt", "--"]),
|
|
|
|
(["--"], "--o", ["--opt"]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_args_with_double_dash_complete(args, part, expect):
|
|
|
|
def _complete(ctx, args, incomplete):
|
|
|
|
values = ["name", "-o", "--opt", "--"]
|
|
|
|
return [x for x in values if x.startswith(incomplete)]
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.option("--opt")
|
|
|
|
@click.argument("args", nargs=-1, autocompletion=_complete)
|
|
|
|
def cli(opt, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert choices_without_help(cli, args, part) == expect
|