예제 #1
0
def test_group_add_command_name(runner):
    cli = click.Group("cli")
    cmd = click.Command("a", params=[click.Option(["-x"], required=True)])
    cli.add_command(cmd, "b")
    # Check that the command is accessed through the registered name,
    # not the original name.
    result = runner.invoke(cli, ["b"], default_map={"b": {"x": 3}})
    assert result.exit_code == 0
예제 #2
0
def test_group_from_list(runner):
    """A Group can be built with a list of commands."""
    @click.command()
    def sub():
        click.echo("sub", nl=False)

    cli = click.Group(commands=[sub])
    result = runner.invoke(cli, ["sub"])
    assert result.output == "sub"
예제 #3
0
def test_group_commands_dict(runner):
    """A Group can be built with a dict of commands."""
    @click.command()
    def sub():
        click.echo("sub", nl=False)

    cli = click.Group(commands={"other": sub})
    result = runner.invoke(cli, ["other"])
    assert result.output == "sub"
예제 #4
0
def test_propagate_show_default_setting(runner):
    """A context's ``show_default`` setting defaults to the value from
    the parent context.
    """
    group = click.Group(
        commands={
            "sub":
            click.Command("sub", params=[click.Option(["-a"], default="a")]),
        },
        context_settings={"show_default": True},
    )
    result = runner.invoke(group, ["sub", "--help"])
    assert "[default: a]" in result.output
예제 #5
0
async def test_other_command_forward(runner):
    cli = click.Group()

    @cli.command()
    @click.option('--count', default=1)
    def test(count):
        click.echo('Count: %d' % count)

    @cli.command()
    @click.option('--count', default=1)
    @click.pass_context
    async def dist(ctx, count):
        await ctx.forward(test)
        await ctx.invoke(test, count=42)

    result = await runner.invoke(cli, ['dist'], _sync=True)
    assert not result.exception
    assert result.output == 'Count: 1\nCount: 42\n'
예제 #6
0
async def test_other_command_forward(runner):
    cli = click.Group()

    @cli.command()
    @click.option("--count", default=1)
    def test(count):
        click.echo("Count: {:d}".format(count))

    @cli.command()
    @click.option("--count", default=1)
    @click.pass_context
    async def dist(ctx, count):
        await ctx.forward(test)
        await ctx.invoke(test, count=42)

    result = await runner.invoke(cli, ["dist"], _sync=True)
    if result.exception:
        raise result.exception
    assert result.output == "Count: 1\nCount: 42\n"
예제 #7
0
async def test_forwarded_params_consistency(runner):
    cli = click.Group()

    @cli.command()
    @click.option("-a")
    @click.pass_context
    def first(ctx, **kwargs):
        click.echo(f"{ctx.params}")

    @cli.command()
    @click.option("-a")
    @click.option("-b")
    @click.pass_context
    async def second(ctx, **kwargs):
        click.echo(f"{ctx.params}")
        await ctx.forward(first)

    result = runner.invoke(cli, ["second", "-a", "foo", "-b", "bar"])
    if result.exception:
        raise result.exception
    assert result.output == "{'a': 'foo', 'b': 'bar'}\n{'a': 'foo', 'b': 'bar'}\n"
예제 #8
0
    },
)
HELLO_COMMAND = (
    click.Command("hello", params=[NUMBER_OPTION[0]]),
    {
        "name": "hello",
        "params": [NUMBER_OPTION[1], HELP_OPTION[1]],
        "help": None,
        "epilog": None,
        "short_help": None,
        "hidden": False,
        "deprecated": False,
    },
)
HELLO_GROUP = (
    click.Group("cli", [HELLO_COMMAND[0]]),
    {
        "name": "cli",
        "params": [HELP_OPTION[1]],
        "help": None,
        "epilog": None,
        "short_help": None,
        "hidden": False,
        "deprecated": False,
        "commands": {
            "hello": HELLO_COMMAND[1]
        },
        "chain": False,
    },
)