3 回答
TA贡献1818条经验 获得超8个赞
这也让我绊倒了,因为我flag.String/flag.Int64/etc在我的应用程序中调用了几个地方,我不想到处传递一个新flag.FlagSet的。
// If a commandline app works like this: ./app subcommand -flag -flag2
// `flag.Parse` won't parse anything after `subcommand`.
// To still be able to use `flag.String/flag.Int64` etc without creating
// a new `flag.FlagSet`, we need this hack to find the first arg that has a dash
// so we know when to start parsing
firstArgWithDash := 1
for i := 1; i < len(os.Args); i++ {
firstArgWithDash = i
if len(os.Args[i]) > 0 && os.Args[i][0] == '-' {
break
}
}
flag.CommandLine.Parse(os.Args[firstArgWithDash:])
我这样做的原因是因为无论如何flag.Parse只是flag.CommandLine.Parse(os.Args[1:])在幕后打电话。
- 3 回答
- 0 关注
- 280 浏览
添加回答
举报