{"id":"378ebfa447a81dc6","repo":"spf13/cobra","slug":"required-flag-s-s-not-set","errorCode":null,"errorMessage":"required flag(s) \"%s\" not set","messagePattern":"required flag\\(s\\) \"(.+?)\" not set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":1198,"sourceCode":"func (c *Command) ValidateRequiredFlags() error {\n\tif c.DisableFlagParsing {\n\t\treturn nil\n\t}\n\n\tflags := c.Flags()\n\tmissingFlagNames := []string{}\n\tflags.VisitAll(func(pflag *flag.Flag) {\n\t\trequiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]\n\t\tif !found {\n\t\t\treturn\n\t\t}\n\t\tif (requiredAnnotation[0] == \"true\") && !pflag.Changed {\n\t\t\tmissingFlagNames = append(missingFlagNames, pflag.Name)\n\t\t}\n\t})\n\n\tif len(missingFlagNames) > 0 {\n\t\treturn fmt.Errorf(`required flag(s) \"%s\" not set`, strings.Join(missingFlagNames, `\", \"`))\n\t}\n\treturn nil\n}\n\n// checkCommandGroups checks if a command has been added to a group that does not exists.\n// If so, we panic because it indicates a coding error that should be corrected.\nfunc (c *Command) checkCommandGroups() {\n\tfor _, sub := range c.commands {\n\t\t// if Group is not defined let the developer know right away\n\t\tif sub.GroupID != \"\" && !c.ContainsGroup(sub.GroupID) {\n\t\t\tpanic(fmt.Sprintf(\"group id '%s' is not defined for subcommand '%s'\", sub.GroupID, sub.CommandPath()))\n\t\t}\n\n\t\tsub.checkCommandGroups()\n\t}\n}\n\n// InitDefaultHelpFlag adds default help flag to c.","sourceCodeStart":1180,"sourceCodeEnd":1216,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/command.go#L1180-L1216","documentation":"Returned by ValidateRequiredFlags when one or more flags marked required (via MarkFlagRequired / the BashCompOneRequiredFlag annotation) were not set on the command line. ValidateRequiredFlags runs during execute after flag parsing; it skips entirely if DisableFlagParsing is true. The message lists all missing required flag names joined by '\", \"'.","triggerScenarios":"Calling cmd.MarkFlagRequired(\"config\") and then invoking the command without --config. Also fires when a required flag is only set on a parent (persistent) but not merged, or when a required flag's name was typo'd in the MarkFlagRequired call so the user can never satisfy it.","commonSituations":"Forgetting a required flag at invocation, env-based config that was expected to set the flag but didn't, or a flag renamed in code while old docs/scripts still run.","solutions":["Supply every flag named in the error (e.g. --config <value>).","If the requirement is conditional, remove MarkFlagRequired and use MarkFlagsRequiredTogether or custom validation in RunE.","Verify the flag name passed to MarkFlagRequired matches a defined flag (a typo here makes the flag impossible to satisfy).","If flag parsing is intentionally disabled, note that ValidateRequiredFlags is skipped — required-flag semantics won't apply."],"exampleFix":"// before\ncmd.Flags().String(\"config\", \"\", \"config path\")\ncmd.MarkFlagRequired(\"config\")\n// `cmd run` -> required flag(s) \"config\" not set\n\n// after: `cmd run --config ./app.yaml`","handlingStrategy":"validation","validationCode":"// Verify required flags are satisfiable before shipping the command\nfunc ensureRequiredFlagsDefined(cmd *cobra.Command) error {\n    ok := true\n    cmd.Flags().VisitAll(func(f *pflag.Flag) {\n        if a, found := f.Annotations[cobra.BashCompOneRequiredFlag]; found && a[0] == \"true\" {\n            // confirm the flag exists and is settable\n            if cmd.Flags().Lookup(f.Name) == nil { ok = false }\n        }\n    })\n    if !ok { return errors.New(\"required flag references undefined flag\") }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"// In RunE, required flags are guaranteed present after ValidateRequiredFlags\nRunE: func(cmd *cobra.Command, args []string) error {\n    v, _ := cmd.Flags().GetString(\"config\") // safe: marked required, validated upstream\n    _ = v\n    return nil\n}","preventionTips":["Call MarkFlagRequired right after defining the flag, using the exact name.","For conditional requirements, use RunE-time checks instead of MarkFlagRequired.","Bind env vars to flags (viper) so required flags can be satisfied without CLI input."],"tags":["cli","flags","required","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}