作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在下面的代码中,我定义了一个命令,其中有两个选项是可能的:1. myapp 信息 --flag1 文本2.我的应用程序信息--flag2如果没有指定这两个选项,我想显示 helpCommand
var infoCmd = &cobra.Command{
Use: "info",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
var infoURL string
if flag1 != "" {
doSomething()
} else if flag2 { //this is a boolean flag
doSomethingElse()
} else {
// Show the default help here
}
},
}
在眼镜蛇中README helpCommand 的用法如myapp help info
或 myapp info --help
或 myapp info --nonexistentoption
但与如何真正主动调用该方法无关。有什么指点吗?
最佳答案
我认为它只是通过库查看(未测试):
var infoCmd = &cobra.Command{
Use: "info",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
var infoURL string
if flag1 != "" {
doSomething()
} else if flag2 { //this is a boolean flag
doSomethingElse()
} else {
// Show the default help here
cmd.Help()
}
},
}
看这里:
// Help puts out the help for the command.
// Used when a user calls help [command].
// Can be defined by user by overriding HelpFunc.
func (c *Command) Help() error {
c.HelpFunc()(c, []string{})
return nil
}
一般来说,自述文件只会提供一些入门和概述信息,您通常需要打开 godocs(内联文档)才能正确理解包:godoc cobra: help command
关于go - 如何在 cobra 命令中调用默认的 helpCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513718/
我是一名优秀的程序员,十分优秀!