The golang cli command line framework uses

Keywords: Go MySQL github

brief introduction
Cli provides simple and fast development of command line functions.Setting parameters and configurations by command is a basic requirement in applications.Cli can help you quickly build command line functionality.

install
go get github.com/urfave/cli 
Simple example
package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    app := cli.NewApp()
    app.Name = "sysconfig"
    app.Usage = "Setting basic configuration"
    app.Version = "0.0.1"

    app.Action = func(c *cli.Context) error {
        fmt.Println("Prepare applying basic configuration")
        return nil
    }

    app.Run(os.Args)
}

Execute tests after compilation

./cli -h
NAME:
   sysconfig - Setting basic configuration

USAGE:
   cli [global options] command [command options] [arguments...]

VERSION:
   0.0.1

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version

You can see that the part of the Action is not executed

Command line without -h or -v

./cli
Prepare applying basic configuration

You can see that part of the Action is executed

Specifies that the parameters to be set are set using cli.flags

port with MySQL address or debug specified in the application can be used
cli --mysqlurl 192.168.111.11 --debugport 6666
perhaps
cli -url 192.168.111.11 -dp 6666

Example

package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    var mysqlUrl string
    var debugPort uint
    app := cli.NewApp()
    app.Name = "sysconfig"
    app.Usage = "Setting basic configuration"
    app.Version = "0.0.1"

    app.Flags = []cli.Flag{
        cli.StringFlag{
            Name:        "mysqlurl, url",
            Usage:       "specify mysql address",
            Value:       "182.122.11.11",
            Destination: &mysqlUrl,
        },
        cli.UintFlag{
            Name:        "debugport, dp",
            Usage:       "specify debug port",
            Value:       9666,
            Destination: &debugPort,
        },
    }

    app.Action = func(c *cli.Context) error {
        fmt.Println("Prepare applying basic configuration")

        fmt.Println("MySQL Url:", mysqlUrl)
        fmt.Println("Debug port:", debugPort)
        return nil
    }

    app.Run(os.Args)
}

Post Compile Execution

./cli --mysqlurl 192.168.128.122 --debugport 76554
Prepare applying basic configuration
MySQL Url: 192.168.128.122
Debug port: 76554

You can see that mysqlUrl and debugPort have been assigned parameters entered on the command line

Use command

For example, to set the debug level, querying the user information corresponding to the userid in mysql can be entered in the following format

./cli debug grade 2
./cli mysql query 12345

The sample code is as follows

package main

import (
    "fmt"
    "os"

    "github.com/urfave/cli"
)

func main() {
    app := cli.NewApp()
    app.Name = "sysconfig"
    app.Usage = "Setting basic configuration"
    app.Version = "0.0.1"

    app.Commands = []cli.Command{
        {
            Name:    "debug",
            Aliases: []string{"d"},
            Usage:   "debug settings",
            Subcommands: []cli.Command{
                {
                    Name:  "grade",
                    Usage: "set debug grade",
                    Action: func(c *cli.Context) error {
                        fmt.Println("Set debug grade to ", c.Args().First())
                        return nil
                    },
                },
            },
        },
        {
            Name:    "mysql",
            Aliases: []string{"q"},
            Usage:   "mysql operations",
            Subcommands: []cli.Command{
                {
                    Name:  "query",
                    Usage: "query userid",
                    Action: func(c *cli.Context) error {
                        fmt.Println("query userid=", c.Args().First())
                        return nil
                    },
                },
            },
        },
    }

    app.Run(os.Args)
}

Post Compile Execution

./cli d grade 2
Set debug grade to  2

./cli q query 3415
query userid= 3415

Posted by JNettles on Thu, 07 Nov 2019 06:19:48 -0800