Skip to content
Myr

:sparkles: Add automatic completion options by default, if click-completion is installed

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 Sebastián Ramírez · parent ecd724a · Browse files · 2 files+78 added−3 removed
This commit

State

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

Diff stats

2 files+78 added−3 removed

  • other:+78 added−3 removed

2 files+78 added−3 removed

added:typer/completion.py+46 added
1from typing import Any23import click4import click_completion5import click_completion.core67from .params import Option89click_completion.init()101112def install_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:13 if not value or ctx.resilient_parsing:14 return value15 shell, path = click_completion.core.install()16 click.echo(f"{shell} completion installed in {path}")17 exit(0)181920def show_callback(ctx: click.Context, param: click.Parameter, value: Any) -> Any:21 if not value or ctx.resilient_parsing:22 return value23 click.echo(click_completion.core.get_code())24 exit(0)252627# Create a fake command function to extract the completion parameters28def _install_completion_placeholder_function(29 install_completion: bool = Option(30 None,31 "--install-completion",32 is_flag=True,33 callback=install_callback,34 expose_value=False,35 help="Install completion for the current shell.",36 ),37 show_completion: bool = Option(38 None,39 "--show-completion",40 is_flag=True,41 callback=show_callback,42 expose_value=False,43 help="Show completion for the current shell, to copy it or customize the installation.",44 ),45) -> Any:46 pass
modified:typer/main.py+32 added-3 removed
23 unmodified lines
24 TextFile,25 TyperInfo,26)2728try:29 import click_completion30 from .completion import _install_completion_placeholder_function31except ImportError:32 click_completion = None333435def get_install_completion_arguments() -> Tuple[click.Parameter, click.Parameter]:36 signature = inspect.signature(_install_completion_placeholder_function)37 install_param, show_param = signature.parameters.values()38 click_install_param, _ = get_click_param(install_param)39 click_show_param, _ = get_click_param(show_param)40 return click_install_param, click_show_param414243class Typer:44 def __init__(45 self,46 *,
13 unmodified lines
60 options_metavar: str = Default("[OPTIONS]"),61 add_help_option: bool = Default(True),62 hidden: bool = Default(False),63 deprecated: bool = Default(False),64 add_completion: bool = True,65 ):66 self._add_completion = add_completion67 self.info = TyperInfo(68 name=name,69 cls=cls,70 invoke_without_command=invoke_without_command,
148 unmodified lines
219 return group220221222def get_command(typer_instance: Typer) -> click.Command:223 if typer_instance._add_completion and click_completion:224 click_completion.init()225 click_install_param, click_show_param = get_install_completion_arguments()226 if (227 typer_instance.registered_callback228 or typer_instance.registered_groups229 or len(typer_instance.registered_commands) > 1230 ):231 # Create a Group212 return get_group(typer_instance)232 click_command = get_group(typer_instance)233 if typer_instance._add_completion and click_completion:234 click_command.params.append(click_install_param)235 click_command.params.append(click_show_param)236 return click_command237 elif len(typer_instance.registered_commands) == 1:238 # Create a single Command214 return get_command_from_info(typer_instance.registered_commands[0])239 click_command = get_command_from_info(typer_instance.registered_commands[0])240 if typer_instance._add_completion and click_completion:241 click_command.params.append(click_install_param)242 click_command.params.append(click_show_param)243 return click_command244 assert False, "Could not get a command for this Typer instance"245246247def get_group_name(typer_info: TyperInfo) -> Optional[str]:
199 unmodified lines
447 if parameter_info.min is not None:448 min_ = int(parameter_info.min)449 if parameter_info.max is not None:450 max_ = int(parameter_info.max)422 return click.IntRange(min=min_, max=max_, clamp=parameter_info.clamp,)451 return click.IntRange(min=min_, max=max_, clamp=parameter_info.clamp)452 else:453 return click.INT454 elif annotation == float:455 if parameter_info.min is not None or parameter_info.max is not None:
215 unmodified lines