{"id":"44200423d07f6a0b","repo":"spf13/cobra","slug":"requires-at-least-d-arg-s-only-received-d","errorCode":null,"errorMessage":"requires at least %d arg(s), only received %d","messagePattern":"requires at least (.+?) arg\\(s\\), only received (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":90,"sourceCode":"\t\tif _, ok := seen[arg]; ok {\n\t\t\treturn fmt.Errorf(\"duplicate argument %q for %q\", arg, cmd.CommandPath())\n\t\t}\n\t\tseen[arg] = struct{}{}\n\t}\n\n\treturn nil\n}\n\n// ArbitraryArgs never returns an error.\nfunc 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 {","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L72-L108","documentation":"Returned by MinimumNArgs(n) when fewer than n positional arguments are supplied. MinimumNArgs returns a PositionalArgs closure you assign to Command.Args; it fires during ValidateArgs at execute time.","triggerScenarios":"Setting `cmd.Args = cobra.MinimumNArgs(2)` and calling `cmd add 1` (one arg). Also fires when optional flags consume tokens the user thought were positional (interspersed parsing), or when shell quoting collapses multiple values into one.","commonSituations":"Missing required operands, flag values accidentally eating the next positional, or argparse-style expectations that don't match cobra's interspersed default.","solutions":["Supply at least n positional arguments.","Verify none of the missing tokens were swallowed by a preceding flag that expects a value.","Lower n if the minimum was set too high, or use RangeArgs if the true minimum is flexible.","Check shell quoting: `cmd \"a b\"` is one arg, not two."],"exampleFix":"// before\ncmd.Args = cobra.MinimumNArgs(2)\n// `cmd add 1` -> requires at least 2 arg(s), only received 1\n\n// after: `cmd add 1 2`  (or relax to MinimumNArgs(1))","handlingStrategy":"validation","validationCode":"// Count positionals (flags already parsed) and warn early\ncmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {\n    const min = 2\n    if len(args) < min {\n        return fmt.Errorf(\"need at least %d operands, got %d\", min, len(args))\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Use MinimumNArgs consistently for the same conceptual need across commands.","Verify flags that take values use '=' form in docs to avoid operand theft.","Document required operands in the command's Long help."],"tags":["cli","args","arity","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}