[go: up one dir, main page]

Skip to content

Commit

Permalink
✨ Add support for Click 8
Browse files Browse the repository at this point in the history
Based on tiangolo#67.
  • Loading branch information
alexreg committed Oct 21, 2022
1 parent 208f122 commit be67612
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
1 change: 1 addition & 0 deletions tests/test_completion_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test_script_completion_run():
env={
**os.environ,
"___MAIN__.PY_COMPLETE": "complete_bash",
"_PYTHON _M TYPER_CLI_COMPLETE": "complete_bash",
"COMP_WORDS": "typer tests/assets/sample.py",
"COMP_CWORD": "2",
"_TYPER_COMPLETE_TESTING": "True",
Expand Down
1 change: 1 addition & 0 deletions tests/test_sub_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test_script_completion_run():
env={
**os.environ,
"___MAIN__.PY_COMPLETE": "complete_bash",
"_PYTHON _M TYPER_CLI_COMPLETE": "complete_bash",
"COMP_WORDS": "typer tests/assets/sample.py run hello --",
"COMP_CWORD": "4",
"_TYPER_COMPLETE_TESTING": "True",
Expand Down
23 changes: 4 additions & 19 deletions typer_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
import re
import sys
from pathlib import Path
from typing import Any, List, Optional, Tuple, cast
from typing import Any, List, Optional, cast

import click
import click.core
import cloup
import typer
import typer.core
from click import Command, Group, Option
from click._bashcomplete import get_choices as original_get_choices # type: ignore
from click._bashcomplete import resolve_ctx # type: ignore

from . import __version__

Expand Down Expand Up @@ -54,7 +52,7 @@ def maybe_update_state(ctx: click.Context) -> None:
state.func = func_name


class TyperCLIGroup(cloup.Group):
class TyperCLIGroup(typer.core.TyperGroup):
def list_commands(self, ctx: click.Context) -> List[str]:
self.maybe_add_run(ctx)
return super().list_commands(ctx)
Expand Down Expand Up @@ -152,26 +150,14 @@ def maybe_add_run_to_cli(cli: click.Group) -> None:
cli.add_command(click_obj)


def get_choices(
cli: Command, prog_name: str, args: List[str], incomplete: str
) -> List[Tuple[str, str]]:
ctx: typer.Context = resolve_ctx(cli, prog_name, args)
if ctx.parent is None:
assert isinstance(cli, Group)
cli = cast(Group, cli)
maybe_update_state(ctx)
maybe_add_run_to_cli(cli)
return original_get_choices(cli, prog_name, args, incomplete)


def print_version(ctx: click.Context, param: Option, value: bool) -> None:
if not value or ctx.resilient_parsing:
return
typer.echo(f"Typer CLI version: {__version__}")
raise typer.Exit()


@app.callback(cls=TyperCLIGroup)
@app.callback(cls=TyperCLIGroup, no_args_is_help=True)
def callback(
ctx: typer.Context,
*,
Expand Down Expand Up @@ -303,5 +289,4 @@ def docs(


def main() -> Any:
click._bashcomplete.get_choices = get_choices
return app()

0 comments on commit be67612

Please sign in to comment.