{"id":"66d8852790c310ad","repo":"spf13/cobra","slug":"registerflagcompletionfunc-flag-s-does-not-exi","errorCode":null,"errorMessage":"RegisterFlagCompletionFunc: flag '%s' does not exist","messagePattern":"RegisterFlagCompletionFunc: flag '(.+?)' does not exist","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"completions.go","lineNumber":173,"sourceCode":"// FixedCompletions can be used to create a completion function which always\n// returns the same results.\n//\n// This method returns a function that satisfies [CompletionFunc]\n// It can be used with [Command.RegisterFlagCompletionFunc] and for [Command.ValidArgsFunction].\nfunc FixedCompletions(choices []Completion, directive ShellCompDirective) CompletionFunc {\n\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","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/completions.go#L155-L191","documentation":"Returned by Command.RegisterFlagCompletionFunc when the named flag does not exist on the command's flag set (c.Flag(flagName) returns nil). Registration must happen after the flag is defined; the lookup uses the merged (local + persistent) flag set.","triggerScenarios":"Calling cmd.RegisterFlagCompletionFunc(\"output\", fn) before defining the --output flag, or with a typo'd flag name, or against a flag defined on a different command. Also fires when a persistent flag is defined on a parent but the child hasn't merged persistent flags at registration time.","commonSituations":"Order-of-initialization bugs (registering completion in an init() before flags exist), flag renamed but completion call not updated, or copy-paste from another command's setup.","solutions":["Define the flag (cmd.Flags().String(...)) BEFORE calling RegisterFlagCompletionFunc.","Spell the flag name exactly as defined (no leading dashes; long name only).","For persistent flags, ensure the parent has added the flag or call c.mergePersistentFlags() path by registering after AddCommand.","Guard the call: check cmd.Flag(name) != nil first, or handle the returned error."],"exampleFix":"// before (flag defined after registration)\ncmd.RegisterFlagCompletionFunc(\"output\", completionFn)\ncmd.Flags().String(\"output\", \"\", \"output format\")\n\n// after\ncmd.Flags().String(\"output\", \"\", \"output format\")\ncmd.RegisterFlagCompletionFunc(\"output\", completionFn)","handlingStrategy":"validation","validationCode":"// Guard registration against a missing flag\nif cmd.Flag(\"output\") == nil {\n    cmd.Flags().String(\"output\", \"\", \"output format\")\n}\nif err := cmd.RegisterFlagCompletionFunc(\"output\", completeOutput); err != nil {\n    log.Printf(\"skip completion: %v\", err)\n}","typeGuard":"null","tryCatchPattern":"if err := cmd.RegisterFlagCompletionFunc(name, fn); err != nil {\n    // non-fatal: completion is optional, log and continue\n    log.Printf(\"flag completion registration failed for %q: %v\", name, err)\n}","preventionTips":["Always define a flag before registering its completion.","Centralize flag+completion setup in one builder function to keep ordering correct.","Treat RegisterFlagCompletionFunc errors as non-fatal but logged."],"tags":["cli","completion","flags","cobra","initialization"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}