click-contrib / click-completion

Add or enhance bash, fish, zsh and powershell completion in Click
MIT License
288 stars 33 forks source link

Problem with completion #36

Closed XReaper95 closed 4 years ago

XReaper95 commented 4 years ago

Hi, so in my CLI, I have some callbacks with complex functionality. One of them checks for a specific file in the current directory, and exits with an error printed to sys.stdout. The problem is that the shell tries to evaluate that printed string. I am not sure what this is happening, don't know much about shells and autocompletion. I'm using ZSH, and you can easily replicate this error in one of the examples of this repository.

At examples/click-completion-callback, line 15, add this print statement:

def install_callback(ctx, attr, value):
    print("Hello World")  # add this
    if not value or ctx.resilient_parsing:
        return value
    shell, path = click_completion.core.install()
    click.echo('%s completion installed in %s' % (shell, path))
    exit(0)

If you try to complete this with TAB cli echo <TAB>', it produces this error

cli echo (eval):1: command not found: Hello
(eval):1: command not found: Hello
(eval):1: command not found: Hello
cli
Konubinix commented 4 years ago

AFAICU, shell completion is made by communicating the relevant information on stdout between the python program and the shell.

The callback is called when the completion is triggered. I suggest you use stderr to display an error. Actually, you would suggest to generally use stderr to deal with error messages.

Also, IIUC, ctx.resilient_parsing is True during the completion and False during the « normal » execution. You might want to use this value to change the behavior during the completion.

XReaper95 commented 4 years ago

@Konubinix Thank you so much for your help! , ctx.resilient_parsing was exactly what I needed, I read about it in the documentation but wasn't sure how to use it, until now. I will also take your suggestion regarding the stderr errors. My problem has been solved so I'm closing the issue.

Konubinix commented 4 years ago

Luis Ernesto Del Toro Peña notifications@github.com writes:

@Konubinix Thank you so much for your help! , ctx.resilient_parsing was exactly what I needed, I read about it in the documentation but wasn't sure how to use it, until now. I will also take your suggestion regarding the stderr errors. My problem has been solved so I'm closing the issue.

I am glad to know it was helpful :-).

-- Konubinix GPG Key : 7439106A Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A