{"id":"77f653b6f7f02441","repo":"spf13/cobra","slug":"accepts-at-most-d-arg-s-received-d","errorCode":null,"errorMessage":"accepts at most %d arg(s), received %d","messagePattern":"accepts at most (.+?) arg\\(s\\), received (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":100,"sourceCode":"func ArbitraryArgs(cmd *Command, args []string) error {\n\treturn nil\n}\n\n// MinimumNArgs returns an error if there is not at least N args.\nfunc MinimumNArgs(n int) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {\n\t\tif len(args) < n {\n\t\t\treturn fmt.Errorf(\"requires at least %d arg(s), only received %d\", n, len(args))\n\t\t}\n\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 {","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L82-L118","documentation":"Returned by MaximumNArgs(n) when more than n positional arguments are supplied. MaximumNArgs is a PositionalArgs factory assigned to Command.Args; it caps the accepted positional count.","triggerScenarios":"Setting `cmd.Args = cobra.MaximumNArgs(1)` and calling `cmd set a b c`. Common when a command takes at most one value but the user passes a list, or when glob expansion yields more files than expected.","commonSituations":"Unbounded shell globs (`cmd *.txt` matching many files), list-vs-scalar confusion, or flags that were expected to repeat but the user passed values as positionals instead.","solutions":["Reduce positional arguments to at most n.","If the command should accept many, raise n or switch to ArbitraryArgs.","Move repeated values behind a repeating flag (StringSlice / StringArray) instead of positionals."],"exampleFix":"// before\ncmd.Args = cobra.MaximumNArgs(1)\n// `cmd set a b` -> accepts at most 1 arg(s), received 2\n\n// after: `cmd set a`  (or use a slice flag)","handlingStrategy":"validation","validationCode":"if len(positionalArgs) > maxN {\n    return fmt.Errorf(\"too many operands (%d > %d); pass extras via --repeat flag\", len(positionalArgs), maxN)\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["For variadic input, prefer a repeating flag (StringArray/StringSlice) over positionals.","Cap globs in shell scripts before passing to the command.","Pick MaximumNArgs only when a hard upper bound truly exists."],"tags":["cli","args","arity","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}