{"id":"0b11ec66f2fbed6d","repo":"spf13/cobra","slug":"invalid-argument-q-for-q-s","errorCode":null,"errorMessage":"invalid argument %q for %q%s","messagePattern":"invalid argument %q for %q(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":61,"sourceCode":"\tif len(args) > 0 {\n\t\treturn fmt.Errorf(\"unknown command %q for %q\", args[0], cmd.CommandPath())\n\t}\n\treturn nil\n}\n\n// OnlyValidArgs returns an error if there are any positional args that are not in\n// the `ValidArgs` field of `Command`\nfunc OnlyValidArgs(cmd *Command, args []string) error {\n\tif len(cmd.ValidArgs) > 0 {\n\t\t// Remove any description that may be included in ValidArgs.\n\t\t// A description is following a tab character.\n\t\tvalidArgs := make([]string, 0, len(cmd.ValidArgs))\n\t\tfor _, v := range cmd.ValidArgs {\n\t\t\tvalidArgs = append(validArgs, strings.SplitN(v, \"\\t\", 2)[0])\n\t\t}\n\t\tfor _, v := range args {\n\t\t\tif !stringInSlice(v, validArgs) {\n\t\t\t\treturn fmt.Errorf(\"invalid argument %q for %q%s\", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// NoDuplicateArgs returns an error if there are any duplicate positional args.\nfunc NoDuplicateArgs(cmd *Command, args []string) error {\n\tseen := make(map[string]struct{}, len(args))\n\tfor _, arg := range args {\n\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}","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L43-L79","documentation":"Returned by OnlyValidArgs when a positional argument is not present in the command's ValidArgs slice. OnlyValidArgs is applied via Command.Args (often combined with MatchAll). The %s suffix appends findSuggestions for close matches against ValidArgs.","triggerScenarios":"Setting `cmd.ValidArgs = []string{\"start\",\"stop\"}` and `cmd.Args = cobra.OnlyValidArgs`, then calling `cmd statr` (typo) or `cmd pause` (not in list). ValidArgs entries may include a tab-delimited description; OnlyValidArgs strips the description before comparing.","commonSituations":"Typos in allowed values, a value list that wasn't updated when new options were added, case sensitivity surprises (matching is exact), or trailing whitespace in the typed arg.","solutions":["Use one of the values listed in ValidArgs (check the suggestion text for close matches).","Add the intended value to cmd.ValidArgs if it should be permitted.","Combine with ExactArgs/MatchAll correctly — ensure OnlyValidArgs is actually the validator you want rather than a stricter one.","Trim/normalize input before validation if whitespace or case is the issue."],"exampleFix":"// before\ncmd.ValidArgs = []string{\"start\", \"stop\"}\ncmd.Args = cobra.OnlyValidArgs\n// `cmd pause` -> invalid argument \"pause\"\n\n// after\ncmd.ValidArgs = []string{\"start\", \"stop\", \"pause\"}","handlingStrategy":"validation","validationCode":"// Pre-check positional args against ValidArgs before Execute\ncmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {\n    if len(c.ValidArgs) == 0 { return nil }\n    allowed := map[string]struct{}{}\n    for _, v := range c.ValidArgs { allowed[strings.SplitN(v, \"\\t\", 2)[0]] = struct{}{} }\n    for _, a := range args {\n        if _, ok := allowed[a]; !ok {\n            return fmt.Errorf(\"%q is not a valid value; allowed: %v\", a, c.ValidArgs)\n        }\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Keep ValidArgs and the actual handling logic in the same source location so they stay in sync.","Generate ValidArgs from an enum/const list to avoid drift.","Normalize (trim/lowercase) input upstream if case or whitespace matters."],"tags":["cli","args","validargs","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}