async def test_group_command_same_option(): cli = Group("cli", params=[Option(["-a"])], commands=[Command("x", params=[Option(["-a"])])]) assert await _get_words(cli, [], "-") == ["-a", "--help"] assert await _get_words(cli, ["-a", "a"], "-") == ["--help"] assert await _get_words(cli, ["-a", "a", "x"], "-") == ["-a", "--help"] assert await _get_words(cli, ["-a", "a", "x", "-a", "a"], "-") == ["--help"]
async def test_option_multiple(): cli = Command( "type", params=[ Option(["-m"], type=Choice(["a", "b"]), multiple=True), Option(["-f"]) ], ) assert await _get_words(cli, ["-m"], "") == ["a", "b"] assert "-m" in await _get_words(cli, ["-m", "a"], "-") assert await _get_words(cli, ["-m", "a", "-m"], "") == ["a", "b"] # used single options aren't suggested again assert "-c" not in await _get_words(cli, ["-c", "f"], "-")
async def test_path_types(type, expect): cli = Command("cli", params=[Option(["-f"], type=type)]) out = await _get_completions(cli, ["-f"], "ab") assert len(out) == 1 c = out[0] assert c.value == "ab" assert c.type == expect
async def test_command(): cli = Command("cli", params=[Option(["-t", "--test"])]) assert await _get_words(cli, [], "") == [] assert await _get_words(cli, [], "-") == ["-t", "--test", "--help"] assert await _get_words(cli, [], "--") == ["--test", "--help"] assert await _get_words(cli, [], "--t") == ["--test"] # -t has been seen, so --test isn't suggested assert await _get_words(cli, ["-t", "a"], "-") == ["--help"]
async def test_choice_case_sensitive(value, expect): cli = Command( "cli", params=[ Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value)) ], ) completions = await _get_words(cli, ["-a"], "a") assert completions == expect
async def test_hidden(): cli = Group( "cli", commands=[ Command( "hidden", add_help_option=False, hidden=True, params=[ Option(["-a"]), Option(["-b"], type=Choice(["a", "b"]), hidden=True), ], ) ], ) assert "hidden" not in await _get_words(cli, [], "") assert "hidden" not in await _get_words(cli, [], "hidden") assert await _get_words(cli, ["hidden"], "-") == ["-a"] assert await _get_words(cli, ["hidden", "-b"], "") == ["a", "b"]
async def test_option_flag(): cli = Command( "cli", add_help_option=False, params=[ Option(["--on/--off"]), Argument(["a"], type=Choice(["a1", "a2", "b"])), ], ) assert await _get_words(cli, [], "--") == ["--on", "--off"] # flag option doesn't take value, use choice argument assert await _get_words(cli, ["--on"], "a") == ["a1", "a2"]
async def test_double_dash(): cli = Command( "cli", add_help_option=False, params=[ Option(["--opt"]), Argument(["name"], type=Choice(["name", "--", "-o", "--opt"])), ], ) assert await _get_words(cli, [], "-") == ["--opt"] assert await _get_words(cli, ["value"], "-") == ["--opt"] assert await _get_words(cli, [], "") == ["name", "--", "-o", "--opt"] assert await _get_words(cli, ["--"], "") == ["name", "--", "-o", "--opt"]
def __init__(self, name: str, bring: Bring, **kwargs): self._bring: Bring = bring params = [ Argument( ["index"], required=True, nargs=1, type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True), metavar="PATH_TO_INDEX_FOLDER", ), Option( ["--output-file", "-o"], required=False, metavar="PATH", help= "the path to the index file, defaults to <index_folder>/.bring/this.br.idx", ), Option( ["--force", "-f"], is_flag=True, help="overwrite existing, inconsistent index file", ), Option( ["--check", "-c"], help="check export for inconsistencies and errors", is_flag=True, ), ] super().__init__(name=name, callback=self.export_index, params=params, **kwargs)
async def test_argument_nargs(): cli = Command( "cli", params=[ Argument(["x"], type=Choice(["a", "b"]), nargs=2), Argument(["y"], type=Choice(["c", "d"]), nargs=-1), Option(["-z"]), ], ) assert await _get_words(cli, [], "") == ["a", "b"] assert await _get_words(cli, ["a"], "") == ["a", "b"] assert await _get_words(cli, ["a", "b"], "") == ["c", "d"] assert await _get_words(cli, ["a", "b", "c"], "") == ["c", "d"] assert await _get_words(cli, ["a", "b", "c", "d"], "") == ["c", "d"] assert await _get_words(cli, ["a", "-z", "1"], "") == ["a", "b"] assert await _get_words(cli, ["a", "-z", "1", "b"], "") == ["c", "d"]
async def test_chained(): cli = Group( "cli", chain=True, commands=[ Command("set", params=[Option(["-y"])]), Command("start"), Group("get", commands=[Command("full")]), ], ) assert await _get_words(cli, [], "") == ["get", "set", "start"] assert await _get_words(cli, [], "s") == ["set", "start"] assert await _get_words(cli, ["set", "start"], "") == ["get"] # subcommands and parent subcommands assert await _get_words(cli, ["get"], "") == ["full", "set", "start"] assert await _get_words(cli, ["get", "full"], "") == ["set", "start"] assert await _get_words(cli, ["get"], "s") == ["set", "start"]
async def test_group(): cli = Group("cli", params=[Option(["-a"])], commands=[Command("x"), Command("y")]) assert await _get_words(cli, [], "") == ["x", "y"] assert await _get_words(cli, [], "-") == ["-a", "--help"]
async def test_option_nargs(): cli = Command("cli", params=[Option(["-c"], type=Choice(["a", "b"]), nargs=2)]) assert await _get_words(cli, ["-c"], "") == ["a", "b"] assert await _get_words(cli, ["-c", "a"], "") == ["a", "b"] assert await _get_words(cli, ["-c", "a", "b"], "") == []
async def test_type_choice(): cli = Command("cli", params=[Option(["-c"], type=Choice(["a1", "a2", "b"]))]) assert await _get_words(cli, ["-c"], "") == ["a1", "a2", "b"] assert await _get_words(cli, ["-c"], "a") == ["a1", "a2"] assert await _get_words(cli, ["-c"], "a2") == ["a2"]