我正在尝试了解如何使用cobra(和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字段表现不一致(据我所知)。我试图了解命令如何在命令行中实际运行,并以不同的方式编译文件来试验它。
例如,我使用cobra.go
编译了这个名为go build 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
(当然只有眼镜蛇可执行文件),它会打印出来:
zsh: no such file or directory: ./app
如果我app help print
它也不起作用(因为在PATH中找不到它):
zsh: command not found: app
所以我的理解是,除非我使用与root相同的名称构建它,否则cobra会表现得很有趣。
所以这意味着使用字段必须匹配帮助输出的文件名才有意义,并且我们能够正确运行其子命令,对吗?
另外,如果只允许一个包main,那么如何在同一个项目中拥有(编译)多个不同的“root”命令?如果在main下有多个“root”命令,它应该不起作用,对吧?我很快就会尝试其中的一些想法。
答案 0 :(得分:6)
您可以查看cobra的使用方式是其他项目(go-search)。
例如,bitballoon-cli
项目定义了多个命令,每个命令都在自己的文件中,每个命令都在主包中。像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年后,请参阅视频&#34; justforfunc #32: CLI tools with Cobra&#34;, Francesc Campoy 。