Golang:Cobra的安装和简单使用总结

Cobra的安装

就本人安装Cobra,解决两个报错即可:
第一个:使用命令 go get -v github.com/spf13/cobra/cobra 下载过程中,会出提示如下错误:

Fetching https://golang.org/x/sys/unix?go-get=1
https fetch failed: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: i/o timeout

方法:到$GOPATH/src/golang.org/x目录下,用git clone下载sys和text项目。

git clone https://github.com/golang/sys
git clone https://github.com/golang/text

或者去找热心网友分享的golang.org包,直接放到$GOPATH/src下。
第二个:继续 go get -v github.com/spf13/cobra/cobra ,报错提示缺少yaml.v2包。

方法:在$GOPATH/src下创建gopkg.in目录,在gopkg.in目录下执行:

git clone https://github.com/go-yaml/yaml.git

将得到的yaml文件夹名字改成yaml.v2。

终端执行go install github.com/spf13/cobra/cobra,安装后在 $GOBIN 下出现了 cobra 可执行程序。然后配置一下\$PATH就可以开始您的表演了(\$HOME/go/bin)。

Cobra的简单使用

生成项目文件:

cobra init demo

会在$GOPATH/src下产生demo文件夹。执行(以下执行go run xxx均在$GOPATH/src/demo下):

 go run main.go

可以看到:

A longer description that spans multiple lines and likely contains examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

添加子命令:

cobra add test

会打印:

test created at $GOPATH/src/demo/cmd/test.go

此时在cmd目录下,已经生成了一个与我们命令同名的go文件:test.go,与该命令有关的操作也正是在此文件中实现(如果使用cobra init 了多个“demo”的话,到相应的demo中执行cobra add xxx即可在相应的demo中添加xxx子命令)。执行这个子命令:

go run main.go test

会打印:

test called

添加子命令下的子命令:
添加test命令下的subtest子命令:在cmd目录下创建subtest.go文件并在文件中写入如下内容:

package cmd

import (
    "fmt"
    "github.com/spf13/cobra"
)
var subtestCmd = &cobra.Command{
    Use:   "subtest",
    Short: "A brief description of your command",
    Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("subtest called")
    },
}

func init() {
    testCmd.AddCommand(subtestCmd)
}

要注意的地方:init函数里的testCmd是在test.go中定义的子命令。执行:

go run main.go test subtest

会打印:

subtest called

输出参数
修改$GOPATH/src/demo/cmd下的test.go文件为:

package cmd

import (
    "fmt"
    "github.com/spf13/cobra"
)

// testCmd represents the test command
var testCmd = &cobra.Command{
    Use:   "test",
    Short: "A brief description of your command",
    Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("test called")
        parameter, _ := cmd.Flags().GetString("user")
        fmt.Println("parameter:", parameter)
    },
}

func init() {
    rootCmd.AddCommand(testCmd)
    testCmd.Flags().StringP("user", "u", "", "test")
}

修改的地方有两处:
第一处:run的匿名函数中添加了:

parameter, _ := cmd.Flags().GetString("user")
fmt.Println("parameter:", parameter)

第二处:init函数中添加了:

   testCmd.Flags().StringP("user", "u", "", "test")

执行:

go run main.go test --user=wangwu
或者
go run main.go test --user wangwu
或者
go run main.go test -u wangwu

会打印:

test called
parameter: wangwu

你可能感兴趣的:(golang)