spf13/cobra · error

accepts between %d and %d arg(s), received %d

Error message

accepts between %d and %d arg(s), received %d

What it means

Returned by RangeArgs(min, max) when the positional argument count falls outside [min, max]. RangeArgs is the flexible counting validator for commands that accept a bounded variable number of operands.

Source

Thrown at args.go:120

		return nil
	}
}

// ExactArgs returns an error if there are not exactly n args.
func ExactArgs(n int) PositionalArgs {
	return func(cmd *Command, args []string) error {
		if len(args) != n {
			return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
		}
		return nil
	}
}

// RangeArgs returns an error if the number of args is not within the expected range.
func RangeArgs(min int, max int) PositionalArgs {
	return func(cmd *Command, args []string) error {
		if len(args) < min || len(args) > max {
			return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
		}
		return nil
	}
}

// MatchAll allows combining several PositionalArgs to work in concert.
func MatchAll(pargs ...PositionalArgs) PositionalArgs {
	return func(cmd *Command, args []string) error {
		for _, parg := range pargs {
			if err := parg(cmd, args); err != nil {
				return err
			}
		}
		return nil
	}
}

// ExactValidArgs returns an error if there are not exactly N positional args OR

View on GitHub (pinned to adbc881390)

Solutions

  1. Supply a positional count within [min, max] (both bounds inclusive).
  2. Adjust min/max to the true operational range.
  3. If only one bound matters, prefer MinimumNArgs or MaximumNArgs for clarity.

Example fix

// before
cmd.Args = cobra.RangeArgs(2, 4)
// `cmd add 1` -> accepts between 2 and 4 arg(s), received 1

// after: `cmd add 1 2`
Defensive patterns

Strategy: validation

Validate before calling

if n := len(positionalArgs); n < min || n > max {
    return fmt.Errorf("operand count %d outside [%d,%d]", n, min, max)
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Setting `cmd.Args = cobra.RangeArgs(2, 4)` and calling `cmd add 1` (below min) or `cmd add 1 2 3 4 5` (above max).

Common situations: Commands like `tag` that need 2-N tags, batch processors with a cap, or off-by-one in setting min/max (min and max inclusive on both ends).

Related errors


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