gpt4 book ai didi

go - 如何在 cobra 命令中调用默认的 helpCommand?

转载 作者:IT王子 更新时间:2023-10-29 01:44:35 24 4
gpt4 key购买 nike

在下面的代码中,我定义了一个命令,其中有两个选项是可能的: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 infomyapp info --helpmyapp 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com