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

[completion] how to print error info with RegisterFlagCompletionFunc. #1710

Closed
lixd opened this issue May 25, 2022 · 3 comments
Closed

[completion] how to print error info with RegisterFlagCompletionFunc. #1710

lixd opened this issue May 25, 2022 · 3 comments
Labels
area/shell-completion All shell completions kind/support Questions, supporting users, etc.

Comments

@lixd
Copy link

lixd commented May 25, 2022

i used RegisterFlagCompletionFunc to build dynamic completion.

_ = cmd.RegisterFlagCompletionFunc("license", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		data, err := httpRequest()
		if err != nil {
                         // this is not work
                         fmt.Println("completion error:",err.Error())
			return nil, cobra.ShellCompDirectiveNoFileComp
		}
}

and want to print error info. but it does not work.
when i use

mycmd --myflag [tab][tab]
# the error info has splited
2h35m54.224688284s  [completion         error]              is                  to                  Unauthorized        
by                  due                 expired             reason              token  

i can‘t get origin error print. only with __complete can got it.

mycmd __complete --myflag ""[enter]
[completion error] Unauthorized due to reason token is expired by 2h38m9.850332898s
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
@marckhouzam
Copy link
Collaborator

Hi @lixd. You cannot currently print error messages for the users to see when doing shell completion. When there is an error usually people return no completions.

If you want to debug your completion code yourself you can use the __complete command and add traces using one of Cobra's completion debugging functions such asCobra.CompError(). Please see this doc for debugging completion code: https://github.com/spf13/cobra/blob/master/shell_completions.md#debugging

If you are interested in printing a message to the actual user when they perform completion, the new ActiveHelp feature of #1482 will allow to do that (for bash and zsh only).

@jpmcb jpmcb added area/shell-completion All shell completions kind/support Questions, supporting users, etc. labels May 25, 2022
@github-actions
Copy link

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interseted in reopening

@marckhouzam
Copy link
Collaborator

Since this was answered and we didn't get any feedback I'll go ahead and close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/shell-completion All shell completions kind/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

3 participants