{"id":"afc3af6bb46720ee","repo":"spf13/cobra","slug":"unknown-command-q-for-q","errorCode":null,"errorMessage":"unknown command %q for %q","messagePattern":"unknown command %q for %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":44,"sourceCode":"// - root commands with subcommands will do subcommand validity checking\n// - subcommands will always accept arbitrary arguments\nfunc legacyArgs(cmd *Command, args []string) error {\n\t// no subcommand, always take args\n\tif !cmd.HasSubCommands() {\n\t\treturn nil\n\t}\n\n\t// root command with subcommands, do subcommand checking.\n\tif !cmd.HasParent() && len(args) > 0 {\n\t\treturn fmt.Errorf(\"unknown command %q for %q%s\", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))\n\t}\n\treturn nil\n}\n\n// NoArgs returns an error if any args are included.\nfunc NoArgs(cmd *Command, args []string) error {\n\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}","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L26-L62","documentation":"Returned by the NoArgs PositionalArgs validator when the user supplies any positional arguments to a command that is configured to accept none. Unlike legacyArgs, NoArgs is opt-in — you must explicitly assign it to Command.Args. It reports the first stray argument as an 'unknown command' because a no-arg command typically only has flag and subcommand surface.","triggerScenarios":"Assigning `Args: cobra.NoArgs` on a command and then invoking it with a positional token, e.g. `app version extra`. Also fires when a subcommand with NoArgs receives args intended for a sibling, or when shell completion logic forwards args incorrectly.","commonSituations":"Commands meant to be leaf/action-only (version, info, status) accidentally receiving args; arg-quoting bugs in shell wrappers; subcommands whose parent passes leftover args down.","solutions":["Remove the stray positional argument from the invocation.","If the command should accept those args, switch to a permissive validator (ArbitraryArgs) or a counting one (ExactArgs/MaximumNArgs).","If the arg was meant for a child command, fix the command path / routing."],"exampleFix":"// before\ncmd := &cobra.Command{Use: \"version\", Args: cobra.NoArgs}\n// `version 1.2.3` -> unknown command \"1.2.3\"\n\n// after: accept and ignore, or document\n// (remove the arg, or) cmd.Args = cobra.ArbitraryArgs","handlingStrategy":"validation","validationCode":"// Reject stray positionals before invoking a NoArgs command\nfunc ensureNoArgs(args []string) error {\n    if len(args) > 0 {\n        return fmt.Errorf(\"this command takes no positional args; got %v\", args)\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"// In RunE, treat the (already-validated) args as empty by contract\nRunE: func(cmd *cobra.Command, args []string) error {\n    // NoArgs guaranteed args is empty; proceed without indexing args\n}","preventionTips":["Document zero-arg commands clearly in Short/Long help.","Pair NoArgs with SilenceUsage=false so the user sees usage on failure.","Prefer NoArgs over ArbitraryArgs for leaf commands to fail fast on misuse."],"tags":["cli","args","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}