spf13/cobra · error

if any flags in the group [%v] are set none of the others ca

Error message

if any flags in the group [%v] are set none of the others can be; %v were all set

What it means

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.

Source

Thrown at flag_groups.go:204

}

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
		}

		// Sort values, so they can be tested/scripted against consistently.
		sort.Strings(set)
		return fmt.Errorf("if any flags in the group [%v] are set none of the others can be; %v were all set", flagList, set)
	}
	return nil
}

func sortedKeys(m map[string]map[string]bool) []string {
	keys := make([]string, len(m))
	i := 0
	for k := range m {
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	return keys
}

// enforceFlagGroupsForCompletion will do the following:
// - when a flag in a group is present, other flags in the group will be marked required
// - when none of the flags in a one-required group are present, all flags in the group will be marked required

View on GitHub (pinned to adbc881390)

Solutions

  1. Pass at most ONE flag from the mutually exclusive group.
  2. If a flag has a default that makes it appear 'set', restructure so the default doesn't trigger Changed.
  3. If the flags are not actually exclusive, remove MarkFlagsMutuallyExclusive.
  4. Note: a flag counts as set only if Changed (user supplied it); defaults alone do not set Changed.

Example fix

// before
cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("yaml", false, "")
cmd.MarkFlagsMutuallyExclusive("json", "yaml")
// `cmd --json --yaml` -> ...none of the others can be; [json yaml] were all set

// after: `cmd --json`  (pick one)
Defensive patterns

Strategy: validation

Validate before calling

// Reject conflicting flags before business logic
cmd.PreRunE = func(c *cobra.Command, args []string) error {
    set := []string{}
    for _, f := range []string{"json","yaml","text"} { if c.Flags().Changed(f) { set = append(set, f) } }
    if len(set) > 1 { return fmt.Errorf("conflicting output flags: %v", set) }
    return nil
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling cmd.MarkFlagsMutuallyExclusive("json","yaml","text") and invoking with `--json --yaml`. Any pair (or more) from the group being simultaneously set triggers the error.

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

Related errors


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