Alt text

install

cd to ../golang.org/x

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

download github.com/spf13/cobra/cobra
cd to its dir
then open cmd & type

1
go install github.com/spf13/cobra/cobra

then use it:

1
cobra init mycalc

Cobra-based application structure:

1
2
3
4
▾ appName/
▾ cmd/
root.go
main.go

cobra main.go structure:

1
2
3
4
5
6
7
8
9
package main

import (
"mycalc/cmd"
)

func main() {
cmd.Execute()
}

when you run it, it will reports error:

1
2
3
4
G:\go-work\bin\mycalc>go run main.go
main.go:18:8: cannot find package "go-work/bin/my-calc/cmd" in any of:
c:\go\src\go-work\bin\my-calc\cmd (from $GOROOT)
G:\go-work\src\go-work\bin\my-calc\cmd (from $GOPATH)

⚠️so you copy the whole package to $GOPATH, then

1
2
3
4
5
6
G:\go-work\bin\mycalc>go install mycalc
G:\go-work\bin\mycalc>go run main.go
Hello CLI

G:\go-work\bin>mycalc
Hello CLI

the command in root.go is called rootCmd.

the cli flow

  1. edit root.go
    1
    2
    3
    4
    func init() {
    fmt.Println("inside init")
    cobra.OnInitialize(initConfig)
    ...
  2. edit main.go
    1
    2
    3
    4
    func main() {
    fmt.Println("inside main")
    cmd.Execute()
    }
    and it will shows this:
    1
    2
    3
    4
    5
    6
    G:\go-work\bin>mycalc
    inside init
    inside main
    inside initConfig
    inside initConfig
    Hello CLI
    Should notice two types of flags: Persistent flags & Local flags

add cli:

  1. add num
    create a command named add, navigate to the project
    1
    2
    G:\go-work\src\mycalc>cobra add add
    add created at G:\go-work\src\mycalc
    rebuild the binary using go install mycalc and run mycalc add.
    1
    G:\go-work\src\mycalc>go install mycalc
1
2
G:\go-work\bin>mycalc add
add called
  1. modify it to add a series of numbers
    to add numbers, fisrt have to convert the string into int then returan the result.
    1
    2
    3
    4
    import (
    ...
    "strconv"
    )
  2. inside the add.go, create a new addInt function.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    func addInt(args []string) {
    var sum int
    // the first return value is index of args, we can omit it using _
    for _, ival := range args {
    // strconv is the library used for type conversion, for string
    // to int conversion Atio method is udes.
    itemp, err := strconv.Atoi(ival)
    if err != nil {
    fmt.Println(err)
    }
    sum = sum + itemp
    }
    fmt.Printf("Addition of numbers %s is %d, args, sum)
    }
  3. update RUN func
    1
    2
    3
    ...
    addInt(args)
    ...
  4. rebuild
    1
    2
    3
    4
    G:\go-work\bin>mycalc add a bc 8
    strconv.Atoi: parsing "a": invalid syntax
    strconv.Atoi: parsing "bc": invalid syntax
    Addition of numbers [a bc 8] is 8

achieve float calc by using flag

  1. create a local flag in init
1
2
3
4
5
//add.go
func init() {
rootCmd.AddCommand(addCmd)
addCmd.Flags().BoolP("float", "f",false,"Add Floating Numbers")
}
  1. create a addFloat func in add.go
    //add.go
    func addFloat(args []string) {
    var sum float64
    for _, fval := range args {
    ftemp, err := strconv.ParseFloat(fval,64)
    if err != nil {
    fmt.Println(err)
    }
    sum = sum + ftemp
    }
    fmt.Printf(“sum of floating nums %s is %f”, args,sum)
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    3. create a addFloat func in addCmd RUN func
    ``` bash
    // add.go
    // addCmd
    Run: func(cmd *cobra.Command, args []string) {
    // get the flag value, its default value is false
    fstatus, _ := cmd.Flags().GetBool("float")
    if fstatus {
    addFloat(args)
    } else {
    addInt(args)
    }
    },

select even numbers & add them

1
cobra add even
  1. init in even.go
    1
    2
    3
    4
    5
    //even.go
    func init() {
    addCmd.AddCommand(evenCmd)
    ...
    }
  2. update evenCmd struct variable’s RUN method
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //even.go
    Run: func(cmd *bobra.Command, args []string) {

    var evenSum int
    for _, ival := range args {
    itemp, _ := strconv.Atoi(ival)
    if itemp%2 == 0 {
    evenSum = evenSum + itemp
    }
    }
    fmt.Printf("the even addition of %s is %d", args, evenSum)
    },

select old numbers & add them

1
2
3
4
//odd.go
...
if itemp%2 != 0
...