Textualize/trogon

[bug] support trogon with `--tui` flag?

mileslucas opened this issue · 6 comments

So far I've really enjoyed using trogon, it's integrated really smoothly into the couple of click CLIs I'm preparing for deployment on a telescope control module. Something I don't vibe with, though, is forcing the use of groups for the scripts. Something I've just been writing is an acquire scripts with a bunch of options that I like having the TUI for. But having to rework my CLI to look like acquire int <args> or something like that to allow the acquire tui group is something I dislike.

What I think I would prefer is a --tui flag that has eager execution. I think this could be added to the api in a backwards-compatible way with a keyword argument in the decorator

@tui(eager_option=True)

and that way I can call my script with

acquire --tui

and launch into the TUI, whereas

acquire <args>...

would run the command as normal (no forced group)

slafs commented

You mean your script only has one command?
I think you can do that by yourself even now. Something like this should work:

import click
from trogon import Trogon


def open_tui(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return
    Trogon(ctx.command, app_name=ctx.info_name, click_context=ctx).run()
    ctx.exit()


@click.option(
    "--tui",
    help="Open Textual TUI.",
    is_flag=True,
    callback=open_tui,
    expose_value=False,
    is_eager=True,
)
@click.command()
def acquire():
    click.echo("Hello world")


if __name__ == "__main__":
    acquire()

Thanks @slafs this works well for me! I edited your open_tui a little bit to manually remove the --tui flag from showing up in the TUI menu

def open_tui(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return
    # remove --tui from context
    del ctx.command.params[0]
    Trogon(ctx.command, app_name=ctx.info_name, click_context=ctx).run()
    ctx.exit()

actually, I spoke too soon. This does not work-

when launching from the TUI, Trogon still tries putting the command name into the command-

command crafted in TUI:

acquire 100

command ran:

Running acquire main 100
slafs commented

Ah, interesting. Now that is a bug, I think. Especially that there's a difference between the TUI command and what's actually being run.

It seems like Trogon assumes that there needs to be a command to call.

After a brief look at the code there seems to be some sort of support for is_grouped_cli (when the app is a group) and include_root_command boolean argument (this is probably a good starting point):

trogon/trogon/trogon.py

Lines 262 to 265 in 2059c51

@on(CommandForm.Changed)
def update_command_to_run(self, event: CommandForm.Changed):
include_root_command = not self.is_grouped_cli
self.post_run_command = event.command_data.to_cli_args(include_root_command)

@mileslucas if I were you, I'd change the title of this issue from [feature request] to [bug] 😄.

I also would really like this feature. I have several cli apps that are built with typer and use multiple sub-commands that I also expose as individual scripts in the package's pyproject.toml (e.g. each subcommand could be called using <command> <sub-command> [options], or <sub-command> [options]). I would like to be able to update each of these tools/sub-commands with a --tui flag as suggested by @mileslucas.

I had a similar question, not sure if it's useful but I asked in the discord:

https://discord.com/channels/1026214085173461072/1033754296224841768/1115571327370264628

Basically, I had to replace @click.command above my only command with @click.group(invoke_without_command=True). Now my command is invoked by default and the help output shows all the flags, and also the --tui option.

(Now I have new issue to look at because I suck up all remaining args with @click.argument('feature_names', nargs=-1) and that interacts with the tui bit, but that's another story)