{"id":"c04460b487af4de1","repo":"spf13/cobra","slug":"if-any-flags-in-the-group-v-are-set-none-of-the","errorCode":null,"errorMessage":"if any flags in the group [%v] are set none of the others can be; %v were all set","messagePattern":"if any flags in the group \\[(.+?)\\] are set none of the others can be; (.+?) were all set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_groups.go","lineNumber":204,"sourceCode":"}\n\nfunc validateExclusiveFlagGroups(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) == 0 || len(set) == 1 {\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(set)\n\t\treturn fmt.Errorf(\"if any flags in the group [%v] are set none of the others can be; %v were all set\", flagList, set)\n\t}\n\treturn nil\n}\n\nfunc sortedKeys(m map[string]map[string]bool) []string {\n\tkeys := make([]string, len(m))\n\ti := 0\n\tfor k := range m {\n\t\tkeys[i] = k\n\t\ti++\n\t}\n\tsort.Strings(keys)\n\treturn keys\n}\n\n// enforceFlagGroupsForCompletion will do the following:\n// - when a flag in a group is present, other flags in the group will be marked required\n// - when none of the flags in a one-required group are present, all flags in the group will be marked required","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/flag_groups.go#L186-L222","documentation":"Returned by validateExclusiveFlagGroups when a command was marked with MarkFlagsMutuallyExclusive and the user set MORE THAN ONE flag from the group. The annotation is cobra_annotation_mutually_exclusive; the validator counts set flags and errors when two or more are set (zero or one is allowed). The 'set' slice in the message is sorted for deterministic output.","triggerScenarios":"Calling cmd.MarkFlagsMutuallyExclusive(\"json\",\"yaml\",\"text\") and invoking with `--json --yaml`. Any pair (or more) from the group being simultaneously set triggers the error.","commonSituations":"Conflicting output-format flags, mutually exclusive verbose/quiet, or --dry-run vs --apply. Often hit when a wrapper script passes both or when a default value makes a flag count as 'set'.","solutions":["Pass at most ONE flag from the mutually exclusive group.","If a flag has a default that makes it appear 'set', restructure so the default doesn't trigger Changed.","If the flags are not actually exclusive, remove MarkFlagsMutuallyExclusive.","Note: a flag counts as set only if Changed (user supplied it); defaults alone do not set Changed."],"exampleFix":"// before\ncmd.Flags().Bool(\"json\", false, \"\")\ncmd.Flags().Bool(\"yaml\", false, \"\")\ncmd.MarkFlagsMutuallyExclusive(\"json\", \"yaml\")\n// `cmd --json --yaml` -> ...none of the others can be; [json yaml] were all set\n\n// after: `cmd --json`  (pick one)","handlingStrategy":"validation","validationCode":"// Reject conflicting flags before business logic\ncmd.PreRunE = func(c *cobra.Command, args []string) error {\n    set := []string{}\n    for _, f := range []string{\"json\",\"yaml\",\"text\"} { if c.Flags().Changed(f) { set = append(set, f) } }\n    if len(set) > 1 { return fmt.Errorf(\"conflicting output flags: %v\", set) }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use MarkFlagsMutuallyExclusive for 'pick at most one' semantics.","Remember only user-supplied flags count (Changed); defaults don't trigger the error.","Avoid defaults that look set — defaults never set Changed to true."],"tags":["cli","flags","flag-groups","mutually-exclusive","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}