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

provide an error message if completion errs in zsh #1922

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gusega
Copy link
Contributor

@gusega gusega commented Mar 3, 2023

If the completion errs, then nothing is displayed to the user.

This change makes the returned completion (containing an error message) displayed on screen if the completion status is error in zsh.

Of course it would be better to do this also in all other shells (bash, powershell, etc), but for now this is for zsh only. Let me know if it's also desirable to look at other shells, maybe I could do that.

@gusega gusega changed the title provide a message in case of an error provide an error message in completion errs Mar 14, 2023
@gusega gusega changed the title provide an error message in completion errs provide an error message if completion errs Mar 14, 2023
@gusega gusega changed the title provide an error message if completion errs provide an error message if completion errs in zsh Mar 14, 2023
@marckhouzam
Copy link
Collaborator

Thanks @gusega. Could you give an example of how you imagine this being used?
It is also a bit tricky because we would start showing a message based on the completions being returned, but some projects may be blindly returning something but expecting it to be ignored when the ShellCompDirectiveError is used.

An alternative could be to use ActiveHelp in this case, which is already supported for both zsh and bash.
See this doc for ActiveHelp details: https://github.com/spf13/cobra/blob/main/active_help.md

@gusega
Copy link
Contributor Author

gusega commented Mar 20, 2023

Hi @marckhouzam

So, in your program, in completion function you'd return some error description as a single value of the completion slice, and the completion directive must be cobra.ShellCompDirectiveError, like this: return []string{"error description"}, cobra.ShellCompDirectiveError. The idea is that completion error means that something went wrong and user should know about it. Examples that I encountered: client application trying to get nouns completions when server is down, or the user has not authorised yet (and therefore cannot get nouns from the server).

I think I could do the same using active ActiveHelp, although it feels like misusing it, because it seems that ActiveHelp is intended for situations when completion cannot be tried, as user didn't provide enough info. And completion error means that all the stuff was provided, but the result is error.

Maybe we can introduce a new completion directive that would make completion function display the completion error, e.g. cobra.ShellCompDirectiveErrorShow, so that old directive still works as it is used to.

Meanwhile I will use ActiveHelp (didn't know about it) with something like this:

comps, err := getCompletions()
if err != nil {
    return cobra.AppendActiveHelp(make([]string, 0, 1), err.Error()), cobra.ShellCompDirectiveNoFileComp
}
return comps, cobra.ShellCompDirectiveNoFileComp

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants