{"record":{"id":"6125709ce1a14eac","repo":"golangci/golangci-lint","slug":"can-t-parse-args-w","errorCode":null,"errorMessage":"can't parse args: %w","messagePattern":"can't parse args: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/root.go","lineNumber":147,"sourceCode":"\t// Ignore unknown flags because we will parse the command flags later.\n\tfs.ParseErrorsAllowlist = pflag.ParseErrorsAllowlist{UnknownFlags: true}\n\n\topts := &rootOptions{}\n\n\t// Don't do `fs.AddFlagSet(cmd.Flags())` because it shares flags representations:\n\t// `changed` variable inside string slice vars will be shared.\n\t// Use another config variable here,\n\t// to not affect main parsing by this parsing of only config option.\n\tsetupRootPersistentFlags(fs, opts)\n\n\tfs.Usage = func() {} // otherwise, help text will be printed twice\n\n\tif err := fs.Parse(safeArgs(fs, os.Args)); err != nil {\n\t\tif errors.Is(err, pflag.ErrHelp) {\n\t\t\treturn nil, err\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"can't parse args: %w\", err)\n\t}\n\n\treturn opts, nil\n}\n\n// Shorthands are a problem because pflag, with UnknownFlags, will try to parse all the letters as options.\n// A shorthand can aggregate several letters (ex `ps -aux`)\n// The function replaces non-supported shorthands by a dumb flag.\nfunc safeArgs(fs *pflag.FlagSet, args []string) []string {\n\tvar shorthands []string\n\tfs.VisitAll(func(flag *pflag.Flag) {\n\t\tshorthands = append(shorthands, flag.Shorthand)\n\t})\n\n\tvar cleanArgs []string\n\tfor _, arg := range args {\n\t\tif len(arg) > 1 && arg[0] == '-' && arg[1] != '-' && !slices.Contains(shorthands, string(arg[1])) {\n\t\t\tcleanArgs = append(cleanArgs, \"--potato\")","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/root.go#L129-L165","documentation":"Before the logger is set up, golangci-lint force-parses the command-line arguments with pflag (using UnknownFlags) to extract logging options. If pflag cannot parse the arguments (and it isn't a help request), the error is wrapped as `can't parse args: %w`. This happens very early, before any command logic runs.","triggerScenarios":"Malformed command-line arguments: a shorthand that pflag tries to interpret letter-by-letter under UnknownFlags (e.g. `-=x`, stray `-` values), an `=` in an unexpected place, or arguments that look like flags but aren't valid for any command.","commonSituations":"Pasting commands with smart quotes or stray characters; passing unknown shorthands like `-abc`; scripts building args dynamically and injecting an empty or garbage token; flag typos that break early parsing before cobra sees them.","solutions":["Fix or remove the malformed argument flagged in the wrapped pflag error.","Use long flags (--flag=value) instead of clustered/unknown shorthands.","Quote arguments containing special characters (especially paths and values with spaces or '=').","Inspect the exact os.Args being passed if launched from a script or wrapper."],"exampleFix":"// before\ngolangci-lint run -file=main.go\n// after\ngolangci-lint run --file=main.go  # or the correct flag, e.g. --show-stats","handlingStrategy":"validation","validationCode":"// sanity-check args before invoking the binary\nfor _, a := range args {\n    if a == \"\" || strings.ContainsAny(a, \"\\u2018\\u2019\\u201c\\u201d\") {\n        return fmt.Errorf(\"malformed argument: %q\", a)\n    }\n}","typeGuard":null,"tryCatchPattern":"if _, err := forceRootParsePersistentFlags(); err != nil {\n    if errors.Is(err, pflag.ErrHelp) {\n        return nil // help requested, not a failure\n    }\n    log.Fatalf(\"arg parse failed: %v\", err) // inspect the wrapped pflag cause\n}","preventionTips":["Prefer long flags (--flag=value) over unknown shorthand clusters.","Quote arguments containing spaces or special characters.","Avoid smart quotes when copying commands from docs/Slack.","Log os.Args in wrapper scripts to catch injected garbage tokens."],"tags":["go","cli","arguments","pflag"],"backgroundTag":"invalid-flag-value","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}