Coverage for typer/completion.py: 100%
64 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
1import os 1habcdefg
2import sys 1habcdefg
3from typing import Any, MutableMapping, Tuple 1habcdefg
5import click 1habcdefg
7from ._completion_classes import completion_init 1habcdefg
8from ._completion_shared import Shells, get_completion_script, install 1habcdefg
9from .models import ParamMeta 1habcdefg
10from .params import Option 1habcdefg
11from .utils import get_params_from_function 1habcdefg
13try: 1habcdefg
14 import shellingham 1habcdefg
15except ImportError: # pragma: no cover
16 shellingham = None
19_click_patched = False 1habcdefg
22def get_completion_inspect_parameters() -> Tuple[ParamMeta, ParamMeta]: 1habcdefg
23 completion_init() 1habcdefg
24 test_disable_detection = os.getenv("_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION") 1habcdefg
25 if shellingham and not test_disable_detection: 1habcdefg
26 parameters = get_params_from_function(_install_completion_placeholder_function) 1habcdefg
27 else:
28 parameters = get_params_from_function( 1habcdefg
29 _install_completion_no_auto_placeholder_function
30 )
31 install_param, show_param = parameters.values() 1habcdefg
32 return install_param, show_param 1habcdefg
35def install_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any: 1habcdefg
36 if not value or ctx.resilient_parsing: 1habcdefg
37 return value # pragma: no cover 1habcdefg
38 if isinstance(value, str): 1habcdefg
39 shell, path = install(shell=value) 1habcdefg
40 else:
41 shell, path = install() 1habcdefg
42 click.secho(f"{shell} completion installed in {path}", fg="green") 1habcdefg
43 click.echo("Completion will take effect once you restart the terminal") 1habcdefg
44 sys.exit(0) 1habcdefg
47def show_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any: 1habcdefg
48 if not value or ctx.resilient_parsing: 1habcdefg
49 return value # pragma: no cover 1habcdefg
50 prog_name = ctx.find_root().info_name 1habcdefg
51 assert prog_name 1habcdefg
52 complete_var = "_{}_COMPLETE".format(prog_name.replace("-", "_").upper()) 1habcdefg
53 shell = "" 1habcdefg
54 test_disable_detection = os.getenv("_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION") 1habcdefg
55 if isinstance(value, str): 1habcdefg
56 shell = value 1habcdefg
57 elif shellingham and not test_disable_detection: 1habcdefg
58 shell, _ = shellingham.detect_shell() 1habcdefg
59 script_content = get_completion_script( 1habcdefg
60 prog_name=prog_name, complete_var=complete_var, shell=shell
61 )
62 click.echo(script_content) 1habcdefg
63 sys.exit(0) 1habcdefg
66# Create a fake command function to extract the completion parameters
67def _install_completion_placeholder_function( 1abcdefg
68 install_completion: bool = Option(
69 None,
70 "--install-completion",
71 is_flag=True,
72 callback=install_callback,
73 expose_value=False,
74 help="Install completion for the current shell.",
75 ),
76 show_completion: bool = Option(
77 None,
78 "--show-completion",
79 is_flag=True,
80 callback=show_callback,
81 expose_value=False,
82 help="Show completion for the current shell, to copy it or customize the installation.",
83 ),
84) -> Any:
85 pass # pragma: no cover
88def _install_completion_no_auto_placeholder_function( 1abcdefg
89 install_completion: Shells = Option(
90 None,
91 callback=install_callback,
92 expose_value=False,
93 help="Install completion for the specified shell.",
94 ),
95 show_completion: Shells = Option(
96 None,
97 callback=show_callback,
98 expose_value=False,
99 help="Show completion for the specified shell, to copy it or customize the installation.",
100 ),
101) -> Any:
102 pass # pragma: no cover
105# Re-implement Click's shell_complete to add error message with:
106# Invalid completion instruction
107# To use 7.x instruction style for compatibility
108# And to add extra error messages, for compatibility with Typer in previous versions
109# This is only called in new Command method, only used by Click 8.x+
110def shell_complete( 1abcdefg
111 cli: click.Command,
112 ctx_args: MutableMapping[str, Any],
113 prog_name: str,
114 complete_var: str,
115 instruction: str,
116) -> int:
117 import click 1habcdefg
118 import click.shell_completion 1habcdefg
120 if "_" not in instruction: 1habcdefg
121 click.echo("Invalid completion instruction.", err=True) 1habcdefg
122 return 1 1habcdefg
124 # Click 8 changed the order/style of shell instructions from e.g.
125 # source_bash to bash_source
126 # Typer override to preserve the old style for compatibility
127 # Original in Click 8.x commented:
128 # shell, _, instruction = instruction.partition("_")
129 instruction, _, shell = instruction.partition("_") 1habcdefg
130 # Typer override end
132 comp_cls = click.shell_completion.get_completion_class(shell) 1habcdefg
134 if comp_cls is None: 1habcdefg
135 click.echo(f"Shell {shell} not supported.", err=True) 1habcdefg
136 return 1 1habcdefg
138 comp = comp_cls(cli, ctx_args, prog_name, complete_var) 1habcdefg
140 if instruction == "source": 1habcdefg
141 click.echo(comp.source()) 1habcdefg
142 return 0 1habcdefg
144 if instruction == "complete": 1habcdefg
145 click.echo(comp.complete()) 1habcdefg
146 return 0 1habcdefg
148 click.echo(f'Completion instruction "{instruction}" not supported.', err=True) 1habcdefg
149 return 1 1habcdefg