{"record":{"id":"64d90002a1a95f44","repo":"urfave/cli","slug":"could-not-parse-1-q-as-2-t-value-from-3-s-fo-64d900","errorCode":null,"errorMessage":"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s","messagePattern":"could not parse %\\[1\\]q as %\\[2\\]T value from %\\[3\\]s for flag %\\[4\\]s: %\\[5\\]s","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_impl.go","lineNumber":144,"sourceCode":"}\n\n// PostParse populates the flag given the flag set and environment\nfunc (f *FlagBase[T, C, V]) PostParse() error {\n\ttracef(\"postparse (flag=%[1]q)\", f.Name)\n\n\tif !f.hasBeenSet {\n\t\tif val, source, found := f.Sources.LookupWithSource(); found {\n\t\t\t// reflect.TypeOf yields nil when T is an interface type (e.g.\n\t\t\t// GenericFlag) and the value is nil, so the kind has to be\n\t\t\t// derived defensively.\n\t\t\tkind := reflect.Invalid\n\t\t\tif ty := reflect.TypeOf(f.Value); ty != nil {\n\t\t\t\tkind = ty.Kind()\n\t\t\t}\n\n\t\t\tif val != \"\" || kind == reflect.String {\n\t\t\t\tif err := f.Set(f.Name, val); err != nil {\n\t\t\t\t\treturn fmt.Errorf(\n\t\t\t\t\t\t\"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s\",\n\t\t\t\t\t\tval, f.Value, source, f.Name, err,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if val == \"\" && kind == reflect.Bool {\n\t\t\t\t_ = f.Set(f.Name, \"false\")\n\t\t\t}\n\n\t\t\tf.hasBeenSet = true\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// pass configuration of parsing to value\nfunc (f *FlagBase[T, C, V]) setMultiValueParsingConfig(c multiValueParsingConfig) {\n\ttracef(\"setMultiValueParsingConfig %T, %+v\", f.value, f.value)","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/urfave/cli/blob/1a4deb4f5a35ee12706602698dc0527d44c14b19/flag_impl.go#L126-L162","documentation":"When a flag value arrives from an external source (env var, config file, etc.) during PostParse, the library calls flag.Set and wraps any failure in this message. It reports the raw value, the Go type of the flag's Value, where the value came from, the flag name, and the underlying parse error — e.g. parsing \"abc\" as an IntFlag from an env var fails.","triggerScenarios":"A flag's Sources (env var, file, config) yields a string that cannot be converted to the flag's type: non-numeric string into an Int/Float flag, invalid bool string like \"yes\" or \"on\" into a Bool flag (only \"true\"/\"false\"/\"1\"/\"0\" etc. are accepted), or malformed duration into a Duration flag.","commonSituations":"MY_FLAG=production where the flag is a bool; quoted values in env (MY_PORT=\"8080\" with literal quotes on Windows); whitespace or typos in config files; version changes where a flag changed type from string to int.","solutions":["Read the wrapped %[5]s cause and fix the value at the cited source (%[3]s — the env var or file) to match the flag's type.","For bool flags set from env, use exactly \"true\"/\"false\" (empty string is treated as \"false\"), not \"yes\"/\"1 (string)\" variants unsupported by strconv.ParseBool.","Validate or sanitize env/config values before the app parses them, e.g. strip quotes/whitespace.","Run with the offending env var unset to confirm the source is the culprit, then correct or remove it."],"exampleFix":"// before\nexport TIMEOUT=10s\\ 30s\n\n// after\nexport TIMEOUT=30s","handlingStrategy":"validation","validationCode":"// validate env-sourced values before running the command\ncase \"$FLAG_TYPE\" in\n  bool) [[ \"$MY_FLAG\" =~ ^(true|false|1|0|t|f|T|F|TRUE|FALSE|True|False)$ ]] || { echo \"MY_FLAG must be a bool\" >&2; exit 1; } ;;\n  int)  [[ \"$MY_PORT\" =~ ^-?[0-9]+$ ]] || { echo \"MY_PORT must be an integer\" >&2; exit 1; } ;;\nesac","typeGuard":"func isParsableBool(s string) bool { _, err := strconv.ParseBool(strings.TrimSpace(s)); return err == nil }\nfunc isParsableInt(s string) bool { _, err := strconv.Atoi(strings.TrimSpace(s)); return err == nil }","tryCatchPattern":"if err := cmd.Run(ctx, os.Args); err != nil {\n    var msg string\n    if strings.Contains(err.Error(), \"could not parse\") {\n        fmt.Fprintf(os.Stderr, \"fix the env/config value: %v\\n\", err)\n        os.Exit(2)\n    }\n    _ = msg\n    return err\n}","preventionTips":["Keep env values in the exact format the flag type expects (\"true\"/\"false\" for bools, plain integers for ints).","Strip quotes and whitespace when exporting values from config files.","Test the app locally with the same env vars used in CI.","When changing a flag's type across versions, migrate the env/config values too."],"tags":["cli","flags","parsing","environment-variables","go"],"backgroundTag":"flag-value-parse-failed","analyzedSha":"1a4deb4f5a35ee12706602698dc0527d44c14b19","analyzedAt":"2026-08-31T18:42:09.451Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}