-4
我还没有定义标志-h也不是--help。 但是用以下代码,使用标志-h 不输出-h未定义的事实 ...为什么?Golang标志包有默认标志吗?
的代码:
package main
import (
"flag"
"fmt"
)
func showHelp() {
fmt.Println(`
Usage: CLI Template [OPTIONS]
Options:
-h, --help Print the help log.
`)
}
// If user makes wrong usage of flags, trigger ShowHelp()
func validateFlag(flag *flag.FlagSet) {
flag.Usage = func() {
showHelp()
}
}
func main() {
// Check if the current flag is valid
validateFlag(flag.CommandLine)
// Parse the command-line flags
flag.Parse()
}
的命令:
# I'm using makefile so
make all
bin/main -h
其结果是:
Usage: CLI Template [OPTIONS]
Options:
-h, --help Print the help log.
预期结果:与标志设置但不限定:-H
flag provided but not defined: -h
Usage: CLI Template [OPTIONS]
Options:
-h, --help Print the help log.
而不是定义一个新的函数分配给'flag.Usage'只需像这样直接指定'showHelp':'flag.Usage = showHelp' –