Open cool-RR opened 1 year ago
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: https://github.com/tiangolo/typer/issues/185
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
https://github.com/FergusFettes/typer-shell
also added some nice new features to it :)
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?