{"id":"251c5f47c4a3e562","repo":"spf13/cobra","slug":"accepts-d-arg-s-received-d","errorCode":null,"errorMessage":"accepts %d arg(s), received %d","messagePattern":"accepts (.+?) arg\\(s\\), received (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":110,"sourceCode":"\t\treturn nil\n\t}\n}\n\n// MaximumNArgs returns an error if there are more than N args.\nfunc MaximumNArgs(n int) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {\n\t\tif len(args) > n {\n\t\t\treturn fmt.Errorf(\"accepts at most %d arg(s), received %d\", n, len(args))\n\t\t}\n\t\treturn nil\n\t}\n}\n\n// ExactArgs returns an error if there are not exactly n args.\nfunc ExactArgs(n int) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {\n\t\tif len(args) != n {\n\t\t\treturn fmt.Errorf(\"accepts %d arg(s), received %d\", n, len(args))\n\t\t}\n\t\treturn nil\n\t}\n}\n\n// RangeArgs returns an error if the number of args is not within the expected range.\nfunc RangeArgs(min int, max int) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {\n\t\tif len(args) < min || len(args) > max {\n\t\t\treturn fmt.Errorf(\"accepts between %d and %d arg(s), received %d\", min, max, len(args))\n\t\t}\n\t\treturn nil\n\t}\n}\n\n// MatchAll allows combining several PositionalArgs to work in concert.\nfunc MatchAll(pargs ...PositionalArgs) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L92-L128","documentation":"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.","triggerScenarios":"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.","commonSituations":"Optional flags consuming an operand, quoting collapsing/expanding arg count, or a command whose arity changed between versions.","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."],"exampleFix":"// before\ncmd.Args = cobra.ExactArgs(2)\n// `cmd cp src` -> accepts 2 arg(s), received 1\n\n// after: `cmd cp src dst`","handlingStrategy":"validation","validationCode":"// Validate exact arity before Execute for a clearer message\ncmd.Args = cobra.ExactArgs(2)\ncmd.PreRunE = func(c *cobra.Command, args []string) error {\n    if len(args) != 2 { return fmt.Errorf(\"expected SRC and DST, got %d args\", len(args)) }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["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."],"tags":["cli","args","arity","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}