{"record":{"id":"40372966df976188","repo":"urfave/cli","slug":"flag-provided-but-not-defined-s","errorCode":null,"errorMessage":"flag provided but not defined: -%s","messagePattern":"flag provided but not defined: -(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command_parse.go","lineNumber":208,"sourceCode":"\n\t\t\tcontinue\n\t\t}\n\n\t\t// no flag lookup found and short handling is disabled\n\t\tif !shortOptionHandling {\n\t\t\t// In shell completion mode, preserve the partial flag so that DefaultCompleteWithFlags can use it\n\t\t\t// as lastArg and offer suggestions that match the prefix.\n\t\t\tif cmd.Root().shellCompletion {\n\t\t\t\tposArgs = append(posArgs, rargs...)\n\t\t\t\treturn &stringSliceArgs{posArgs}, nil\n\t\t\t}\n\t\t\t// When DefaultCommand is set, pass unknown flags through as positional args\n\t\t\t// so the default command can handle them (fixes #2249)\n\t\t\tif cmd.DefaultCommand != \"\" {\n\t\t\t\tposArgs = append(posArgs, rargs...)\n\t\t\t\treturn &stringSliceArgs{posArgs}, nil\n\t\t\t}\n\t\t\treturn &stringSliceArgs{posArgs}, fmt.Errorf(\"%s%s\", providedButNotDefinedErrMsg, flagName)\n\t\t}\n\n\t\t// try to split the flags\n\t\tfor index, c := range flagName {\n\t\t\ttracef(\"processing flag (fName=%[1]q)\", string(c))\n\t\t\tif sf := cmd.lookupFlag(string(c)); sf == nil {\n\t\t\t\tif index == 0 && cmd.DefaultCommand != \"\" {\n\t\t\t\t\tposArgs = append(posArgs, rargs...)\n\t\t\t\t\treturn &stringSliceArgs{posArgs}, nil\n\t\t\t\t}\n\t\t\t\treturn &stringSliceArgs{posArgs}, fmt.Errorf(\"%s%s\", providedButNotDefinedErrMsg, flagName)\n\t\t\t} else if fb, ok := sf.(boolFlag); ok && fb.IsBoolFlag() {\n\t\t\t\tfv := flagVal\n\t\t\t\tif index == (len(flagName)-1) && flagVal == \"\" {\n\t\t\t\t\tfv = \"true\"\n\t\t\t\t}\n\t\t\t\tif fv == \"\" {\n\t\t\t\t\tfv = \"true\"","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/urfave/cli/blob/1a4deb4f5a35ee12706602698dc0527d44c14b19/command_parse.go#L190-L226","documentation":"parseFlags encountered '--name' (long form) that does not match any flag registered on the command. Unless a DefaultCommand is set (in which case unknown flags are passed through as positional args), parsing fails with 'flag provided but not defined: -<name>'.","triggerScenarios":"Passing a long flag like --verbos instead of --verbose; passing a flag valid on a sibling subcommand; library version where the flag was removed or renamed.","commonSituations":"Typos in CI scripts; flags from an older CLI version after an upgrade; copying a command line between tools with similar but not identical flag sets.","solutions":["Correct the flag spelling against the command's help output (app <cmd> --help)","Verify the flag is declared in the Flags field of the exact command being run (flags are per-command unless persistent)","Move the flag to the right side of the subcommand it belongs to, or declare it on root/persistent flags"],"exampleFix":"// before\n$ app --verbos run\n// after\n$ app run --verbose","handlingStrategy":"validation","validationCode":"valid := map[string]bool{\"verbose\": true, \"config\": true}\nfor _, a := range os.Args[1:] {\n    if strings.HasPrefix(a, \"--\") {\n        name := strings.TrimPrefix(strings.SplitN(a, \"=\", 2)[0], \"--\")\n        if !valid[name] { fmt.Printf(\"unknown flag --%s\\n\", name) }\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(os.Args); err != nil {\n    if strings.Contains(err.Error(), \"flag provided but not defined\") {\n        cmd := cli.Command{Name: \"app\"}\n        _ = showHelp(cmd) // surface --help with valid flags\n        os.Exit(2)\n    }\n    return err\n}","preventionTips":["Run app <cmd> --help to confirm flag names before scripting","Declare shared flags as persistent/root flags so they work on every subcommand","Lint CI scripts after CLI version upgrades for renamed flags"],"tags":["go","cli","flags"],"backgroundTag":"unknown-flag","analyzedSha":"1a4deb4f5a35ee12706602698dc0527d44c14b19","analyzedAt":"2026-08-31T18:42:09.451Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}