go build_error ai_generated true

flag provided but not defined: -foo

ID: go/flag-provided-but-not-defined

Also available as: JSON · Markdown · 中文
97%Fix Rate
88%Confidence
1Evidence
2023-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
go1.20 active
go1.21 active
go1.22 active

Root Cause

A command-line flag passed to a Go program that does not have a corresponding flag definition using the flag package.

generic

中文

向 Go 程序传递的命令行标志没有使用 flag 包进行相应的标志定义。

Official Documentation

https://pkg.go.dev/flag#FlagSet.Parse

Workarounds

  1. 97% success Define the flag before parsing: var foo string; flag.StringVar(&foo, "foo", "", "usage"); flag.Parse()
    Define the flag before parsing: var foo string; flag.StringVar(&foo, "foo", "", "usage"); flag.Parse()
  2. 95% success If the flag is from a third-party tool, check its documentation and add the correct flag definition
    If the flag is from a third-party tool, check its documentation and add the correct flag definition
  3. 90% success Use flag.VisitAll() to list all defined flags and verify the intended flag exists
    Use flag.VisitAll() to list all defined flags and verify the intended flag exists

中文步骤

  1. 在解析前定义标志:var foo string; flag.StringVar(&foo, "foo", "", "用法"); flag.Parse()
  2. 如果标志来自第三方工具,请查阅其文档并添加正确的标志定义
  3. 使用 flag.VisitAll() 列出所有已定义的标志,并验证预期的标志是否存在

Dead Ends

Common approaches that don't work:

  1. Change the flag name in the command line to match an existing flag 50% fail

    May not be the intended flag; could cause incorrect behavior if the wrong flag is used

  2. Ignore the error and continue execution by catching it 95% fail

    flag.Parse() terminates on undefined flags; cannot be ignored

  3. Use a third-party flag library that doesn't report this error 60% fail

    Switching libraries may introduce compatibility issues and doesn't fix the missing definition