1. 程式人生 > >Golang:Cobra的安裝和簡單使用總結

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

會列印:

test called
parameter: wangwu