示例#1
0
async def test_option_choice():
    @click.command()
    @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"]))
    def cli():
        pass

    assert await choices_with_help(cli, [], "-") == [
        ("--opt1", "opt1 help"),
        ("--opt2", None),
        ("--opt3", None),
    ]
    assert await choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]
    assert await choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]
    assert await choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]
    assert await choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]
    assert await choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]
    assert await choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]
    assert await choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]
    assert await choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [
        "opt21",
        "opt22",
    ]
    assert await choices_without_help(cli, ["--opt2", "opt21"], "-") == ["--opt1", "--opt3"]
    assert await choices_without_help(cli, ["--opt1", "opt11"], "-") == ["--opt2", "--opt3"]
    assert await choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]
    assert await choices_without_help(cli, ["--opt3"], "opti") == ["option"]

    assert await choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [
        "--opt2",
        "--opt3",
    ]
示例#2
0
def test_case_insensitive_choice(runner):
    @click.command()
    @click.option("--foo", type=click.Choice(["Orange", "Apple"], case_sensitive=False))
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd, ["--foo", "apple"])
    assert result.exit_code == 0
    assert result.output == "Apple\n"

    result = runner.invoke(cmd, ["--foo", "oRANGe"])
    assert result.exit_code == 0
    assert result.output == "Orange\n"

    result = runner.invoke(cmd, ["--foo", "Apple"])
    assert result.exit_code == 0
    assert result.output == "Apple\n"

    @click.command()
    @click.option("--foo", type=click.Choice(["Orange", "Apple"]))
    def cmd2(foo):
        click.echo(foo)

    result = runner.invoke(cmd2, ["--foo", "apple"])
    assert result.exit_code == 2

    result = runner.invoke(cmd2, ["--foo", "oRANGe"])
    assert result.exit_code == 2

    result = runner.invoke(cmd2, ["--foo", "Apple"])
    assert result.exit_code == 0
示例#3
0
async def test_option_choice():
    @click.command()
    @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']))
    def cli():
        pass

    assert await choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),
                                               ('--opt2', None),
                                               ('--opt3', None)]
    assert await choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']
    assert await choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']
    assert await choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']
    assert await choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']
    assert await choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']
    assert await choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']
    assert await choices_without_help(cli, ['--opt3'], 'opti') == ['option']

    assert await choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']
示例#4
0
def test_case_insensitive_choice(runner):
    @click.command()
    @click.option('--foo',
                  type=click.Choice(['Orange', 'Apple'], case_sensitive=False))
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd, ['--foo', 'apple'])
    assert result.exit_code == 0
    assert result.output == 'Apple\n'

    result = runner.invoke(cmd, ['--foo', 'oRANGe'])
    assert result.exit_code == 0
    assert result.output == 'Orange\n'

    result = runner.invoke(cmd, ['--foo', 'Apple'])
    assert result.exit_code == 0
    assert result.output == 'Apple\n'

    @click.command()
    @click.option('--foo', type=click.Choice(['Orange', 'Apple']))
    def cmd2(foo):
        click.echo(foo)

    result = runner.invoke(cmd2, ['--foo', 'apple'])
    assert result.exit_code == 2

    result = runner.invoke(cmd2, ['--foo', 'oRANGe'])
    assert result.exit_code == 2

    result = runner.invoke(cmd2, ['--foo', 'Apple'])
    assert result.exit_code == 0
示例#5
0
async def test_multi_option_choice():
    @click.command()
    @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2']))
    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ['-m'], '') == ['m1', 'm2']
    assert await choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']
    assert await choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']
示例#6
0
async def test_multi_option_choice():
    @click.command()
    @click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))
    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ["-m"], "") == ["m1", "m2"]
    assert await choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]
    assert await choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]
示例#7
0
async def test_multi_value_option_choice():
    @click.command()
    @click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))
    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]
    assert await choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]
    assert await choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]
    assert await choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []
示例#8
0
async def test_multi_value_option_choice():
    @click.command()
    @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2']))
    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']
    assert await choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']
    assert await choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']
    assert await choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []
