Skip to content
Myr

Merge pull request #1679 from pallets/completion-context

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

pass extra context settings to completion

authored by David Lord · committed by GitHub · parents 6c8301e, 8981a95 · 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

4 files+32 added−11 removed

modified:src/click/core.py+3 added-3 removed
948 unmodified lines
949 if prog_name is None:950 prog_name = _detect_program_name()951952 # 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)954955 try:956 try:957 with self.make_context(prog_name, args, **extra) as ctx:
41 unmodified lines
999 raise1000 echo("Aborted!", file=sys.stderr)1001 sys.exit(1)10021003 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 lines
1019 return10201021 from .shell_completion import shell_complete10221023 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)10251026 def __call__(self, *args, **kwargs):1027 """Alias for :meth:`main`."""
1478 unmodified lines
modified:src/click/shell_completion.py+10 added-6 removed
6 unmodified lines
7from .parser import split_arg_string8from .utils import echo91011def shell_complete(cli, prog_name, complete_var, instruction):11def shell_complete(cli, ctx_args, prog_name, complete_var, instruction):12 """Perform shell completion for the given CLI program.1314 :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 completion
5 unmodified lines
2627 if comp_cls is None:28 return 12928 comp = comp_cls(cli, prog_name, complete_var)30 comp = comp_cls(cli, ctx_args, prog_name, complete_var)3132 if instruction == "source":33 echo(comp.source())34 return 0
156 unmodified lines
191 """Completion script template formatted by :meth:`source`. This must192 be provided by subclasses.193 """194193 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_var200201 @property
38 unmodified lines
240241 :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)245246 if ctx is None:247 return []248
199 unmodified lines
448449 return last_option is not None and last_option in param.opts450451449def _resolve_context(cli, prog_name, args):452def _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.456457 :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.args464465 while args:466 if isinstance(ctx.command, MultiCommand):
74 unmodified lines
modified:tests/test_shell_completion.py+15 added-1 removed
12 unmodified lines
13from click.types import Path141516def _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)192021def _get_words(cli, args, incomplete):
252 unmodified lines
274 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 == expect278279280@pytest.mark.usefixtures("_patch_for_completion")281def test_context_settings(runner):282 def complete(ctx, param, incomplete):283 return ctx.obj["choices"]284285 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 lines
11- 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 value
84 unmodified lines
103 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.106107- Extra context settings (``obj=...``, etc.) are passed on to the108 completion system. :issue:`942`109- Include ``--help`` option in completion. :pr:`1504`110111112Version 7.1.2113-------------114115Released 2020-04-27
633 unmodified lines