{"id":"467af2b6a81dd651","repo":"spf13/cobra","slug":"if-any-flags-in-the-group-v-are-set-they-must-a","errorCode":null,"errorMessage":"if any flags in the group [%v] are set they must all be set; missing %v","messagePattern":"if any flags in the group \\[(.+?)\\] are set they must all be set; missing (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_groups.go","lineNumber":161,"sourceCode":"\nfunc validateRequiredFlagGroups(data map[string]map[string]bool) error {\n\tkeys := sortedKeys(data)\n\tfor _, flagList := range keys {\n\t\tflagnameAndStatus := data[flagList]\n\n\t\tunset := []string{}\n\t\tfor flagname, isSet := range flagnameAndStatus {\n\t\t\tif !isSet {\n\t\t\t\tunset = append(unset, flagname)\n\t\t\t}\n\t\t}\n\t\tif len(unset) == len(flagnameAndStatus) || len(unset) == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\t// Sort values, so they can be tested/scripted against consistently.\n\t\tsort.Strings(unset)\n\t\treturn fmt.Errorf(\"if any flags in the group [%v] are set they must all be set; missing %v\", flagList, unset)\n\t}\n\n\treturn nil\n}\n\nfunc validateOneRequiredFlagGroups(data map[string]map[string]bool) error {\n\tkeys := sortedKeys(data)\n\tfor _, flagList := range keys {\n\t\tflagnameAndStatus := data[flagList]\n\t\tvar set []string\n\t\tfor flagname, isSet := range flagnameAndStatus {\n\t\t\tif isSet {\n\t\t\t\tset = append(set, flagname)\n\t\t\t}\n\t\t}\n\t\tif len(set) >= 1 {\n\t\t\tcontinue\n\t\t}","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/flag_groups.go#L143-L179","documentation":"Returned by validateRequiredFlagGroups when a command was marked with MarkFlagsRequiredTogether and the user set SOME but not ALL of the flags in that group. The group annotation is cobra_annotation_required_if_others_set; the validator collects set/unset status per group and errors if the group is partially satisfied. flagList in the message is the space-joined flag names; the missing slice is sorted for deterministic output.","triggerScenarios":"Calling cmd.MarkFlagsRequiredTogether(\"cert\",\"key\") and invoking with only --cert. The validator skips the group only if all flags are unset OR all are set; any partial set triggers the error.","commonSituations":"TLS cert/key pairs, username/password pairs, or any co-dependent options where the user supplies one half via CLI and the other via env/config that doesn't flow into the same flag.","solutions":["Set ALL flags named in the error's missing list.","If the flags are not actually co-dependent, remove the MarkFlagsRequiredTogether call.","If one is sourced from env, bind it to the flag (e.g. viper.BindPFlag) so it counts as set.","DisableFlagParsing suppresses ValidateFlagGroups entirely — only use if parsing is intentionally off."],"exampleFix":"// before\ncmd.Flags().String(\"cert\", \"\", \"\")\ncmd.Flags().String(\"key\", \"\", \"\")\ncmd.MarkFlagsRequiredTogether(\"cert\", \"key\")\n// `cmd --cert foo.pem` -> ...missing [key]\n\n// after: `cmd --cert foo.pem --key bar.key`","handlingStrategy":"validation","validationCode":"// Pre-flight: confirm co-dependent flags are all set or all unset\ncmd.PreRunE = func(c *cobra.Command, args []string) error {\n    a, _ := c.Flags().GetString(\"cert\"); b, _ := c.Flags().GetString(\"key\")\n    aSet := c.Flags().Changed(\"cert\"); bSet := c.Flags().Changed(\"key\")\n    if aSet != bSet {\n        return fmt.Errorf(\"--cert and --key must be supplied together (cert=%q key=%q)\", a, b)\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Bind env-sourced values to the flag so Changed reflects them.","Document co-dependencies in flag usage strings.","Prefer MarkFlagsRequiredTogether over hand-rolled checks for the common case."],"tags":["cli","flags","flag-groups","required","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}