示例#9
0
async def test_variadic_argument_choice():
    @click.command()
    @click.option('--opt', type=click.Choice(['opt1', 'opt2']))
    @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']
    assert await choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt']
    assert await choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2']
    assert await choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']
示例#10
0
async def test_variadic_argument_choice():
    @click.command()
    @click.option("--opt", type=click.Choice(["opt1", "opt2"]))
    @click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]
    assert await choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]
    assert await choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]
    assert await choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]
示例#11
0
async def test_argument_choice():
    @click.command()
    @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")
    def cli():
        pass

    assert await choices_without_help(cli, [], "") == ["arg11", "arg12"]
    assert await choices_without_help(cli, [], "arg") == ["arg11", "arg12"]
    assert await choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]
    assert await choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]
    assert await choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]
示例#12
0
async def test_argument_choice():
    @click.command()
    @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')
    def cli():
        pass

    assert await choices_without_help(cli, [], '') == ['arg11', 'arg12']
    assert await choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']
    assert await choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']
    assert await choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']
    assert await choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']
示例#13
0
async def test_long_chain():
    @click.group('cli')
    @click.option('--cli-opt')
    def cli(cli_opt):
        pass

    @cli.group('asub')
    @click.option('--asub-opt')
    def asub(asub_opt):
        pass

    @asub.group('bsub')
    @click.option('--bsub-opt')
    def bsub(bsub_opt):
        pass

    COLORS = ['red', 'green', 'blue']
    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

    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)
    def csub(csub_opt, color):
        pass

    assert await choices_without_help(cli, [], '-') == ['--cli-opt']
    assert await choices_without_help(cli, [], '') == ['asub']
    assert await choices_without_help(cli, ['asub'], '-') == ['--asub-opt']
    assert await choices_without_help(cli, ['asub'], '') == ['bsub']
    assert await choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']
    assert await choices_without_help(cli, ['asub', 'bsub'], '') == ['csub']
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub'], '-') == ['--csub-opt', '--csub', '--search-color']
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], '') == CSUB_OPT_CHOICES
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub'], '--csub') == ['--csub-opt', '--csub']
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub'], '') == CSUB_CHOICES
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], 'f') == ['foo']
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub'], '') == COLORS
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub'], 'b') == ['blue']
    assert await choices_without_help(cli, ['asub', 'bsub', 'csub', '--search-color'], 'een') == ['green']
示例#14
0
async def test_option_and_arg_choice():
    @click.command()
    @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']))
    def cli():
        pass

    assert await choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']
    assert await choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']
    assert await choices_without_help(cli, [], '') == ['arg11', 'arg12']
    assert await choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']
    assert await choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2']
    assert await choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']
示例#15
0
async def test_option_and_arg_choice():
    @click.command()
    @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"]))
    def cli():
        pass

    assert await choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]
    assert await choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]
    assert await choices_without_help(cli, [], "") == ["arg11", "arg12"]
    assert await choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]
    assert await choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]
    assert await choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]
示例#16
0
def test_multiple_param_decls_not_allowed(runner):
    with pytest.raises(TypeError):

        @click.command()
        @click.argument("x", click.Choice(["a", "b"]))
        def copy(x):
            click.echo(x)
示例#17
0
async def test_hidden():
    @click.group()
    @click.option("--name", hidden=True)
    @click.option("--choices", type=click.Choice([1, 2]), hidden=True)
    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)
    @click.option("--hname")
    def hsub():
        pass

    assert await choices_without_help(cli, [], "--n") == []
    assert await choices_without_help(cli, [], "--c") == []
    # If the user exactly types out the hidden param, complete its options.
    assert await choices_without_help(cli, ["--choices"], "") == [1, 2]
    assert await choices_without_help(cli, [], "") == ["asub"]
    assert await choices_without_help(cli, [], "") == ["asub"]
    assert await choices_without_help(cli, [], "h") == []
    # If the user exactly types out the hidden command, complete its subcommands.
    assert await choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]
    assert await choices_without_help(cli, ["hsub"], "--h") == ["--hname"]
