clarkperkins / click-shell

An extension to click that easily turns your click app into a shell utility
BSD 3-Clause "New" or "Revised" License
88 stars 17 forks source link

Subshells #21

Open kmikaz51 opened 4 years ago

kmikaz51 commented 4 years ago

Hello,

I am interested in making a shell with subshells but I can't find a way of doing it with your lib.

Do you know if it is possible ?

Regards,

shengdi commented 4 years ago

Had the same issue and I ended up spawning another shell inside the shell...

@cli.group(invoke_without_command=True)
@click.pass_context
def test(ctx):
    if ctx.invoked_subcommand is None:
        subshell = make_click_shell(ctx, prompt='subshell >> ')
        subshell.cmdloop()

@test.command()
def test1():
    print("Test1")

@test.command()
def test2():
    print("Test2")
kmikaz51 commented 4 years ago

Thank you a lot.

Do you know if it is possible to change the prompt when the shell is running ?

deidukas commented 4 years ago

If you pass a callable that takes in a context as the prompt when using make_click_shell, I believe you can have it return a different string based on values in the click context.

See https://github.com/clarkperkins/click-shell/blob/ca211f6bce6755a79c1748015e1900908ca87b1d/click_shell/_cmd.py#L108

and https://github.com/clarkperkins/click-shell/blob/ca211f6bce6755a79c1748015e1900908ca87b1d/click_shell/_cmd.py#L129