spf13/cobra · error

at least one of the flags in the group [%v] is required

Error message

at least one of the flags in the group [%v] is required

What it means

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.

Source

Thrown at flag_groups.go:183

}

func validateOneRequiredFlagGroups(data map[string]map[string]bool) error {
	keys := sortedKeys(data)
	for _, flagList := range keys {
		flagnameAndStatus := data[flagList]
		var set []string
		for flagname, isSet := range flagnameAndStatus {
			if isSet {
				set = append(set, flagname)
			}
		}
		if len(set) >= 1 {
			continue
		}

		// Sort values, so they can be tested/scripted against consistently.
		sort.Strings(set)
		return fmt.Errorf("at least one of the flags in the group [%v] is required", flagList)
	}
	return nil
}

func validateExclusiveFlagGroups(data map[string]map[string]bool) error {
	keys := sortedKeys(data)
	for _, flagList := range keys {
		flagnameAndStatus := data[flagList]
		var set []string
		for flagname, isSet := range flagnameAndStatus {
			if isSet {
				set = append(set, flagname)
			}
		}
		if len(set) == 0 || len(set) == 1 {
			continue
		}

View on GitHub (pinned to adbc881390)

Solutions

  1. Supply at least one of the flags listed in the group ([...]).
  2. If the requirement is wrong, remove MarkFlagsOneRequired.
  3. If exactly one specific flag is mandatory, use MarkFlagRequired instead.
  4. Bind an env-derived value to one of the flags so the group is satisfied.

Example fix

// before
cmd.Flags().String("token", "", "")
cmd.Flags().String("creds", "", "")
cmd.MarkFlagsOneRequired("token", "creds")
// `cmd` -> at least one of the flags in the group [...] is required

// after: `cmd --token xyz`
Defensive patterns

Strategy: validation

Validate before calling

// Confirm at least one alternative is present
cmd.PreRunE = func(c *cobra.Command, args []string) error {
    if !c.Flags().Changed("token") && !c.Flags().Changed("creds") {
        return errors.New("provide either --token or --creds")
    }
    return nil
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of spf13/cobra@adbc881390 (2026-08-04). Data as JSON: /data/errors/bcaebf7ed826d4e2.json. Report an issue: GitHub.