示例#18
0
def common_decorator(f):
    @click.option(
        "-o",
        "--output-dir",
        "--output--directory",
        "output",
        default=".",
        type=click.Path(),
        # fmt: off
        help="Target download directory."
             "Also creates if the directory doesn't exist.",
        # fmt: on
    )
    @click.option(
        "-j", "--jobs", default=4, type=int, help="Number of concurrent jobs."
    )
    @click.option(
        "-t",
        "--type",
        type=click.Choice(["sample", "file", "preview"]),
        default="file",
        help="Quality of the image.",
    )
    @click.pass_context
    def new_func(ctx, *args, **kwargs):
        return ctx.invoke(f, ctx, *args, **kwargs)

    return update_wrapper(new_func, f)
示例#19
0
async def test_hidden():
    @click.group()
    @click.option('--name', hidden=True)
    @click.option('--choices', type=click.Choice([1, 2]), hidden=True)
    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)
    @click.option('--hname')
    def hsub():
        pass

    assert await choices_without_help(cli, [], '--n') == []
    assert await choices_without_help(cli, [], '--c') == []
    # If the user exactly types out the hidden param, complete its options.
    assert await choices_without_help(cli, ['--choices'], '') == [1, 2]
    assert await choices_without_help(cli, [], '') == ['asub']
    assert await choices_without_help(cli, [], '') == ['asub']
    assert await choices_without_help(cli, [], 'h') == []
    # If the user exactly types out the hidden command, complete its subcommands.
    assert await choices_without_help(cli, ['hgroup'], '') == ['hgroupsub']
    assert await choices_without_help(cli, ['hsub'], '--h') == ['--hname']
示例#20
0
def test_choice_normalization(runner):
    @click.command(context_settings=CONTEXT_SETTINGS)
    @click.option("--choice", type=click.Choice(["Foo", "Bar"]))
    def cli(choice):
        click.echo(choice)

    result = runner.invoke(cli, ["--CHOICE", "FOO"])
    assert result.output == "Foo\n"
示例#21
0
def test_choice_normalization(runner):
    @click.command(context_settings=CONTEXT_SETTINGS)
    @click.option('--choice', type=click.Choice(['Foo', 'Bar']))
    def cli(choice):
        click.echo(choice)

    result = runner.invoke(cli, ['--CHOICE', 'FOO'])
    assert result.output == 'Foo\n'
示例#22
0
async def test_boolean_flag_choice():
    @click.command()
    @click.option("--shout/--no-shout", default=False)
    @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]
    assert await choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]
示例#23
0
async def test_boolean_flag_choice():
    @click.command()
    @click.option('--shout/--no-shout', default=False)
    @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))
    def cli(local_opt):
        pass

    assert await choices_without_help(cli, [], '-') == ['--shout', '--no-shout']
    assert await choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']
示例#24
0
async def test_chaining():
    @click.group("cli", chain=True)
    @click.option("--cli-opt")
    @click.argument("arg", type=click.Choice(["cliarg1", "cliarg2"]))
    def cli(cli_opt, arg):
        pass

    @cli.command()
    @click.option("--asub-opt")
    def asub(asub_opt):
        pass

    @cli.command(help="bsub help")
    @click.option("--bsub-opt")
    @click.argument("arg", type=click.Choice(["arg1", "arg2"]))
    def bsub(bsub_opt, arg):
        pass

    @cli.command()
    @click.option("--csub-opt")
    @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")
    def csub(csub_opt, arg):
        pass

    assert await choices_without_help(cli, [], "-") == ["--cli-opt"]
    assert await choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]
    assert await choices_without_help(cli, ["cliarg1", "asub"], "-") == ["--asub-opt"]
    assert await choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]
    assert await choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]
    assert await choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []
    assert await choices_without_help(
        cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"
    ) == ["--bsub-opt"]
    assert await choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == ["--bsub-opt"]
    assert await choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [
        "carg1",
        "carg2",
    ]
    assert await choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [
        "carg1",
        "carg2",
    ]
    assert await choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == ["--csub-opt"]
    assert await choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]
