: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.
Add automatic completion options by default, if click-completion is installed
authored by Sebastián Ramírez · committed by GitHub · parents ecd724a, e5c1c35 · 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
Provenance
- Commit
6d4bf36- Tree
c15ea29- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
ecd724a- Parent 2
e5c1c35
2 files changed+78 added−3 removed
added:typer/completion.py+46 added
1+from typing import Any2+3+import click4+import click_completion5+import click_completion.core6+7+from .params import Option8+9+click_completion.init()10+11+12+def 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)18+19+20+def 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)25+26+27+# Create a fake command function to extract the completion parameters28+def _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+ passmodified:typer/main.py+32 added-3 removed
23 unmodified lines24 TextFile,25 TyperInfo,26 )27 28+try:29+ import click_completion30+ from .completion import _install_completion_placeholder_function31+except ImportError:32+ click_completion = None33 34+35+def 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_param41+42+43 class Typer:44 def __init__(45 self,46 *,13 unmodified lines60 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 lines219 return group220 221 222 def 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"245 246 247 def get_group_name(typer_info: TyperInfo) -> Optional[str]:199 unmodified lines447 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