cobra #
A Commander for modern Go CLI interactions
quick start #
typically a Cobra-based application will follow the following organizational structure:
▾ appName/
▾ cmd/
add.go
your.go
commands.go
here.go
main.go
In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.
package main
import (
"{pathToYourApp}/cmd"
)
func main() {
cmd.Execute()
}
Ideally you place this in app/cmd/root.go:
var rootCmd = &cobra.Command{
Use: "hugo",
Short: "Hugo is a very fast static site generator",
Long: `A Fast and Flexible Static Site Generator built with
love by spf13 and friends in Go.
Complete documentation is available at http://hugo.spf13.com`,
Run: func(cmd *cobra.Command, args []string) {
// 在这里写应用逻辑
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
叶王 © 2013-2024 版权所有。如果本文档对你有所帮助,可以请作者喝饮料。