Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Am I missing something with ValidArgsFunction? It isn't able to autocomplete anything #1796

Closed
ragntune opened this issue Sep 5, 2022 · 4 comments
Labels
kind/support Questions, supporting users, etc.

Comments

@ragntune
Copy link

ragntune commented Sep 5, 2022

I feel like I'm missing something with autocomplete suggestions. I want it to suggest the names "steve" and "john" (for the command below) but all it does is suggest files in the directory. Am I missing something? I feel like I've tried everything - and I can't get it to suggest a single thing.

var HelloCmd = &cobra.Command{
    Use:   "hello <name>",
    Short: "Say hello to someone",
    Long:  `Say hello to someone`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println(args)
        fmt.Println("hello " + args[0])
        cmd.Help()
    },
    ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
        if len(args) != 0 {
            return nil, cobra.ShellCompDirectiveNoFileComp
        }
        return []string{"steve", "john"}, cobra.ShellCompDirectiveNoFileComp
    },
}

On mac zsh I run go build . followed by ./program hello s[TAB] or ./program hello j[TAB]. If I type ./program hello [TAB] it suggests the files in the directory. Any help is greatly appreciated.

@marckhouzam
Copy link
Collaborator

Hi @johnnyjamil. You need to first get the shell completion script setup. You can look at how to do this through the help: ./program completion zsh -h.

To get you going quickly, here is what you need to do to setup in the current shell:

source <(./program completion zsh)
compdef _program program

If your shell completion wasn't setup at all, you need to do:

autoload -Uz compinit
compinit

I hope this helps.

@ragntune
Copy link
Author

ragntune commented Sep 6, 2022

Thank you @marckhouzam. Is there no way to do this out of the box? So a user installs the package via homebrew and the autocomplete works on their shell straight away.

@marckhouzam
Copy link
Collaborator

Yes homebrew can do this automatically. But you would need a homebrew package for your own tool. Here's Helm's:
https://github.com/Homebrew/homebrew-core/blob/7b45a34a86645962ad642cb2e750662a995f076a/Formula/helm.rb#L33

@johnSchnake johnSchnake added the kind/support Questions, supporting users, etc. label Sep 7, 2022
@marckhouzam
Copy link
Collaborator

marckhouzam commented Sep 30, 2022

Closing this as answered.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
kind/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

3 participants