test_bashcomplete.py
This file
Path
tests/test_bashcomplete.py
520 lines · 16.3 KiB · Python
Last changed by
Include --help option in completion (#1504)Sebastián Ramírezbefore adoptionc99e0e8
History of this fileTake it
1# -*- coding: utf-8 -*-2import pytest34import click5from click._bashcomplete import get_choices678def choices_without_help(cli, args, incomplete):9 completions = get_choices(cli, "dummy", args, incomplete)10 return [c[0] for c in completions]111213def choices_with_help(cli, args, incomplete):14 return list(get_choices(cli, "dummy", args, incomplete))151617def test_single_command():18 @click.command()19 @click.option("--local-opt")20 def cli(local_opt):21 pass2223 assert choices_without_help(cli, [], "-") == ["--local-opt", "--help"]24 assert choices_without_help(cli, [], "") == []252627def test_boolean_flag():28 @click.command()29 @click.option("--shout/--no-shout", default=False)30 def cli(local_opt):31 pass3233 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]343536def test_multi_value_option():37 @click.group()38 @click.option("--pos", nargs=2, type=float)39 def cli(local_opt):40 pass4142 @cli.command()43 @click.option("--local-opt")44 def sub(local_opt):45 pass4647 assert choices_without_help(cli, [], "-") == ["--pos", "--help"]48 assert choices_without_help(cli, ["--pos"], "") == []49 assert choices_without_help(cli, ["--pos", "1.0"], "") == []50 assert choices_without_help(cli, ["--pos", "1.0", "1.0"], "") == ["sub"]515253def test_multi_option():54 @click.command()55 @click.option("--message", "-m", multiple=True)56 def cli(local_opt):57 pass5859 assert choices_without_help(cli, [], "-") == ["--message", "-m", "--help"]60 assert choices_without_help(cli, ["-m"], "") == []616263def test_small_chain():64 @click.group()65 @click.option("--global-opt")66 def cli(global_opt):67 pass6869 @cli.command()70 @click.option("--local-opt")71 def sub(local_opt):72 pass7374 assert choices_without_help(cli, [], "") == ["sub"]75 assert choices_without_help(cli, [], "-") == ["--global-opt", "--help"]76 assert choices_without_help(cli, ["sub"], "") == []77 assert choices_without_help(cli, ["sub"], "-") == ["--local-opt", "--help"]787980def test_long_chain():81 @click.group("cli")82 @click.option("--cli-opt")83 def cli(cli_opt):84 pass8586 @cli.group("asub")87 @click.option("--asub-opt")88 def asub(asub_opt):89 pass9091 @asub.group("bsub")92 @click.option("--bsub-opt")93 def bsub(bsub_opt):94 pass9596 COLORS = ["red", "green", "blue"]9798 def get_colors(ctx, args, incomplete):99 for c in COLORS:100 if c.startswith(incomplete):101 yield c102103 def search_colors(ctx, args, incomplete):104 for c in COLORS:105 if incomplete in c:106 yield c107108 CSUB_OPT_CHOICES = ["foo", "bar"]109 CSUB_CHOICES = ["bar", "baz"]110111 @bsub.command("csub")112 @click.option("--csub-opt", type=click.Choice(CSUB_OPT_CHOICES))113 @click.option("--csub", type=click.Choice(CSUB_CHOICES))114 @click.option("--search-color", autocompletion=search_colors)115 @click.argument("color", autocompletion=get_colors)116 def csub(csub_opt, color):117 pass118119 assert choices_without_help(cli, [], "-") == ["--cli-opt", "--help"]120 assert choices_without_help(cli, [], "") == ["asub"]121 assert choices_without_help(cli, ["asub"], "-") == ["--asub-opt", "--help"]122 assert choices_without_help(cli, ["asub"], "") == ["bsub"]123 assert choices_without_help(cli, ["asub", "bsub"], "-") == ["--bsub-opt", "--help"]124 assert choices_without_help(cli, ["asub", "bsub"], "") == ["csub"]125 assert choices_without_help(cli, ["asub", "bsub", "csub"], "-") == [126 "--csub-opt",127 "--csub",128 "--search-color",129 "--help",130 ]131 assert (132 choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "")133 == CSUB_OPT_CHOICES134 )135 assert choices_without_help(cli, ["asub", "bsub", "csub"], "--csub") == [136 "--csub-opt",137 "--csub",138 ]139 assert (140 choices_without_help(cli, ["asub", "bsub", "csub", "--csub"], "")141 == CSUB_CHOICES142 )143 assert choices_without_help(cli, ["asub", "bsub", "csub", "--csub-opt"], "f") == [144 "foo"145 ]146 assert choices_without_help(cli, ["asub", "bsub", "csub"], "") == COLORS147 assert choices_without_help(cli, ["asub", "bsub", "csub"], "b") == ["blue"]148 assert choices_without_help(149 cli, ["asub", "bsub", "csub", "--search-color"], "een"150 ) == ["green"]151152153def test_chaining():154 @click.group("cli", chain=True)155 @click.option("--cli-opt")156 @click.argument("arg", type=click.Choice(["cliarg1", "cliarg2"]))157 def cli(cli_opt, arg):158 pass159160 @cli.command()161 @click.option("--asub-opt")162 def asub(asub_opt):163 pass164165 @cli.command(help="bsub help")166 @click.option("--bsub-opt")167 @click.argument("arg", type=click.Choice(["arg1", "arg2"]))168 def bsub(bsub_opt, arg):169 pass170171 @cli.command()172 @click.option("--csub-opt")173 @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")174 def csub(csub_opt, arg):175 pass176177 assert choices_without_help(cli, [], "-") == ["--cli-opt", "--help"]178 assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]179 assert choices_without_help(cli, ["cliarg1", "asub"], "-") == [180 "--asub-opt",181 "--help",182 ]183 assert choices_without_help(cli, ["cliarg1", "asub"], "") == ["bsub", "csub"]184 assert choices_without_help(cli, ["cliarg1", "bsub"], "") == ["arg1", "arg2"]185 assert choices_without_help(cli, ["cliarg1", "asub", "--asub-opt"], "") == []186 assert choices_without_help(187 cli, ["cliarg1", "asub", "--asub-opt", "5", "bsub"], "-"188 ) == ["--bsub-opt", "--help"]189 assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == [190 "--bsub-opt",191 "--help",192 ]193 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "") == [194 "carg1",195 "carg2",196 ]197 assert choices_without_help(cli, ["cliarg1", "bsub", "arg1", "csub"], "") == [198 "carg1",199 "carg2",200 ]201 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == [202 "--csub-opt",203 "--help",204 ]205 assert choices_with_help(cli, ["cliarg1", "asub"], "b") == [("bsub", "bsub help")]206207208def test_argument_choice():209 @click.command()210 @click.argument("arg1", required=True, type=click.Choice(["arg11", "arg12"]))211 @click.argument("arg2", type=click.Choice(["arg21", "arg22"]), default="arg21")212 @click.argument("arg3", type=click.Choice(["arg", "argument"]), default="arg")213 def cli():214 pass215216 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]217 assert choices_without_help(cli, [], "arg") == ["arg11", "arg12"]218 assert choices_without_help(cli, ["arg11"], "") == ["arg21", "arg22"]219 assert choices_without_help(cli, ["arg12", "arg21"], "") == ["arg", "argument"]220 assert choices_without_help(cli, ["arg12", "arg21"], "argu") == ["argument"]221222223def test_option_choice():224 @click.command()225 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]), help="opt1 help")226 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]), default="opt21")227 @click.option("--opt3", type=click.Choice(["opt", "option"]))228 def cli():229 pass230231 assert choices_with_help(cli, [], "-") == [232 ("--opt1", "opt1 help"),233 ("--opt2", None),234 ("--opt3", None),235 ("--help", "Show this message and exit."),236 ]237 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2", "--opt3"]238 assert choices_without_help(cli, [], "--opt1=") == ["opt11", "opt12"]239 assert choices_without_help(cli, [], "--opt2=") == ["opt21", "opt22"]240 assert choices_without_help(cli, ["--opt2"], "=") == ["opt21", "opt22"]241 assert choices_without_help(cli, ["--opt2", "="], "opt") == ["opt21", "opt22"]242 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]243 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]244 assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [245 "opt21",246 "opt22",247 ]248 assert choices_without_help(cli, ["--opt2", "opt21"], "-") == [249 "--opt1",250 "--opt3",251 "--help",252 ]253 assert choices_without_help(cli, ["--opt1", "opt11"], "-") == [254 "--opt2",255 "--opt3",256 "--help",257 ]258 assert choices_without_help(cli, ["--opt1"], "opt") == ["opt11", "opt12"]259 assert choices_without_help(cli, ["--opt3"], "opti") == ["option"]260261 assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [262 "--opt2",263 "--opt3",264 "--help",265 ]266267268def test_option_and_arg_choice():269 @click.command()270 @click.option("--opt1", type=click.Choice(["opt11", "opt12"]))271 @click.argument("arg1", required=False, type=click.Choice(["arg11", "arg12"]))272 @click.option("--opt2", type=click.Choice(["opt21", "opt22"]))273 def cli():274 pass275276 assert choices_without_help(cli, ["--opt1"], "") == ["opt11", "opt12"]277 assert choices_without_help(cli, [""], "--opt1=") == ["opt11", "opt12"]278 assert choices_without_help(cli, [], "") == ["arg11", "arg12"]279 assert choices_without_help(cli, ["--opt2"], "") == ["opt21", "opt22"]280 assert choices_without_help(cli, ["arg11"], "--opt") == ["--opt1", "--opt2"]281 assert choices_without_help(cli, [], "--opt") == ["--opt1", "--opt2"]282283284def test_boolean_flag_choice():285 @click.command()286 @click.option("--shout/--no-shout", default=False)287 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))288 def cli(local_opt):289 pass290291 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]292 assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]293294295def test_multi_value_option_choice():296 @click.command()297 @click.option("--pos", nargs=2, type=click.Choice(["pos1", "pos2"]))298 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))299 def cli(local_opt):300 pass301302 assert choices_without_help(cli, ["--pos"], "") == ["pos1", "pos2"]303 assert choices_without_help(cli, ["--pos", "pos1"], "") == ["pos1", "pos2"]304 assert choices_without_help(cli, ["--pos", "pos1", "pos2"], "") == ["arg1", "arg2"]305 assert choices_without_help(cli, ["--pos", "pos1", "pos2", "arg1"], "") == []306307308def test_multi_option_choice():309 @click.command()310 @click.option("--message", "-m", multiple=True, type=click.Choice(["m1", "m2"]))311 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))312 def cli(local_opt):313 pass314315 assert choices_without_help(cli, ["-m"], "") == ["m1", "m2"]316 assert choices_without_help(cli, ["-m", "m1", "-m"], "") == ["m1", "m2"]317 assert choices_without_help(cli, ["-m", "m1"], "") == ["arg1", "arg2"]318319320def test_variadic_argument_choice():321 @click.command()322 @click.option("--opt", type=click.Choice(["opt1", "opt2"]))323 @click.argument("src", nargs=-1, type=click.Choice(["src1", "src2"]))324 def cli(local_opt):325 pass326327 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]328 assert choices_without_help(cli, ["src1", "src2"], "--o") == ["--opt"]329 assert choices_without_help(cli, ["src1", "src2", "--opt"], "") == ["opt1", "opt2"]330 assert choices_without_help(cli, ["src1", "src2"], "") == ["src1", "src2"]331332333def test_variadic_argument_complete():334 def _complete(ctx, args, incomplete):335 return ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"]336337 @click.group()338 def entrypoint():339 pass340341 @click.command()342 @click.option("--opt", autocompletion=_complete)343 @click.argument("arg", nargs=-1)344 def subcommand(opt, arg):345 pass346347 entrypoint.add_command(subcommand)348349 assert choices_without_help(entrypoint, ["subcommand", "--opt"], "") == _complete(350 0, 0, 0351 )352 assert choices_without_help(353 entrypoint, ["subcommand", "whatever", "--opt"], ""354 ) == _complete(0, 0, 0)355 assert (356 choices_without_help(entrypoint, ["subcommand", "whatever", "--opt", "abc"], "")357 == []358 )359360361def test_long_chain_choice():362 @click.group()363 def cli():364 pass365366 @cli.group()367 @click.option("--sub-opt", type=click.Choice(["subopt1", "subopt2"]))368 @click.argument(369 "sub-arg", required=False, type=click.Choice(["subarg1", "subarg2"])370 )371 def sub(sub_opt, sub_arg):372 pass373374 @sub.command(short_help="bsub help")375 @click.option("--bsub-opt", type=click.Choice(["bsubopt1", "bsubopt2"]))376 @click.argument(377 "bsub-arg1", required=False, type=click.Choice(["bsubarg1", "bsubarg2"])378 )379 @click.argument(380 "bbsub-arg2", required=False, type=click.Choice(["bbsubarg1", "bbsubarg2"])381 )382 def bsub(bsub_opt):383 pass384385 @sub.group("csub")386 def csub():387 pass388389 @csub.command()390 def dsub():391 pass392393 assert choices_with_help(cli, ["sub", "subarg1"], "") == [394 ("bsub", "bsub help"),395 ("csub", ""),396 ]397 assert choices_without_help(cli, ["sub"], "") == ["subarg1", "subarg2"]398 assert choices_without_help(cli, ["sub", "--sub-opt"], "") == ["subopt1", "subopt2"]399 assert choices_without_help(cli, ["sub", "--sub-opt", "subopt1"], "") == [400 "subarg1",401 "subarg2",402 ]403 assert choices_without_help(404 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"405 ) == ["--bsub-opt", "--help"]406 assert choices_without_help(407 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], ""408 ) == ["bsubarg1", "bsubarg2"]409 assert choices_without_help(410 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub", "--bsub-opt"], ""411 ) == ["bsubopt1", "bsubopt2"]412 assert choices_without_help(413 cli,414 [415 "sub",416 "--sub-opt",417 "subopt1",418 "subarg1",419 "bsub",420 "--bsub-opt",421 "bsubopt1",422 "bsubarg1",423 ],424 "",425 ) == ["bbsubarg1", "bbsubarg2"]426 assert choices_without_help(427 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "csub"], ""428 ) == ["dsub"]429430431def test_chained_multi():432 @click.group()433 def cli():434 pass435436 @cli.group()437 def sub():438 pass439440 @sub.group()441 def bsub():442 pass443444 @sub.group(chain=True)445 def csub():446 pass447448 @csub.command()449 def dsub():450 pass451452 @csub.command()453 def esub():454 pass455456 assert choices_without_help(cli, ["sub"], "") == ["bsub", "csub"]457 assert choices_without_help(cli, ["sub"], "c") == ["csub"]458 assert choices_without_help(cli, ["sub", "csub"], "") == ["dsub", "esub"]459 assert choices_without_help(cli, ["sub", "csub", "dsub"], "") == ["esub"]460461462def test_hidden():463 @click.group()464 @click.option("--name", hidden=True)465 @click.option("--choices", type=click.Choice([1, 2]), hidden=True)466 def cli(name):467 pass468469 @cli.group(hidden=True)470 def hgroup():471 pass472473 @hgroup.group()474 def hgroupsub():475 pass476477 @cli.command()478 def asub():479 pass480481 @cli.command(hidden=True)482 @click.option("--hname")483 def hsub():484 pass485486 assert choices_without_help(cli, [], "--n") == []487 assert choices_without_help(cli, [], "--c") == []488 # If the user exactly types out the hidden param, complete its options.489 assert choices_without_help(cli, ["--choices"], "") == [1, 2]490 assert choices_without_help(cli, [], "") == ["asub"]491 assert choices_without_help(cli, [], "") == ["asub"]492 assert choices_without_help(cli, [], "h") == []493 # If the user exactly types out the hidden command, complete its subcommands.494 assert choices_without_help(cli, ["hgroup"], "") == ["hgroupsub"]495 assert choices_without_help(cli, ["hsub"], "--h") == ["--hname", "--help"]496497498@pytest.mark.parametrize(499 ("args", "part", "expect"),500 [501 ([], "-", ["--opt", "--help"]),502 (["value"], "--", ["--opt", "--help"]),503 ([], "-o", []),504 (["--opt"], "-o", []),505 (["--"], "", ["name", "-o", "--opt", "--"]),506 (["--"], "--o", ["--opt"]),507 ],508)509def test_args_with_double_dash_complete(args, part, expect):510 def _complete(ctx, args, incomplete):511 values = ["name", "-o", "--opt", "--"]512 return [x for x in values if x.startswith(incomplete)]513514 @click.command()515 @click.option("--opt")516 @click.argument("args", nargs=-1, autocompletion=_complete)517 def cli(opt, args):518 pass519520 assert choices_without_help(cli, args, part) == expect