MCPcopy
hub / github.com/spf13/cobra / RegisterFlagCompletionFunc

Method RegisterFlagCompletionFunc

completions.go:170–183  ·  view source on GitHub ↗

RegisterFlagCompletionFunc should be called to register a function to provide completion for a flag. You can use pre-defined completion functions such as [FixedCompletions] or [NoFileCompletions], or you can define your own.

(flagName string, f CompletionFunc)

Source from the content-addressed store, hash-verified

168// You can use pre-defined completion functions such as [FixedCompletions] or [NoFileCompletions],
169// or you can define your own.
170func (c *Command) RegisterFlagCompletionFunc(flagName string, f CompletionFunc) error {
171 flag := c.Flag(flagName)
172 if flag == nil {
173 return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' does not exist", flagName)
174 }
175 flagCompletionMutex.Lock()
176 defer flagCompletionMutex.Unlock()
177
178 if _, exists := flagCompletionFunctions[flag]; exists {
179 return fmt.Errorf("RegisterFlagCompletionFunc: flag '%s' already registered", flagName)
180 }
181 flagCompletionFunctions[flag] = f
182 return nil
183}
184
185// GetFlagCompletionFunc returns the completion function for the given flag of the command, if available.
186func (c *Command) GetFlagCompletionFunc(flagName string) (CompletionFunc, bool) {

Calls 1

FlagMethod · 0.95