go build_error ai_generated true

提供了标志但未定义:-foo

flag provided but not defined: -foo

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

其他格式: JSON · Markdown 中文 · English
97%修复率
88%置信度
1证据数
2023-04-05首次发现

版本兼容性

版本状态引入弃用备注
go1.20 active
go1.21 active
go1.22 active

根因分析

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

English

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

generic

官方文档

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

解决方案

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

无效尝试

常见但无效的做法:

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

    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% 失败

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

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

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