2

I'm trying to get autocomplete working with Cobra but nothing happens after pressing the tab key - on something that I think should autocomplete. Am I missing something?

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
    },
}

And then I call go build . followed by ./program hello s[TAB] or ./program hello j[TAB] it suggests nothing. I want to it suggest the names "steve" and "john". If I type ./program hello [TAB] it suggests the files in the directory.

Pls help I've been ripping my hairs out all morning to fix this!

johnny
  • 51
  • 4

1 Answers1

0

you need to generate autocomplete script and add it to your shell profile follow this section:

Creating your own completion command

and see [cmd] completion -h for more help.

Lookis
  • 65
  • 4