示例#25
0
async def test_long_chain_choice():
    @click.group()
    def cli():
        pass

    @cli.group()
    @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))
    @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))
    def sub(sub_opt, sub_arg):
        pass

    @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']))
    def bsub(bsub_opt):
        pass

    @sub.group('csub')
    def csub():
        pass

    @csub.command()
    def dsub():
        pass

    assert await choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')]
    assert await choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2']
    assert await choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']
    assert await choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \
           ['subarg1', 'subarg2']
    assert await choices_without_help(cli,
                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']
    assert await choices_without_help(cli,
                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2']
    assert await choices_without_help(cli,
                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \
           ['bsubopt1', 'bsubopt2']
    assert await choices_without_help(cli,
                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],
                                '') == ['bbsubarg1', 'bbsubarg2']
    assert await choices_without_help(cli,
                                ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'],
                                '') == ['dsub']
示例#26
0
def test_choices_list_in_prompt(runner, monkeypatch):
    @click.command()
    @click.option('-g',
                  type=click.Choice(['none', 'day', 'week', 'month']),
                  prompt=True)
    def cli_with_choices(g):
        pass

    @click.command()
    @click.option('-g',
                  type=click.Choice(['none', 'day', 'week', 'month']),
                  prompt=True,
                  show_choices=False)
    def cli_without_choices(g):
        pass

    result = runner.invoke(cli_with_choices, [], input='none')
    assert '(none, day, week, month)' in result.output

    result = runner.invoke(cli_without_choices, [], input='none')
    assert '(none, day, week, month)' not in result.output
示例#27
0
async def select_pool(ctx):
    click.clear()
    click.echo("==================")
    click.echo("  Download Pools  ")
    click.echo("==================")
    click.echo("")
    click.echo("Please give me the URLs and/or pool ID.")
    click.echo("When you're done, you can give me an empty line.")
    pools = []
    while True:
        # fmt: off
        response = click.prompt("",
                                prompt_suffix="> ",
                                default="",
                                show_default=False)
        # fmt: on
        if not response or not response.strip():
            if not pools:
                continue
            break

        pool_id = get_pool_id(response)
        if not pool_id:
            click.secho("Please send valid URL or pool ID!")
            continue

        obj = await get_pool(ctx, pool_id)
        if obj:
            print_pool(obj)
            pools.append(obj)  # noqa
        else:
            click.secho(f'Pool "{pool_id}" was not found.')  # noqa

    output = click.prompt("Where will the images be saved? ",
                          type=click.Path(),
                          default=".")
    jobs = click.prompt(
        "How many concurrent jobs will be done? "
        "(If you don't know what that means, just leave it as is.)",
        type=int,
        default=4,
    )
    type_ = click.prompt(
        "Which quality do you want to download? ",
        type=click.Choice(["sample", "file", "preview"]),
        default="file",
    )
    await ctx.invoke(pool,
                     pool_id=-1,
                     output=output,
                     jobs=jobs,
                     type=type_,
                     pools=pools)
示例#28
0
def test_missing_choice(runner):
    @click.command()
    @click.option('--foo', type=click.Choice(['foo', 'bar']), required=True)
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd)
    assert result.exit_code == 2
    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
示例#29
0
def test_missing_choice(runner):
    @click.command()
    @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd)
    assert result.exit_code == 2
    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
示例#30
0
def test_choices_list_in_prompt(runner, monkeypatch):
    @click.command()
    @click.option("-g",
                  type=click.Choice(["none", "day", "week", "month"]),
                  prompt=True)
    def cli_with_choices(g):
        pass

    @click.command()
    @click.option(
        "-g",
        type=click.Choice(["none", "day", "week", "month"]),
        prompt=True,
        show_choices=False,
    )
    def cli_without_choices(g):
        pass

    result = runner.invoke(cli_with_choices, [], input="none")
    assert "(none, day, week, month)" in result.output

    result = runner.invoke(cli_without_choices, [], input="none")
    assert "(none, day, week, month)" not in result.output