fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.44k stars 656 forks source link

Auto-completion when application works in 2 modes (GUI, CLI) #543

Open mzebrak opened 1 year ago

mzebrak commented 1 year ago

First Check

Commit to Help

Example Code

import sys

import typer

cli = typer.Typer()

def main() -> None:
    if not sys.argv[1:]:
        run_gui()
        return

    cli()

if __name__ == "__main__":
    main()

Description

Let's say I have an application called "myapp". This application runs in two modes - TUI/GUI and CLI. How I want it to be run is:

And it works fine as I have attached in the sample code, but... There is a problem when installing auto-completion via myapp --install-completion. Since it installs #compdef with just myapp command, when I press [TAB][TAB], it tries to run in TUI/GUI mode instead of CLI.

A simple, but not ideal workaround is to edit the complete definition and place some option there like myapp --from-completion so the full script for ZSH looks like this:

#compdef myapp

_myapp_completion() {
  eval $(env _TYPER_COMPLETE_ARGS="${words[1,$CURRENT]}" _MYAPP_COMPLETE=complete_zsh myapp)
}

compdef _myapp_completion myapp

and in the main() function we don't even have to edit anything since this additional --from-completion argv doesn't make the if statement to run the GUI.

But this workaround requires some action to do from the app user side. (manually editing the autocomplete definition script)

Is there another way to perform a check if the application was just run while [TAB][TAB] occurs?

Operating System

Linux

Operating System Details

ubuntu:22.04

Typer Version

0.7.0

Python Version

3.8.16

Additional Context

No response

mzebrak commented 1 year ago

One way I found is to perform additional checks like:

    if not sys.argv[1:] and sys.stdin and sys.stdin.isatty():
        run_gui()

and it works since the sys.stdin.isatty() is False when running from [TAB][TAB], but I haven't fully tested it yet, and there might be a better way to do this.

EDIT: Actually, since it looks like _MYAPP_COMPLETE env var is set in every compdef (bash zsh etc.) It is a better way to check if we are in the application from [TAB][TAB]

if not sys.argv[1:] and "_MYAPP_COMPLETE" not in os.environ:
    run_gui()