Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this work with Typer? #49

Open
cool-RR opened this issue Apr 1, 2023 · 3 comments
Open

Can this work with Typer? #49

cool-RR opened this issue Apr 1, 2023 · 3 comments

Comments

@cool-RR
Copy link

cool-RR commented Apr 1, 2023

I recently started using Typer, which is a layer above Click that parses variable annotations to make arguments and options. Is it possible to use click-shell with Typer commands?

@FergusFettes
Copy link

I'm going to make a PR with a solution for this, I will report back when I do, however if I forget, you can also look here, I'll post it there too: fastapi/typer#185

@FergusFettes
Copy link

FergusFettes commented May 10, 2023

Actually I just dumped my whole solution in there-- Q for the click-shell devs, would you be interested in integrating the typer-shell solution implemented at that link? Here is the core typer_shell.py:

from typing_extensions import Annotated
from typing import Optional, Callable

from click_shell import make_click_shell
from typer import Context, Typer, Argument

from rich import print


def make_typer_shell(
        app: Typer,
        prompt: str = ">> ",
        intro: str = "\n Welcome to typer-shell! Type help to see commands.\n",
        default: Optional[Callable] = None
) -> None:
    @app.command(hidden=True)
    def help(ctx: Context, command: Annotated[Optional[str], Argument()] = None):
        print("\n Type 'command --help' or 'help <command>' for help on a specific command.")
        if not command:
            ctx.parent.get_help()
            return
        ctx.parent.command.get_command(ctx, command).get_help(ctx)

    @app.command(hidden=True)
    def _default(args: Annotated[Optional[str], Argument()] = None):
        """Default command"""
        if default:
            default(args)
        else:
            print("Command not found. Type 'help' to see commands.")

    @app.callback(invoke_without_command=True)
    def launch(ctx: Context):
        if ctx.invoked_subcommand is None:
            shell = make_click_shell(ctx, prompt=prompt, intro=intro)
            shell.default = _default
            shell.cmdloop()

Happy to make a PR if it is something you are interested in. @clarkperkins

@FergusFettes
Copy link

https://github.com/FergusFettes/typer-shell

also added some nice new features to it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants