我正在尝试了解如何使用 cobra 为 go 创建服装命令(和 viper)库并能够使用它们。
具体来说,我试图理解并使他们提供的示例发挥作用。示例如下:
import(
"github.com/spf13/cobra"
"fmt"
"strings"
)
func main() {
var echoTimes int
var cmdPrint = &cobra.Command{
Use: "print [string to print]",
Short: "Print anything to the screen",
Long: `print is for printing anything back to the screen.
For many years people have printed back to the screen.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " "))
},
}
var cmdEcho = &cobra.Command{
Use: "echo [string to echo]",
Short: "Echo anything to the screen",
Long: `echo is for echoing anything back.
Echo works a lot like print, except it has a child command.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Print: " + strings.Join(args, " "))
},
}
var cmdTimes = &cobra.Command{
Use: "times [# times] [string to echo]",
Short: "Echo anything to the screen more times",
Long: `echo things multiple times back to the user by providing
a count and a string.`,
Run: func(cmd *cobra.Command, args []string) {
for i:=0; i < echoTimes; i++ {
fmt.Println("Echo: " + strings.Join(args, " "))
}
},
}
cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
var rootCmd = &cobra.Command{Use: "app"}
rootCmd.AddCommand(cmdPrint, cmdEcho)
cmdEcho.AddCommand(cmdTimes)
rootCmd.Execute()
}
但是,我已经为它制作了文件,在 package main
中,但我似乎无法让它正常工作。我明白 rootCmd 不应该是可执行的。但是,在我看来,Use 字段的行为不一致(据我所知)。我试图了解命令实际上是如何在命令行中运行的,并以不同的方式编译文件来对此进行试验。
例如,我使用 go build cobra.go
编译了这个名为 cobra.go
的文件,现在我有了一个可执行文件 cobra
。
如果我执行 ./name
它的行为方式与 ./name help
相同并打印:
Usage:
app [command]
Available Commands:
print [string to print] Print anything to the screen
echo [string to echo] Echo anything to the screen
help [command] Help about any command
Available Flags:
--help=false: help for app
Use "app help [command]" for more information about that command.
然而,描述错误地指出要使用它,您必须执行“app help [command]”。
如果我这样做 ./app help print
(当然只有 cobra 可执行文件)它会打印:
zsh: no such file or directory: ./app
如果我执行 app help print
它也不起作用(因为它在 PATH 中找不到):
zsh: command not found: app
所以我的理解是,除非我用与 root 相同的名称构建它,否则 cobra 会表现得很滑稽。
所以这意味着 use 字段必须匹配帮助输出的文件名才有意义,并且我们能够正确运行其子命令,对吧?
另外,如果只允许一个 package main,如何在同一个项目中拥有(编译)多个不同的“root”命令?如果一个人在 main 下有多个“root”命令,它应该不起作用,对吧?我很快就会尝试其中的一些想法。
最佳答案
您可以检查 cobra 在其他项目中的使用方式 ( go-search )。
例如,bitballoon-cli
project 定义了多个命令,每个命令都在自己的文件中,每个都在主包中。喜欢create.go
:
package main
import (
"github.com/BitBalloon/bitballoon-go"
"github.com/spf13/cobra"
"log"
)
var createCmd = &cobra.Command{
Use: "create",
Short: "create a new BitBalloon site",
Long: "creates a new BitBalloon site and returns an ID you can deploy to",
}
var siteName, customDomain, password, notificationEmail string
func init() {
createCmd.Run = create
createCmd.Flags().StringVarP(&siteName, "name", "n", "", "Name of the site (must be a valid subdomain: <name>.bitballoon.com)")
createCmd.Flags().StringVarP(&customDomain, "domain", "d", "", "Custom domain for the site (only works for premium sites)")
createCmd.Flags().StringVarP(&password, "password", "p", "", "Password for the site")
createCmd.Flags().StringVarP(¬ificationEmail, "email", "e", "", "Notification email for form submissions (only works for premium sites)")
}
func create(cmd *cobra.Command, args []string) {
// your function for this command
}
您可以检查这种定义命令并将其添加到您的项目的方式是否适用于您的情况。
4 年后,观看视频“justforfunc #32: CLI tools with Cobra”,作者: Francesc Campoy .
关于go - go(golang)的眼镜蛇指挥官如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791878/