Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
7 tasks done
mzebrak opened this issue Jan 31, 2023 · 1 comment
Open
7 tasks done

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

mzebrak opened this issue Jan 31, 2023 · 1 comment
Labels
question Question or problem shell / zsh

Comments

@mzebrak
Copy link

mzebrak commented Jan 31, 2023

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

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:

  • myapp -> run the TUI/GUI mode
  • myapp -h (or any other argument/option given) -> run the CLI mode via Typer

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 mzebrak added the question Question or problem label Jan 31, 2023
@mzebrak
Copy link
Author

mzebrak commented Jan 31, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem shell / zsh
Projects
None yet
Development

No branches or pull requests

2 participants