2 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果打算實(shí)現(xiàn)子命令,則不應(yīng)調(diào)用 flag。解析())。
相反,請決定使用哪個(gè)子命令(就像您所做的那樣),并僅調(diào)用其 FlagSet.Parse() 方法。os.Args[1]
是的,要使此操作有效,所有標(biāo)志集都應(yīng)包含公共標(biāo)志。但是,只需注冊一次(在一個(gè)地方)就很容易了。創(chuàng)建包級別變量:
var (
required string
fooCmd = flag.NewFlagSet("foo", flag.ExitOnError)
barCmd = flag.NewFlagSet("bar", flag.ExitOnError)
)
使用循環(huán)遍歷所有標(biāo)志集,并注冊公共標(biāo)志,使用FlagSet.StringVar()指向您的變量:
func setupCommonFlags() {
for _, fs := range []*flag.FlagSet{fooCmd, barCmd} {
fs.StringVar(
&required,
"required",
"",
"required for all commands",
)
}
}
并在調(diào)用適當(dāng)?shù)臉?biāo)志集,并在之后進(jìn)行測試:main()Parse()required
func main() {
setupCommonFlags()
switch os.Args[1] {
case "foo":
fooCmd.Parse(os.Args[2:])
fmt.Println("foo")
case "bar":
barCmd.Parse(os.Args[2:])
fmt.Println("bar")
default:
log.Fatalf("[ERROR] unknown subcommand '%s', see help for more details.", os.Args[1])
}
if required == "" {
fmt.Println("-required is required for all commands")
}
}
您可以通過創(chuàng)建標(biāo)志集映射來改進(jìn)上述解決方案,因此可以使用該映射注冊公共標(biāo)志,還可以執(zhí)行解析。
完整應(yīng)用:
var (
required string
fooCmd = flag.NewFlagSet("foo", flag.ExitOnError)
barCmd = flag.NewFlagSet("bar", flag.ExitOnError)
)
var subcommands = map[string]*flag.FlagSet{
fooCmd.Name(): fooCmd,
barCmd.Name(): barCmd,
}
func setupCommonFlags() {
for _, fs := range subcommands {
fs.StringVar(
&required,
"required",
"",
"required for all commands",
)
}
}
func main() {
setupCommonFlags()
cmd := subcommands[os.Args[1]]
if cmd == nil {
log.Fatalf("[ERROR] unknown subcommand '%s', see help for more details.", os.Args[1])
}
cmd.Parse(os.Args[2:])
fmt.Println(cmd.Name())
if required == "" {
fmt.Println("-required is required for all commands")
}
}

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
將全局標(biāo)志放在子命令之前:
go run . -required=x foo.
使用代替 :flag.Args()os.Args
package main
import (
"flag"
"fmt"
"log"
"os"
)
var (
required = flag.String(
"required",
"",
"required for all commands",
)
fooCmd = flag.NewFlagSet("foo", flag.ExitOnError)
barCmd = flag.NewFlagSet("bar", flag.ExitOnError)
)
func main() {
flag.Parse()
if *required == "" {
fmt.Println("-required is required for all commands")
}
args := flag.Args() // everything after the -required flag, e.g. [foo, -foo-flag-1, -foo-flag-2, ...]
switch args[0] {
case "foo":
fooCmd.Parse(args[1:])
fmt.Println("foo")
case "bar":
barCmd.Parse(args[1:])
fmt.Println("bar")
default:
log.Fatalf("[ERROR] unknown subcommand '%s', see help for more details.", args[0])
}
}
如果要將所有標(biāo)志放在一起,請?jiān)谧用钪缶帉懸粋€(gè)幫助程序函數(shù),該函數(shù)將公共標(biāo)志添加到每個(gè)標(biāo)志集:
var (
fooCmd = flag.NewFlagSet("foo", flag.ExitOnError)
barCmd = flag.NewFlagSet("bar", flag.ExitOnError)
)
type globalOpts struct {
required string
}
func main() {
var opts globalOpts
addGlobalFlags(fooCmd, &opts)
addGlobalFlags(barCmd, &opts)
if opts.required == "" {
fmt.Println("-required is required for all commands")
}
// ...
}
func addGlobalFlags(fs *flag.FlagSet, opts *globalOpts) {
fs.StringVar(
&opts.required,
"required",
"",
"required for all commands",
)
}
也許您還可以將這兩種方法結(jié)合起來,使全局標(biāo)志在任何位置工作。
- 2 回答
- 0 關(guān)注
- 144 瀏覽
添加回答
舉報(bào)