Unable to get typer autocompletion working

413 Views Asked by At

Fairly new to typer and trying to get a simple CLI application to auto-complete with [TAB] on my terminal, but without success. Here is my code structure and code itself:

vinicius.ferreira@FVFFPG4VQ6L4 dev-env % tree
.
├── README.md
├── dev_env
│   ├── __init__.py
│   ├── main.py
│   ├── services.py
│   ├── team_product1.py
│   └── team_product2.py
├── dist
│   ├── dev_env-0.1.2-py3-none-any.whl
│   └── dev_env-0.1.2.tar.gz
├── poetry.lock
├── pyproject.toml
└── tests
    └── __init__.py

3 directories, 11 files
##### main.py #####
import typer

from . import team_product1
from . import team_product2
from . import services

app = typer.Typer()
app.add_typer(team_product1.app, name="team_product1")
app.add_typer(team_product2.app, name="team_product2")
app.add_typer(services.app, name="services")

if __name__ == "__main__":
    app()
##### team_product1.py #####
import typer

app = typer.Typer()


@app.command()
def start():
    print(f"Starting all services for Team Product 1 ...")


@app.command()
def stop():
    print(f"Stopping all services for Team Product 1 ...")


@app.command()
def destroy():
    print(f"Destroying all services for Team Product 1 ...")


if __name__ == "__main__":
    app()
##### team_product2.py #####
import typer

app = typer.Typer()


@app.command()
def start():
    print(f"Starting all services for Team Product 2 ...")


@app.command()
def stop():
    print(f"Stopping all services for Team Product 2 ...")


@app.command()
def destroy():
    print(f"Destroying all services for Team Product 2 ...")


if __name__ == "__main__":
    app()
##### services.py #####
import typer

app = typer.Typer()


@app.command()
def start(name: str):
    print(f"Starting service: {name}")


@app.command()
def stop(name: str):
    print(f"Stopping service: {name}")


@app.command()
def destroy(name: str):
    print(f"Destroying service: {name}")


if __name__ == "__main__":
    app()

I was able to build it into a .whl file with poetry build command, and later on install the package globally for all users using pip install dist/dev_env-0.1.2-py3-none-any.whl from the project root.

CLI application runs fine, except for the [TAB] auto-complete, and already executed dev-env --install-completion and restarted my terminal. It mentioned that "zsh completion installed in /Users/vinicius.ferreira/.zfunc/_dev-env" and the file content is this

#compdef dev-env

_dev_env_completion() {
  eval $(env _TYPER_COMPLETE_ARGS="${words[1,$CURRENT]}" _DEV_ENV_COMPLETE=complete_zsh dev-env)
}

compdef _dev_env_completion dev-env% 

Can anybody tell me what am I missing over here?

Not sure if it helps, but my ~/.zshrc file has this at the end:

zstyle ':completion:*' menu select
fpath+=~/.zfunc

and I also noticed typer tutorial shows completion should be installed on /home/user/.zshrc.. Not really sure why on my MacOS it was installed on a different place.

1

There are 1 best solutions below

1
Giles Knap On

I get zsh completion for typer projects using a command like this:

source <(dev-env --show-completion zsh)

You can put this in your .zshrc to make it available in all shells (but need to have dev-env installed