当前位置 博文首页 > Golang中基础的命令行模块urfave/cli的用法说明

    Golang中基础的命令行模块urfave/cli的用法说明

    作者:龙舞飞扬v 时间:2021-02-09 06:25

    前言

    相信只要部署过线上服务,都知道启动参数一定是必不可少的,当你在不同的网络、硬件、软件环境下去启动一个服务的时候,总会有一些启动参数是不确定的,这时候就需要通过命令行模块去解析这些参数,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 \n",host)
     fmt.Printf("host=%v \n",c.Int("port")) //不使用变量接收,直接解析
     fmt.Printf("host=%v \n",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 \n",c.Args().First())
     },
     },
     {
     Name:"encode",
     Aliases:[]string{"e"},
     Usage:"Setting encoding",
     Action: func(c *cli.Context) {
     fmt.Printf("encoding=%v \n",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
     */ 
    }

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

    补充:Go语言命令行库-urfave/cli(gopkg.in/urfave/cli.v2)

    Go语言命令行库-urfave/cli

    官网:https://github.com/urfave/cli

    很多用Go写的命令行程序都用了urfave/cli这个库。urfave/cli是一个命令行的框架。

    用C写过命令行程序的人应该都不陌生,我们需要根据argc/argv一个个地解析命令行参数,调用不同的函数,最后还要写一个usage()函数用于打印帮助信息。urfave/cli把这个过程做了一下封装,抽象出flag/command/subcommand这些模块,用户只需要提供一些模块的配置,参数的解析和关联在库内部完成,帮助信息也可以自动生成。

    总体来说,urfave/cli这个库还是很好用的,完成了很多routine的工作,程序员只需要专注于具体业务逻辑的实现。

    怎么使用urfave/cli

    go如何编写命令行(cli)程序

    首先下载类库包

    go get github.com/urfave/cli

    main.go

    package main
    import (
     "os"
     "github.com/urfave/cli/v2"
     "fmt"
    )
    func main() {
     app := &cli.App{
     Name: "greet",
     Usage: "say a greeting",
     Action: func(c *cli.Context) error {
     fmt.Println("Greetings")
     return nil
     },
     }
     // 接受os.Args启动程序
     app.Run(os.Args)
    }
    

    Flags 用于设置参数。

    Action 对应的函数就是你具体对各个参数具体的处理逻辑。

    “gopkg.in/urfave/cli.v2” 和 “github.com/urfave/cli”

    官网:https://github.com/urfave/cli

    gopkg:一种方便的go pakcage管理方式

    根据官网 readme描述,现在2个版本,主版本使用的是 v2 分支。

    导入包为: “github.com/urfave/cli/v2”

    有些 go 的代码库地址是gopkg.in开头的,比如gopkg.in/urfave/cli.v2。

    v2 表明版本号为 v2,而代码则为 github 上面相应的 v2 branch。

    这个也是 Go 的包管理解决方案之一,就是 gopkg.in 做了一个转发过程,实际上是使用了 github 里面的相应的 tag 的代码

    子命令 Subcommands

    如下 demo所示,我们再Action:同层添加 我们定义指针 &cli.Command 变量即可。

    demo:

    var daemonStopCmd = &cli.Command{
     Name: "stop",
     Usage: "Stop a running lotus daemon",
     Flags: []cli.Flag{},
     Action: func(cctx *cli.Context) error {
     panic("wombat attack")
     },
    }
    func main() {
     app := &cli.App{
     Name: "greet",
     Usage: "say a greeting",
     Action: func(c *cli.Context) error {
     fmt.Println("Greetings")
     return nil
     },
     Subcommands: []*cli.Command{
     daemonStopCmd,
     },
     }
     // 接受os.Args启动程序
     app.Run(os.Args)
    }
    

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持站长博客。如有错误或未考虑完全的地方,望不吝赐教。

    js