async def test_argument_default(): cli = Command( "cli", add_help_option=False, params=[ Argument(["a"], type=Choice(["a"]), default="a"), Argument(["b"], type=Choice(["b"]), default="b"), ], ) assert await _get_words(cli, [], "") == ["a"] assert await _get_words(cli, ["a"], "b") == ["b"] # ignore type validation assert await _get_words(cli, ["x"], "b") == ["b"]
async def test_option_custom(): def custom(ctx, param, incomplete): return [incomplete.upper()] cli = Command( "cli", params=[ Argument(["x"]), Argument(["y"]), Argument(["z"], shell_complete=custom), ], ) assert await _get_words(cli, ["a", "b"], "") == [""] assert await _get_words(cli, ["a", "b"], "c") == ["C"]
async def test_argument_order(): cli = Command( "cli", params=[ Argument(["plain"]), Argument(["c1"], type=Choice(["a1", "a2", "b"])), Argument(["c2"], type=Choice(["c1", "c2", "d"])), ], ) # first argument has no completions assert await _get_words(cli, [], "") == [] assert await _get_words(cli, [], "a") == [] # first argument filled, now completion can happen assert await _get_words(cli, ["x"], "a") == ["a1", "a2"] assert await _get_words(cli, ["x", "b"], "d") == ["d"]
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_autocompletion_deprecated(): # old function takes args and not param, returns all values, can mix # strings and tuples def custom(ctx, args, incomplete): assert isinstance(args, list) return [("art", "x"), "bat", "cat"] with pytest.deprecated_call(): cli = Command("cli", params=[Argument(["x"], autocompletion=custom)]) assert await _get_words(cli, [], "") == ["art", "bat", "cat"] assert await _get_words(cli, [], "c") == ["cat"]
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 test_context_settings(runner): def complete(ctx, param, incomplete): return ctx.obj["choices"] cli = Command("cli", params=[Argument("x", shell_complete=complete)]) result = runner.invoke( cli, obj={"choices": ["a", "b"]}, env={ "COMP_WORDS": "", "COMP_CWORD": "0", "_CLI_COMPLETE": "bash_complete" }, ) assert result.output == "plain,a\nplain,b\n"
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)