go
build_error
ai_generated
true
提供了标志但未定义:-foo
flag provided but not defined: -foo
ID: go/flag-provided-but-not-defined
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.
官方文档
https://pkg.go.dev/flag#FlagSet.Parse解决方案
-
在解析前定义标志:var foo string; flag.StringVar(&foo, "foo", "", "用法"); flag.Parse()
-
如果标志来自第三方工具,请查阅其文档并添加正确的标志定义
-
使用 flag.VisitAll() 列出所有已定义的标志,并验证预期的标志是否存在
无效尝试
常见但无效的做法:
-
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
-
Ignore the error and continue execution by catching it
95% 失败
flag.Parse() terminates on undefined flags; cannot be ignored
-
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