{"id":"163835648b3387f5","repo":"spf13/cobra","slug":"registerflagcompletionfunc-flag-s-already-regi","errorCode":null,"errorMessage":"RegisterFlagCompletionFunc: flag '%s' already registered","messagePattern":"RegisterFlagCompletionFunc: flag '(.+?)' already registered","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"completions.go","lineNumber":179,"sourceCode":"\treturn func(cmd *Command, args []string, toComplete string) ([]Completion, ShellCompDirective) {\n\t\treturn choices, directive\n\t}\n}\n\n// RegisterFlagCompletionFunc should be called to register a function to provide completion for a flag.\n//\n// You can use pre-defined completion functions such as [FixedCompletions] or [NoFileCompletions],\n// or you can define your own.\nfunc (c *Command) RegisterFlagCompletionFunc(flagName string, f CompletionFunc) error {\n\tflag := c.Flag(flagName)\n\tif flag == nil {\n\t\treturn fmt.Errorf(\"RegisterFlagCompletionFunc: flag '%s' does not exist\", flagName)\n\t}\n\tflagCompletionMutex.Lock()\n\tdefer flagCompletionMutex.Unlock()\n\n\tif _, exists := flagCompletionFunctions[flag]; exists {\n\t\treturn fmt.Errorf(\"RegisterFlagCompletionFunc: flag '%s' already registered\", flagName)\n\t}\n\tflagCompletionFunctions[flag] = f\n\treturn nil\n}\n\n// GetFlagCompletionFunc returns the completion function for the given flag of the command, if available.\nfunc (c *Command) GetFlagCompletionFunc(flagName string) (CompletionFunc, bool) {\n\tflag := c.Flag(flagName)\n\tif flag == nil {\n\t\treturn nil, false\n\t}\n\n\tflagCompletionMutex.RLock()\n\tdefer flagCompletionMutex.RUnlock()\n\n\tcompletionFunc, exists := flagCompletionFunctions[flag]\n\treturn completionFunc, exists\n}","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L161-L197","documentation":"Returned by Command.RegisterFlagCompletionFunc when a completion function has already been registered for that exact flag. The check uses the *flag.Flag pointer as the key in the global flagCompletionFunctions map, so it is per-flag-instance, not per-name.","triggerScenarios":"Calling RegisterFlagCompletionFunc twice for the same flag in the same process — common when setup code runs in an init() that executes multiple times (e.g. tests re-instantiating commands, or a command struct reused across runs).","commonSituations":"Test suites that rebuild the command tree per test without resetting state, shared helper that registers completion being called twice, or a subcommand whose completion was registered both at parent and child level.","solutions":["Register each flag's completion exactly once; move registration to a single setup function.","In tests, build a fresh command tree and avoid process-global double registration (the map is package-global).","Check the returned error and ignore/skip on already-registered if duplicate registration is benign in your flow.","Use GetFlagCompletionFunc to check existence before registering."],"exampleFix":"// before: helper called twice registers twice\nsetupCmd(cmd)\nsetupCmd(cmd) // error: already registered\n\n// after: register once, or guard\nif _, ok := cmd.GetFlagCompletionFunc(\"output\"); !ok {\n    cmd.RegisterFlagCompletionFunc(\"output\", fn)\n}","handlingStrategy":"validation","validationCode":"// Idempotent registration helper\nfunc registerCompletionOnce(cmd *cobra.Command, name string, fn cobra.CompletionFunc) error {\n    if _, ok := cmd.GetFlagCompletionFunc(name); ok {\n        return nil // already registered\n    }\n    return cmd.RegisterFlagCompletionFunc(name, fn)\n}","typeGuard":"null","tryCatchPattern":"if err := cmd.RegisterFlagCompletionFunc(name, fn); err != nil {\n    if strings.Contains(err.Error(), \"already registered\") { return nil }\n    return err\n}","preventionTips":["Register completion in a single setup function called once per process.","In tests, isolate the global completion map by avoiding double setup or by skipping completion in test fixtures.","Use GetFlagCompletionFunc to check before registering."],"tags":["cli","completion","flags","cobra","duplicate","initialization"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}