{"id":"36a60ce9760d26df","repo":"spf13/cobra","slug":"accepts-between-d-and-d-arg-s-received-d","errorCode":null,"errorMessage":"accepts between %d and %d arg(s), received %d","messagePattern":"accepts between (.+?) and (.+?) arg\\(s\\), received (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":120,"sourceCode":"\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 {\n\t\tfor _, parg := range pargs {\n\t\t\tif err := parg(cmd, args); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t\treturn nil\n\t}\n}\n\n// ExactValidArgs returns an error if there are not exactly N positional args OR","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L102-L138","documentation":"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.","triggerScenarios":"Setting `cmd.Args = cobra.RangeArgs(2, 4)` and calling `cmd add 1` (below min) or `cmd add 1 2 3 4 5` (above max).","commonSituations":"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).","solutions":["Supply a positional count within [min, max] (both bounds inclusive).","Adjust min/max to the true operational range.","If only one bound matters, prefer MinimumNArgs or MaximumNArgs for clarity."],"exampleFix":"// before\ncmd.Args = cobra.RangeArgs(2, 4)\n// `cmd add 1` -> accepts between 2 and 4 arg(s), received 1\n\n// after: `cmd add 1 2`","handlingStrategy":"validation","validationCode":"if n := len(positionalArgs); n < min || n > max {\n    return fmt.Errorf(\"operand count %d outside [%d,%d]\", n, min, max)\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Remember both min and max are inclusive.","Prefer MinimumNArgs/MaximumNArgs when only one bound is real, for readability.","Reflect the accepted range in the Use string."],"tags":["cli","args","arity","range","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}