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)
| 168 | // You can use pre-defined completion functions such as [FixedCompletions] or [NoFileCompletions], |
| 169 | // or you can define your own. |
| 170 | func (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. |
| 186 | func (c *Command) GetFlagCompletionFunc(flagName string) (CompletionFunc, bool) { |