tiangolo / typer

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

[BUG] Completions that include colons get separated in zsh #162

Open jaywonchung opened 3 years ago

jaywonchung commented 3 years ago

Describe the bug

I have completions (via the autocompletion keyword argument in Arguments and Options) that include a colon (:) in the string. In zsh, these completions get split into completion candidates and help texts at the colon.

To Reproduce

import typer

images = ["alpine:latest", "nvidia/cuda:10.0-devel-ubuntu18.04"]

def _complete(value: str) -> str:
    for image in images:
        if image.startswith(value):
            yield value

app = typer.Typer()

@app.command("image")
def hello(name: str = typer.Option(..., autocompletion=_complete)):
    typer.echo(name)

if __name__ == "__main__":
    app()
$ <app_entrypoint> image --name [TAB]
$ <app_entrypoint> image --name [TAB]
alpine       -- latest
nvidia/cuda  -- 10.0-devel-ubuntu18.04

When I cycle through the completion candidates, only alpine and nvidia/cuda gets filled in.

$ <app_entrypoint> image --name [TAB]
alpine:latest
nvidia/cuda:10.0-devel-ubuntu18.04

Environment

Additional context

I tried the exact same thing in Bash, and it worked as expected (the output in "But I expected it to output").

alextremblay commented 3 years ago

I don't think this is a bug in typer, I think this is just the nature / default behaviour of the zsh completion system. zsh expects the results of your completetion function to take the form completion_candidate:help_text and formats that output accordingly. There are ways of getting around that in zsh, like so: https://unix.stackexchange.com/questions/445889/use-colon-as-filename-separator-in-zsh-tab-completion

but I suspect this is beyond the scope of typer's zsh auto-completion support (I'm not a core contributor, i can't speak for them, this is just my two cents)

tddschn commented 2 years ago

@alextremblay I tried the snippet in the link but it didn't work. I want my autocompletion function to return something like

B6:AA:33:B5:C0:93 -- Lmi 10.0.201.222 0

but got B6 -- AA:33:B5:C0:93 -- Lmi 10.0.201.222 0 instead.

I have no idea how zsh completion works, can you help me configure zsh so that the completions work? Thank you!

alextremblay commented 2 years ago

@tddschn sadly, no. i think this is something that would need to change in typer's auto-generated zsh autocomplete script.

you could try editing the generated script yourself, but then if you change your CLI's code and need to regenerate autocompletions, your changes will be wiped out