一. cobra是什么
cobra是一个命令行程序库,可以用来编写命令行程序。同时,它也提供了一个脚手架,用于生成基于 cobra 的应用程序框架。非常多知名的开源项目使用了 cobra 库构建命令行,如Kubernetes、Hugo、etcd等等等等。
二. 引入cobra库
go get -u github.com/spf13/cobra
三. go示例代码
package main
import (
"fmt"
"github.com/spf13/cobra"
"os"
"strings"
)
var (
Verbose bool
ConfigPath string
SomeString string
)
func main() {
Execute()
return
}
var rootCmd = &cobra.Command{
Use: "root",
Short: "the short description shown in the 'help' output.",
Long: `root description detail
Long is the long message shown in the 'help <this-command>' output.`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
fmt.Printf("root verbose: %v\n", Verbose)
fmt.Printf("root config: %s\n", ConfigPath)
fmt.Printf("root string: %s\n", SomeString)
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(e

最低0.47元/天 解锁文章
526

被折叠的 条评论
为什么被折叠?



