Skip to content

Commit 288cfae

Browse files
committed
Introduce FixedCompletions
Example usage: choices := []string{"choice1", "choice2", "choice3"} cmd.RegisterFlagCompletionFunc(cobra.FixedCompletions(choices, ShellCompDirectiveNoFileComp))
1 parent cb9d7b1 commit 288cfae

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

completions.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]string
103103
return nil, ShellCompDirectiveNoFileComp
104104
}
105105

106+
// FixedCompletions can be used to create a completion function which always
107+
// returns the same results.
108+
func FixedCompletions(choices []string, directive ShellCompDirective) func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
109+
return func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
110+
return choices, directive
111+
}
112+
}
113+
106114
// RegisterFlagCompletionFunc should be called to register a function to provide completion for a flag.
107115
func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective)) error {
108116
flag := c.Flag(flagName)

0 commit comments

Comments
 (0)