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
Provenance
- Commit
c99e0e8- Tree
0bc3b42- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
1784558
3 files changed+50 added−24 removed
modified:src/click/_bashcomplete.py+3 added-3 removed
295 unmodified lines296 297 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_opts3 unmodified lines308 [(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)319 320 add_subcommand_completions(ctx, incomplete, completions)55 unmodified linesmodified:tests/test_bashcomplete.py+41 added-21 removed
18 unmodified lines19 @click.option("--local-opt")20 def cli(local_opt):21 pass22 23- assert choices_without_help(cli, [], "-") == ["--local-opt"]23+ assert choices_without_help(cli, [], "-") == ["--local-opt", "--help"]24 assert choices_without_help(cli, [], "") == []25 26 27 def test_boolean_flag():1 unmodified line29 @click.option("--shout/--no-shout", default=False)30 def cli(local_opt):31 pass32 33- assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout"]33+ assert choices_without_help(cli, [], "-") == ["--shout", "--no-shout", "--help"]34 35 36 def test_multi_value_option():37 @click.group()5 unmodified lines43 @click.option("--local-opt")44 def sub(local_opt):45 pass46 47- 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 lines55 @click.option("--message", "-m", multiple=True)56 def cli(local_opt):57 pass58 59- assert choices_without_help(cli, [], "-") == ["--message", "-m"]59+ assert choices_without_help(cli, [], "-") == ["--message", "-m", "--help"]60 assert choices_without_help(cli, ["-m"], "") == []61 62 63 def test_small_chain():7 unmodified lines71 def sub(local_opt):72 pass73 74 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"]78 79 80 def test_long_chain():81 @click.group("cli")33 unmodified lines115 @click.argument("color", autocompletion=get_colors)116 def csub(csub_opt, color):117 pass118 119- 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_CHOICES39 unmodified lines173 @click.argument("arg", type=click.Choice(["carg1", "carg2"]), default="carg1")174 def csub(csub_opt, arg):175 pass176 176- 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")]206 207 208 def test_argument_choice():22 unmodified lines231 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 lines244 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"]260 261 assert choices_without_help(cli, ["--opt1", "invalid_opt"], "-") == [262 "--opt2",263 "--opt3",264+ "--help",265 ]266 267 268 def test_option_and_arg_choice():18 unmodified lines287 @click.argument("arg", required=False, type=click.Choice(["arg1", "arg2"]))288 def cli(local_opt):289 pass290 271- 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"]293 294 295 def test_multi_value_option_choice():105 unmodified lines401 "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 lines491 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"]496 497 498 @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 linesmodified:CHANGES.rst+6 added
1 .. currentmodule:: click2 3+Version 7.24+-----------5+6+- Include ``--help`` option in completion. :pr:`1504`7+8+9 Version 7.1.210 -------------11 12 Released 2020-04-27630 unmodified lines