Skip to content

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

@ragntune

Description

@ragntune

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/supportQuestions, supporting users, etc.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions