Skip to content
Myr

Include --help option in completion (#1504)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 1784558 · Browse files · 3 files+50 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+50 added−24 removed

  • tests:+41 added−21 removed
  • other:+9 added−3 removed

3 files+50 added−24 removed

modified:src/click/_bashcomplete.py+3 added-3 removed
295 unmodified lines
296297 completions = []298 if not has_double_dash and start_of_option(incomplete):299 # completions for partial options300 for param in ctx.command.params:300 for param in ctx.command.get_params(ctx):301 if isinstance(param, Option) and not param.hidden:302 param_opts = [303 param_opt304 for param_opt in param.opts + param.secondary_opts
3 unmodified lines
308 [(o, param.help) for o in param_opts if o.startswith(incomplete)]309 )310 return completions311 # completion for option values from user supplied values312 for param in ctx.command.params:312 for param in ctx.command.get_params(ctx):313 if is_incomplete_option(all_args, param):314 return get_user_autocompletions(ctx, all_args, incomplete, param)315 # completion for argument values from user supplied values316 for param in ctx.command.params:316 for param in ctx.command.get_params(ctx):317 if is_incomplete_argument(ctx.params, param):318 return get_user_autocompletions(ctx, all_args, incomplete, param)319320 add_subcommand_completions(ctx, incomplete, completions)
55 unmodified lines
modified:tests/test_bashcomplete.py+41 added-21 removed
18 unmodified lines
19 @click.option("--local-opt")20 def cli(local_opt):21 pass2223 assert choices_without_help(cli, [], "-") == ["--local-opt"]23 assert choices_without_help(cli, [], "-") == ["--local-opt", "--help"]24 assert choices_without_help(cli, [], "") == []252627def test_boolean_flag():
1 unmodified line
29 @click.option("--shout/--no-shout", default=False)30 def cli(local_opt):31 pass3233 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]33 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]343536def test_multi_value_option():37 @click.group()
5 unmodified lines
43 @click.option("--local-opt")44 def sub(local_opt):45 pass4647 assert choices_without_help(cli, [], "-") == ["--pos"]47 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"]51
3 unmodified lines
55 @click.option("--message", "-m", multiple=True)56 def cli(local_opt):57 pass5859 assert choices_without_help(cli, [], "-") == ["--message", "-m"]59 assert choices_without_help(cli, [], "-") == ["--message", "-m", "--help"]60 assert choices_without_help(cli, ["-m"], "") == []616263def test_small_chain():
7 unmodified lines
71 def sub(local_opt):72 pass7374 assert choices_without_help(cli, [], "") == ["sub"]75 assert choices_without_help(cli, [], "-") == ["--global-opt"]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"]77 assert choices_without_help(cli, ["sub"], "-") == ["--local-opt", "--help"]787980def test_long_chain():81 @click.group("cli")
33 unmodified lines
115 @click.argument("color", autocompletion=get_colors)116 def csub(csub_opt, color):117 pass118119 assert choices_without_help(cli, [], "-") == ["--cli-opt"]119 assert choices_without_help(cli, [], "-") == ["--cli-opt", "--help"]120 assert choices_without_help(cli, [], "") == ["asub"]121 assert choices_without_help(cli, ["asub"], "-") == ["--asub-opt"]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"]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_CHOICES
39 unmodified lines
173 @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")174 def csub(csub_opt, arg):175 pass176176 assert choices_without_help(cli, [], "-") == ["--cli-opt"]177 assert choices_without_help(cli, [], "-") == ["--cli-opt", "--help"]178 assert choices_without_help(cli, [], "") == ["cliarg1", "cliarg2"]178 assert choices_without_help(cli, ["cliarg1", "asub"], "-") == ["--asub-opt"]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"], "-"184 ) == ["--bsub-opt"]185 assert choices_without_help(cli, ["cliarg1", "asub", "bsub"], "-") == ["--bsub-opt"]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 ]194 assert choices_without_help(cli, ["cliarg1", "asub", "csub"], "-") == ["--csub-opt"]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():
22 unmodified lines
231 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"]
4 unmodified lines
244 assert choices_without_help(cli, ["--opt1", "opt11", "--opt2"], "") == [245 "opt21",246 "opt22",247 ]237 assert choices_without_help(cli, ["--opt2", "opt21"], "-") == ["--opt1", "--opt3"]238 assert choices_without_help(cli, ["--opt1", "opt11"], "-") == ["--opt2", "--opt3"]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():
18 unmodified lines
287 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))288 def cli(local_opt):289 pass290271 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]291 assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]292 assert choices_without_help(cli, ["--shout"], "") == ["arg1", "arg2"]293294295def test_multi_value_option_choice():
105 unmodified lines
401 "subarg2",402 ]403 assert choices_without_help(404 cli, ["sub", "--sub-opt", "subopt1", "subarg1", "bsub"], "-"385 ) == ["--bsub-opt"]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(
81 unmodified lines
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"]475 assert choices_without_help(cli, ["hsub"], "--h") == ["--hname"]495 assert choices_without_help(cli, ["hsub"], "--h") == ["--hname", "--help"]496497498@pytest.mark.parametrize(499 ("args", "part", "expect"),500 [481 ([], "-", ["--opt"]),482 (["value"], "--", ["--opt"]),501 ([], "-", ["--opt", "--help"]),502 (["value"], "--", ["--opt", "--help"]),503 ([], "-o", []),504 (["--opt"], "-o", []),505 (["--"], "", ["name", "-o", "--opt", "--"]),506 (["--"], "--o", ["--opt"]),
14 unmodified lines
modified:CHANGES.rst+6 added
1.. currentmodule:: click23Version 7.24-----------56- Include ``--help`` option in completion. :pr:`1504`789Version 7.1.210-------------1112Released 2020-04-27
630 unmodified lines