spf13/cobra · error

if any flags in the group [%v] are set they must all be set;

Error message

if any flags in the group [%v] are set they must all be set; missing %v

What it means

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.

Source

Thrown at flag_groups.go:161

func validateRequiredFlagGroups(data map[string]map[string]bool) error {
	keys := sortedKeys(data)
	for _, flagList := range keys {
		flagnameAndStatus := data[flagList]

		unset := []string{}
		for flagname, isSet := range flagnameAndStatus {
			if !isSet {
				unset = append(unset, flagname)
			}
		}
		if len(unset) == len(flagnameAndStatus) || len(unset) == 0 {
			continue
		}

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

	return nil
}

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
		}

View on GitHub (pinned to adbc881390)

Solutions

  1. Set ALL flags named in the error's missing list.
  2. If the flags are not actually co-dependent, remove the MarkFlagsRequiredTogether call.
  3. If one is sourced from env, bind it to the flag (e.g. viper.BindPFlag) so it counts as set.
  4. DisableFlagParsing suppresses ValidateFlagGroups entirely — only use if parsing is intentionally off.

Example fix

// before
cmd.Flags().String("cert", "", "")
cmd.Flags().String("key", "", "")
cmd.MarkFlagsRequiredTogether("cert", "key")
// `cmd --cert foo.pem` -> ...missing [key]

// after: `cmd --cert foo.pem --key bar.key`
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: confirm co-dependent flags are all set or all unset
cmd.PreRunE = func(c *cobra.Command, args []string) error {
    a, _ := c.Flags().GetString("cert"); b, _ := c.Flags().GetString("key")
    aSet := c.Flags().Changed("cert"); bSet := c.Flags().Changed("key")
    if aSet != bSet {
        return fmt.Errorf("--cert and --key must be supplied together (cert=%q key=%q)", a, b)
    }
    return nil
}

Type guard

null

Try / catch

null

Prevention

When it happens

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

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

Related errors


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