Cobra提供简单的接口来创建强大的现代化CLI接口,比如git与go工具。Cobra同时也是一个程序, 用于创建CLI程序。
▾ appName/
▾ cmd/
root.go
main.go
main.go
package main
import (
"fmt"
"os"
"{pathToYourApp}/cmd"
)
func main() {
cmd.Execute()
}
root.go
package update
import (
"github.com/spf13/cobra"
"fmt"
"github.com/dwdcth/cli_sample/rootcmd"
)
var (
a = ""
b = ""
)
// 注册命令
func init() {
rootCmd.PersistentFlags().StringVar(&a, "url", "a", "aaa")
rootCmd.PersistentFlags().StringVar(&b, "config", "b", "bbb")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var rootCmd = &cobra.Command{
Use: "app",
Short: "A brief description of your command",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
// your func
},
}