spf13/cobra · error
accepts %d arg(s), received %d
Error message
accepts %d arg(s), received %d
What it means
Returned by ExactArgs(n) when the number of positional arguments is not exactly n. ExactArgs is the strictest of the counting validators — both too few and too many trigger it.
Source
Thrown at args.go:110
return nil
}
}
// MaximumNArgs returns an error if there are more than N args.
func MaximumNArgs(n int) PositionalArgs {
return func(cmd *Command, args []string) error {
if len(args) > n {
return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
}
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 {View on GitHub (pinned to adbc881390)
Solutions
- Provide exactly n positional arguments.
- Confirm flags that take values aren't stealing operands (use `=` form: `--flag=val`).
- If the real requirement is a range, switch to RangeArgs; if it's a minimum, use MinimumNArgs.
Example fix
// before cmd.Args = cobra.ExactArgs(2) // `cmd cp src` -> accepts 2 arg(s), received 1 // after: `cmd cp src dst`
Defensive patterns
Strategy: validation
Validate before calling
// Validate exact arity before Execute for a clearer message
cmd.Args = cobra.ExactArgs(2)
cmd.PreRunE = func(c *cobra.Command, args []string) error {
if len(args) != 2 { return fmt.Errorf("expected SRC and DST, got %d args", len(args)) }
return nil
} Type guard
null
Try / catch
null
Prevention
- Use ExactArgs only for fixed-arity commands (cp, mv, rename).
- Document operands positionally in Use (e.g. `Use: "cp SRC DST"`).
- Switch to RangeArgs if the true arity has any flex.
When it happens
Trigger: Setting `cmd.Args = cobra.ExactArgs(2)` and calling `cmd cp src` (one) or `cmd cp src dst extra` (three). Frequently seen with cp/mv-style commands that demand a fixed operand count.
Common situations: Optional flags consuming an operand, quoting collapsing/expanding arg count, or a command whose arity changed between versions.
Related errors
- requires at least %d arg(s), only received %d
- accepts at most %d arg(s), received %d
- accepts between %d and %d arg(s), received %d
- unknown command %q for %q%s
- unknown command %q for %q
AI-assisted analysis of spf13/cobra@adbc881390 (2026-08-04).
Data as JSON: /data/errors/251c5f47c4a3e562.json.
Report an issue: GitHub.