pass extra context settings to completion
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 6c8301e · Browse files · 4 files·+32 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−11 removed
- tests:+15 added−1 removed
- other:+17 added−10 removed
Provenance
- Commit
8981a95- Tree
e965c86- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
6c8301e
4 files changed+32 added−11 removed
modified:src/click/core.py+3 added-3 removed
948 unmodified lines949 if prog_name is None:950 prog_name = _detect_program_name()951 952 # Process shell completion requests and exit early.953- self._main_shell_completion(prog_name, complete_var)953+ self._main_shell_completion(extra, prog_name, complete_var)954 955 try:956 try:957 with self.make_context(prog_name, args, **extra) as ctx:41 unmodified lines999 raise1000 echo("Aborted!", file=sys.stderr)1001 sys.exit(1)1002 1003- def _main_shell_completion(self, prog_name, complete_var=None):1003+ def _main_shell_completion(self, ctx_args, prog_name, complete_var=None):1004 """Check if the shell is asking for tab completion, process1005 that, then exit early. Called from :meth:`main` before the1006 program is invoked.1007 11 unmodified lines1019 return1020 1021 from .shell_completion import shell_complete1022 1023- rv = shell_complete(self, prog_name, complete_var, instruction)1023+ rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)1024 _fast_exit(rv)1025 1026 def __call__(self, *args, **kwargs):1027 """Alias for :meth:`main`."""1478 unmodified linesmodified:src/click/shell_completion.py+10 added-6 removed
6 unmodified lines7 from .parser import split_arg_string8 from .utils import echo9 10 11-def shell_complete(cli, prog_name, complete_var, instruction):11+def shell_complete(cli, ctx_args, prog_name, complete_var, instruction):12 """Perform shell completion for the given CLI program.13 14 :param cli: Command being called.15+ :param ctx_args: Extra arguments to pass to16+ ``cli.make_context``.17 :param prog_name: Name of the executable in the shell.18 :param complete_var: Name of the environment variable that holds19 the completion instruction.20 :param instruction: Value of ``complete_var`` with the completion5 unmodified lines26 27 if comp_cls is None:28 return 129 28- comp = comp_cls(cli, prog_name, complete_var)30+ comp = comp_cls(cli, ctx_args, prog_name, complete_var)31 32 if instruction == "source":33 echo(comp.source())34 return 0156 unmodified lines191 """Completion script template formatted by :meth:`source`. This must192 be provided by subclasses.193 """194 193- def __init__(self, cli, prog_name, complete_var):195+ def __init__(self, cli, ctx_args, prog_name, complete_var):196 self.cli = cli197+ self.ctx_args = ctx_args198 self.prog_name = prog_name199 self.complete_var = complete_var200 201 @property38 unmodified lines240 241 :param args: List of complete args before the incomplete value.242 :param incomplete: Value being completed. May be empty.243 """241- ctx = _resolve_context(self.cli, self.prog_name, args)244+ ctx = _resolve_context(self.cli, self.ctx_args, self.prog_name, args)245 246 if ctx is None:247 return []248 199 unmodified lines448 449 return last_option is not None and last_option in param.opts450 451 449-def _resolve_context(cli, prog_name, args):452+def _resolve_context(cli, ctx_args, prog_name, args):453 """Produce the context hierarchy starting with the command and454 traversing the complete arguments. This only follows the commands,455 it doesn't trigger input prompts or callbacks.456 457 :param cli: Command being called.458 :param prog_name: Name of the executable in the shell.459 :param args: List of complete args before the incomplete value.460 """461+ ctx_args["resilient_parsing"] = True458- ctx = cli.make_context(prog_name, args.copy(), resilient_parsing=True)462+ ctx = cli.make_context(prog_name, args.copy(), **ctx_args)463 args = ctx.protected_args + ctx.args464 465 while args:466 if isinstance(ctx.command, MultiCommand):74 unmodified linesmodified:tests/test_shell_completion.py+15 added-1 removed
12 unmodified lines13 from click.types import Path14 15 16 def _get_completions(cli, args, incomplete):17- comp = ShellComplete(cli, cli.name, "_CLICK_COMPLETE")17+ comp = ShellComplete(cli, {}, cli.name, "_CLICK_COMPLETE")18 return comp.get_completions(args, incomplete)19 20 21 def _get_words(cli, args, incomplete):252 unmodified lines274 cli = Group("cli", commands=[Command("a"), Command("b", help="bee")])275 env["_CLI_COMPLETE"] = f"complete_{shell}"276 result = runner.invoke(cli, env=env)277 assert result.output == expect278+279+280+@pytest.mark.usefixtures("_patch_for_completion")281+def test_context_settings(runner):282+ def complete(ctx, param, incomplete):283+ return ctx.obj["choices"]284+285+ cli = Command("cli", params=[Argument("x", shell_complete=complete)])286+ result = runner.invoke(287+ cli,288+ obj={"choices": ["a", "b"]},289+ env={"COMP_WORDS": "", "COMP_CWORD": "0", "_CLI_COMPLETE": "complete_bash"},290+ )291+ assert result.output == "plain,a\nplain,b\n"modified:CHANGES.rst+4 added-1 removed
10 unmodified lines11 - Add support for distinguishing the source of a command line12 parameter. :issue:`1264`, :pr:`1329`13 - Add an optional parameter to ``ProgressBar.update`` to set the14 ``current_item``. :issue:`1226`, :pr:`1332`15-- Include ``--help`` option in completion. :pr:`1504`15 - ``version_option`` uses ``importlib.metadata`` (or the16 ``importlib_metadata`` backport) instead of ``pkg_resources``.17 :issue:`1582`18 - If validation fails for a prompt with ``hide_input=True``, the value84 unmodified lines103 all values, and must return a list of strings or a list of104 ``ShellComplete``. The old name and behavior is deprecated and105 will be removed in 8.1.106 107+- Extra context settings (``obj=...``, etc.) are passed on to the108+ completion system. :issue:`942`109+- Include ``--help`` option in completion. :pr:`1504`110 111+112 Version 7.1.2113 -------------114 115 Released 2020-04-27633 unmodified lines