def test_full_source(runner, shell): cli = Group("cli", commands=[Command("a"), Command("b")]) result = runner.invoke(cli, env={"_CLI_COMPLETE": f"{shell}_source"}) assert f"_CLI_COMPLETE={shell}_complete" in result.output
def test_option_nargs(): cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)]) assert _get_words(cli, ["-c"], "") == ["a", "b"] assert _get_words(cli, ["-c", "a"], "") == ["a", "b"] assert _get_words(cli, ["-c", "a", "b"], "") == []
def test_add_different_name(): cli = Group("cli", commands={"renamed": Command("original")}) words = _get_words(cli, [], "") assert "renamed" in words assert "original" not in words
def test_type_choice(): cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))]) assert _get_words(cli, ["-c"], "") == ["a1", "a2", "b"] assert _get_words(cli, ["-c"], "a") == ["a1", "a2"] assert _get_words(cli, ["-c"], "a2") == ["a2"]
def test_absolute_path(): cli = Command("cli", params=[Option(["-f"], type=Path())]) out = _get_completions(cli, ["-f"], "/ab") assert len(out) == 1 c = out[0] assert c.value == "/ab"
def test_group(): cli = Group("cli", params=[Option(["-a"])], commands=[Command("x"), Command("y")]) assert _get_words(cli, [], "") == ["x", "y"] assert _get_words(cli, [], "-") == ["-a", "--help"]
def test_full_complete(runner, shell, env, expect): cli = Group("cli", commands=[Command("a"), Command("b", help="bee")]) env["_CLI_COMPLETE"] = f"{shell}_complete" result = runner.invoke(cli, env=env) assert result.output == expect
def test_option_count(): cli = Command("cli", params=[Option(["-c"], count=True)]) assert _get_words(cli, ["-c"], "") == [] assert _get_words(cli, ["-c"], "-") == ["--help"]
def test_choice_special_characters(): cli = Command("cli", params=[Option(["-c"], type=Choice(["!1", "!2", "+3"]))]) assert _get_words(cli, ["-c"], "") == ["!1", "!2", "+3"] assert _get_words(cli, ["-c"], "!") == ["!1", "!2"] assert _get_words(cli, ["-c"], "!2") == ["!2"]
def map_command(command: Command, f) -> Command: import copy new_command = copy.copy(command) new_command.callback = lambda **kwargs: f(command.callback(**kwargs)) return new_command
def test_help_option(): cli = Group("cli", commands=[Command("with"), Command("no", add_help_option=False)]) assert _get_words(cli, ["with"], "--") == ["--help"] assert _get_words(cli, ["no"], "--") == []