Perform shell completion for the given CLI program. :param cli: Command being called. :param ctx_args: Extra arguments to pass to ``cli.make_context``. :param prog_name: Name of the executable in the shell. :param complete_var: Name of the environment variable that holds
(
cli: Command,
ctx_args: cabc.MutableMapping[str, t.Any],
prog_name: str,
complete_var: str,
instruction: str,
)
| 17 | |
| 18 | |
| 19 | def shell_complete( |
| 20 | cli: Command, |
| 21 | ctx_args: cabc.MutableMapping[str, t.Any], |
| 22 | prog_name: str, |
| 23 | complete_var: str, |
| 24 | instruction: str, |
| 25 | ) -> t.Literal[0, 1]: |
| 26 | """Perform shell completion for the given CLI program. |
| 27 | |
| 28 | :param cli: Command being called. |
| 29 | :param ctx_args: Extra arguments to pass to |
| 30 | ``cli.make_context``. |
| 31 | :param prog_name: Name of the executable in the shell. |
| 32 | :param complete_var: Name of the environment variable that holds |
| 33 | the completion instruction. |
| 34 | :param instruction: Value of ``complete_var`` with the completion |
| 35 | instruction and shell, in the form ``instruction_shell``. |
| 36 | :return: Status code to exit with. |
| 37 | """ |
| 38 | shell, _, instruction = instruction.partition("_") |
| 39 | comp_cls = get_completion_class(shell) |
| 40 | |
| 41 | if comp_cls is None: |
| 42 | return 1 |
| 43 | |
| 44 | comp = comp_cls(cli, ctx_args, prog_name, complete_var) |
| 45 | |
| 46 | # Write bytes, otherwise Windows text stdout translates LF to CRLF and breaks. |
| 47 | if instruction == "source": |
| 48 | echo(comp.source().encode(), nl=False) |
| 49 | return 0 |
| 50 | |
| 51 | if instruction == "complete": |
| 52 | echo(comp.complete().encode()) |
| 53 | return 0 |
| 54 | |
| 55 | return 1 |
| 56 | |
| 57 | |
| 58 | if t.TYPE_CHECKING: |
searching dependent graphs…