FergusFettes / typer-shell

Simple wrapper around the Typer library for making beautiful shells/REPLs for fast command-line prototyping with Python.
GNU General Public License v3.0
9 stars 2 forks source link

Question: how to run command on start #1

Closed MZachary closed 1 year ago

MZachary commented 1 year ago

Hi, I happened to be googling interactive typer shells and found the feature thread and your repo. It's been working great for me so far, I really appreciate the work!

I'm a bit new to the typer ecosystem so I hope this isn't too dumb of a question, but I've noticed that using make_typer_shell seems to prevent me from using app.callback() afterwards. Do you have a recommended way to get a particular command to run on app startup?

Thanks so much!

FergusFettes commented 1 year ago

My pleasure!

Can you share some code, I'm a bit confused by the issue. This works for example:

app = make_typer_shell(prompt="🔥: ", obj=App(), params={"name": "Bob"}, params_path="params.yaml")

@app.command()
def foobar(name: str = "Bob"):
    "Foobar command"
    print("Hello", name)

(thats from demo.py, maybe you can look through that file?)

glad you are finding it useful!

FergusFettes commented 1 year ago

Ah wait I see what you are saying. all.callback not app.command! Yeah I don't think that will work, because to initiate the shell I need to set app.callback. app.callback is what sets the shell, so I'm not sure if you will be able to set up your own one..

FergusFettes commented 1 year ago

oh wait actually you can set a callback to run on launch like so:

def launch(ctx: Context):
    print("hello world")

app = make_typer_shell(prompt="🔥: ", obj=App(), params={"name": "Bob"}, params_path="params.yaml", launch=launch)

it will be run when app.callback is run, aka directly when the shell launches

MZachary commented 1 year ago

Awesome, that "launch" code worked! Even kept the rich progress/loading wheel I was using. Much appreciated!