{"id":"bcaebf7ed826d4e2","repo":"spf13/cobra","slug":"at-least-one-of-the-flags-in-the-group-v-is-req","errorCode":null,"errorMessage":"at least one of the flags in the group [%v] is required","messagePattern":"at least one of the flags in the group \\[(.+?)\\] is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_groups.go","lineNumber":183,"sourceCode":"}\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}\n\n\t\t// Sort values, so they can be tested/scripted against consistently.\n\t\tsort.Strings(set)\n\t\treturn fmt.Errorf(\"at least one of the flags in the group [%v] is required\", flagList)\n\t}\n\treturn nil\n}\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","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/flag_groups.go#L165-L201","documentation":"Returned by validateOneRequiredFlagGroups when a command was marked with MarkFlagsOneRequired and NONE of the flags in the group were set. The annotation is cobra_annotation_one_required; the validator counts set flags in the group and errors only when the count is zero. This differs from a simple required flag: at least one of several alternatives must be present.","triggerScenarios":"Calling cmd.MarkFlagsOneRequired(\"config\",\"inline\") and invoking with neither --config nor --inline. Distinct from MarkFlagRequired (single mandatory flag) — here the user has a choice among the grouped flags.","commonSituations":"Auth commands requiring one of --token or --credentials-file, output commands requiring one of --format or --template, or any 'pick at least one alternative' requirement.","solutions":["Supply at least one of the flags listed in the group ([...]).","If the requirement is wrong, remove MarkFlagsOneRequired.","If exactly one specific flag is mandatory, use MarkFlagRequired instead.","Bind an env-derived value to one of the flags so the group is satisfied."],"exampleFix":"// before\ncmd.Flags().String(\"token\", \"\", \"\")\ncmd.Flags().String(\"creds\", \"\", \"\")\ncmd.MarkFlagsOneRequired(\"token\", \"creds\")\n// `cmd` -> at least one of the flags in the group [...] is required\n\n// after: `cmd --token xyz`","handlingStrategy":"validation","validationCode":"// Confirm at least one alternative is present\ncmd.PreRunE = func(c *cobra.Command, args []string) error {\n    if !c.Flags().Changed(\"token\") && !c.Flags().Changed(\"creds\") {\n        return errors.New(\"provide either --token or --creds\")\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use MarkFlagsOneRequired for 'pick at least one' semantics.","Distinguish from MarkFlagRequired (single mandatory) — they are not interchangeable.","Bind one alternative to an env var to ease non-interactive use."],"tags":["cli","flags","flag-groups","required","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}