美文网首页
Golang cli 命令行框架笔记

Golang cli 命令行框架笔记

作者: Arrows | 来源:发表于2019-05-30 19:00 被阅读0次

简介

cli 一个简单、快速的命令行程序开发框架。

安装

go get -v github.com/urfave/cli
or
gopm get -v github.com/urfave/cli 

示例

快速开始

Code:

package main

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

func main()  {
    _ = cli.NewApp().Run(os.Args)
}

Run:

go run main.go

Output:

NAME:
   main - A new cli application

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

VERSION:
   0.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

简单使用示例

Code:

package main

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

func main()  {
    app := cli.NewApp()
    app.Name   = "greet"                       // 应用名称
    app.Usage  = "say a greeting"              // 应用功能说明
    
    app.Action = func(c *cli.Context) error {  // 应用执行函数
        println("Greetings")
        return nil
    }
    _ = app.Run(os.Args) // 运行应用
}

Run:

go run main.go

Output:

Greetings

Flag 使用

Code:

package main

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

func main()  {
    var name string
    app := cli.NewApp()
    app.Name    = "hello"                       // 应用名称
    app.Usage   = "say hello"                   // 应用功能说明
    app.Author  = "arrows"                      // 应用作者
    app.Email   = "shangmlee@foxmail.com"       // 邮箱
    app.Version = "0.0.1"                       // 版本
    app.Flags   = []cli.Flag{
        cli.StringFlag{
            Name       : "name, n",             // 命令名称
            Usage      : "input your name",     // 命令说明
            Value      : "Unknown",             // 默认值
            Destination: &name,                 // 赋值
        },
    }
    app.Action  = func(c *cli.Context) error {  // 应用执行函数
        // fmt.Printf("hello, %s \n", c.String("name"))
        fmt.Printf("hello, %s \n", name)
        return nil
    }

    _ = app.Run(os.Args) // 运行应用
}

Run:

go run main.go --name jack
#or
go run main.go --name=jack
#or
go run main.go -n jack

Output:

hello, jack 

Command 使用示例

Code:

package main

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

func main()  {
    //var name string
    app := cli.NewApp()
    app.Name    = "hello"                       // 应用名称
    app.Usage   = "say hello"                   // 应用功能说明
    app.Author  = "arrows"                      // 应用作者
    app.Email   = "shangmlee@foxmail.com"       // 邮箱
    app.Version = "0.0.1"                       // 版本

    var name string
    var level int
    app.Commands = []cli.Command{
        {
            Name   :  "say",
            Aliases: []string{"s"},
            Usage  : "say hello",
            Flags  : []cli.Flag{
               cli.StringFlag{
                   Name       : "name, n",             // 命令名称
                   Usage      : "input your name",     // 命令说明
                   Value      : "Unknown",             // 默认值
                   Destination: &name,                 // 赋值
               },
               cli.IntFlag{
                   Name       : "level, l",             // 命令名称
                   Usage      : "input your level",     // 命令说明
                   Value      : 0,                      // 默认值
                   Destination: &level,                 // 赋值
               },
            },
            Action : func(c *cli.Context) error {
                switch level {
                case 0:
                    fmt.Printf("hello, %s \n", name)
                case 1:
                    fmt.Printf("hello, %s ! You are super user \n", name)
                default:
                    fmt.Printf("hello, %s ! no record \n", name)
                }
                return nil
            },
        },
    }
    _ = app.Run(os.Args) // 运行应用
}

Run:

go run main.go say -n tom -l 1

Output:

hello, tom ! You are super user

SubCommand 使用

Code:

package main

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

func main()  {
    //var name string
    app := cli.NewApp()
    app.Name    = "hello"                       // 应用名称
    app.Usage   = "say hello"                   // 应用功能说明
    app.Author  = "arrows"                      // 应用作者
    app.Email   = "shangmlee@foxmail.com"       // 邮箱
    app.Version = "0.0.1"                       // 版本

    app.Commands = []cli.Command{
        {
            Name   :  "mysql",
            Aliases: []string{"d"},
            Usage  : "mysql operations ",
            Subcommands: []cli.Command{
                {
                    Name:  "insert",
                    Usage: "insert data",
                    Action: func(c *cli.Context) error {
                        fmt.Println("insert subcommand")
                        return nil
                    },
                },
                {
                    Name:  "delete",
                    Usage: "delete data",
                    Action: func(c *cli.Context) error {
                        fmt.Println("delete subcommand")
                        return nil
                    },
                },
            },
        },
    }
    _ = app.Run(os.Args) // 运行应用
}

Run:

go run main.go mysql insert

Output:

insert subcommand

相关文章

  • 基于Cobra的golang命令行工具开发

    基于golang语言开发命令行工具(command line interfaces,CLI)最常用的框架是Cobr...

  • Golang cli 命令行框架笔记

    简介 ​ cli 一个简单、快速的命令行程序开发框架。 安装 示例 快速开始 Code: Run: Outpu...

  • GOLang CLI

    GOLang CLI 刚接触GoLang,发现大家在写CLI的时候都会直接使用Go语言中自动的flag或者cobr...

  • Squoosh-cli的代码解析

    cli是libsquoosh的命令行工具,可以直接在命令行中直接运行。 Cli的核心流程分析 cli使用comma...

  • 第2章 走进shell

    2.1 走进命令行 CLI(command line interface) 文本命令行界面CLI 只能接受文本输入...

  • Vue.js项目的搭建

    使用Vue框架编写前端页面步骤 1.安装vue-cli脚手架工具 打开命令行,输入指令 npm install -...

  • Vue.js项目的搭建

    使用Vue框架编写前端页面步骤 1.安装vue-cli脚手架工具 打开命令行,输入指令 npm install -...

  • golang应用快速搭建框架jotnar

    golang应用快速搭建框架jotnar 博客/golang jotnar 是一个golang项目快速搭建的框架,...

  • 2019-11-20 文摘

    10.2 Installing the Spring Boot CLI Spring Boot CLI(命令行接口...

  • HiveQL快速使用

    hive命令包括Hive cli 和 hiveQL命令 Hive cli cli 就是命令行界面,可以通过cli创...

网友评论

      本文标题:Golang cli 命令行框架笔记

      本文链接:https://www.haomeiwen.com/subject/lmcbtctx.html