• Golang中基础的命令行模块urfave/cli


    前言
    相信只要部署过线上服务,都知道启动参数一定是必不可少的,当你在不同的网络、硬件、软件环境下去启动一个服务的时候,总会有一些启动参数是不确定的,这时候就需要通过命令行模块去解析这些参数,urfave/cli是Golang中一个简单实用的命令行工具。

    安装
    通过 go get github.com/urfave/cli 命令即可完成安装。

    正文
    使用了urfave/cli之后,你的程序就会变成一个命令行程序,以下就是通过urfave/cli创建的一个最简单的命令行程序,它设定了一些基础的信息,这个程序的最终只是简单的打印了Test信息。

    package main

    import (
    "github.com/urfave/cli"
    "os"
    "log"
    "fmt"
    )

    func main() {
    //实例化一个命令行程序
    oApp := cli.NewApp()
    //程序名称
    oApp.Name = "GoTool"
    //程序的用途描述
    oApp.Usage = "To save the world"
    //程序的版本号
    oApp.Version = "1.0.0"
    //该程序执行的代码
    oApp.Action = func(c *cli.Context) error {
    fmt.Println("Test")
    return nil
    }
    //启动
    if err := oApp.Run(os.Args); err != nil {
    log.Fatal(err)
    }
    /*
    result:
    [root@localhost cli]# go run main.go help

    NAME:
    GoTool - To save the world

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

    VERSION:
    1.0.0

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

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

    [root@localhost cli]# go run main.go
    Test
    */

    }
    我们看到运行 go run main.go help 之后会输出一些帮助信息,说明你的程序已经成功成为一个命令行程序,接着使用命令 go run main.go 运行这个程序,结果是打印了Test信息,所以这个程序实际运行的函数由oApp.Action来控制,你后面的代码应该都在这个函数的内部去实现。

    接下来我们设定一些常见的启动参数,非常的简单,代码如下

    package main

    import (
    "github.com/urfave/cli"
    "os"
    "log"
    "fmt"
    )

    func main() {
    //实例化一个命令行程序
    oApp := cli.NewApp()
    //程序名称
    oApp.Name = "GoTool"
    //程序的用途描述
    oApp.Usage = "To save the world"
    //程序的版本号
    oApp.Version = "1.0.0"

    //预置变量
    var host string
    var debug bool

    //设置启动参数
    oApp.Flags = []cli.Flag{
    //参数类型string,int,bool
    cli.StringFlag{
    Name: "host", //参数名字
    Value: "127.0.0.1", //参数默认值
    Usage: "Server Address", //参数功能描述
    Destination: &host, //接收值的变量
    },
    cli.IntFlag{
    Name: "port,p",
    Value: 8888,
    Usage: "Server port",
    },
    cli.BoolFlag{
    Name: "debug",
    Usage: "debug mode",
    Destination: &debug,
    },
    }

    //该程序执行的代码
    oApp.Action = func(c *cli.Context) error {
    fmt.Printf("host=%v ",host)
    fmt.Printf("host=%v ",c.Int("port")) //不使用变量接收,直接解析
    fmt.Printf("host=%v ",debug)
    /*
    result:
    [root@localhost cli]# go run main.go --port 7777
    host=127.0.0.1
    host=7777
    host=false

    [root@localhost cli]# go run main.go help
    NAME:
    GoTool - To save the world

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

    VERSION:
    1.0.0

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

    GLOBAL OPTIONS:
    --host value Server Address (default: "127.0.0.1")
    --port value, -p value Server port (default: 8888)
    --debug debug mode
    --help, -h show help
    --version, -v print the version
    */
    return nil
    }
    //启动
    if err := oApp.Run(os.Args); err != nil {
    log.Fatal(err)
    }

    }
    执行 go run main.go --port 7777 之后,可以看到输出了设定的7777端口而非默认的8888端口,而服务器地址(host)和调试模式(debug)都输出了默认的数值。

    如果第三方人员第一次使用你的程序也可以通过help命令看到可以设定的参数都有哪些,非常的人性化。

    当然,urfave/cli还允许我们设置多个命令,不同的命令执行不同的操作,具体如下

    package main

    import (
    "github.com/urfave/cli"
    "os"
    "log"
    "fmt"
    )

    func main() {
    //实例化一个命令行程序
    oApp := cli.NewApp()
    //程序名称
    oApp.Name = "GoTool"
    //程序的用途描述
    oApp.Usage = "To save the world"
    //程序的版本号
    oApp.Version = "1.0.0"

    //设置多个命令处理函数
    oApp.Commands = []cli.Command{
    {
    //命令全称
    Name:"lang",
    //命令简写
    Aliases:[]string{"l"},
    //命令详细描述
    Usage:"Setting language",
    //命令处理函数
    Action: func(c *cli.Context) {
    // 通过c.Args().First()获取命令行参数
    fmt.Printf("language=%v ",c.Args().First())
    },
    },
    {
    Name:"encode",
    Aliases:[]string{"e"},
    Usage:"Setting encoding",
    Action: func(c *cli.Context) {
    fmt.Printf("encoding=%v ",c.Args().First())
    },
    },
    }

    //启动
    if err := oApp.Run(os.Args); err != nil {
    log.Fatal(err)
    }

    /*
    [root@localhost cli]# go run main.go l english
    language=english

    [root@localhost cli]# go run main.go e utf8
    encoding=utf8

    [root@localhost cli]# go run main.go help
    NAME:
    GoTool - To save the world

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

    VERSION:
    1.0.0

    COMMANDS:
    lang, l Setting language
    encode, e Setting encoding
    help, h Shows a list of commands or help for one command

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

    }
    上面代码只实现了两个简单命令,两个命令最后的处理函数不同,自然使用不同命令,最后的输出也不一样。

  • 相关阅读:
    MATLAB 高斯金字塔
    MATLAB 灰度图直方图均衡化
    MATLAB 生成高斯图像
    MATLAB 灰度、二值图像腐蚀膨胀
    MATLAB 中值滤波
    MATLAB 最大中值滤波
    MATLAB 最大均值滤波
    MATLAB 图像加噪,各种滤波
    MATLAB 图像傅里叶变换,幅度谱,相位谱
    十款最佳人工智能软件
  • 原文地址:https://www.cnblogs.com/ExMan/p/11535445.html
Copyright © 2020-2023  润新知