Skip to content

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

Description

@mzebrak

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions