danielchg / gocli

Cobra tutorial to create a CLI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cobra tutorial (Golang CLI)

Getting started

First of all install cobra framework

go get github.com/spf13/cobra/cobra

Initialize the boilerplate

cobra init --pkg-name=github.com/danielchg/gocli

Add new option say to our CLI

cobra add say

Add subcommand say hello

cobra add hello

and edit the content of the file cmd/hello.go with the following in the function init

func init() {
    sayCmd.AddCommand(helloCmd)
}

and also teh conten of the Run

Run: func(cmd *cobra.Command, args []string) {
    fmt.Println("Hello World!")
},

Build

go build .

Run

./gocli say hello
Hello World!

Links

Tutorial

Cobra framework

About

Cobra tutorial to create a CLI

License:Apache License 2.0


Languages

Language:Go 100.0%