Golang cli 命令行框架筆記

簡(jiǎn)介

? cli 一個(gè)簡(jiǎn)單、快速的命令行程序開發(fā)框架。

安裝

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

簡(jiǎn)單使用示例

Code:

package main

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

func main()  {
    app := cli.NewApp()
    app.Name   = "greet"                       // 應(yīng)用名稱
    app.Usage  = "say a greeting"              // 應(yīng)用功能說明
    
    app.Action = func(c *cli.Context) error {  // 應(yīng)用執(zhí)行函數(shù)
        println("Greetings")
        return nil
    }
    _ = app.Run(os.Args) // 運(yùn)行應(yīng)用
}

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"                       // 應(yīng)用名稱
    app.Usage   = "say hello"                   // 應(yīng)用功能說明
    app.Author  = "arrows"                      // 應(yīng)用作者
    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",             // 默認(rèn)值
            Destination: &name,                 // 賦值
        },
    }
    app.Action  = func(c *cli.Context) error {  // 應(yīng)用執(zhí)行函數(shù)
        // fmt.Printf("hello, %s \n", c.String("name"))
        fmt.Printf("hello, %s \n", name)
        return nil
    }

    _ = app.Run(os.Args) // 運(yùn)行應(yīng)用
}

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"                       // 應(yīng)用名稱
    app.Usage   = "say hello"                   // 應(yīng)用功能說明
    app.Author  = "arrows"                      // 應(yīng)用作者
    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",             // 默認(rèn)值
                   Destination: &name,                 // 賦值
               },
               cli.IntFlag{
                   Name       : "level, l",             // 命令名稱
                   Usage      : "input your level",     // 命令說明
                   Value      : 0,                      // 默認(rèn)值
                   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) // 運(yùn)行應(yīng)用
}

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"                       // 應(yīng)用名稱
    app.Usage   = "say hello"                   // 應(yīng)用功能說明
    app.Author  = "arrows"                      // 應(yīng)用作者
    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) // 運(yùn)行應(yīng)用
}

Run:

go run main.go mysql insert

Output:

insert subcommand
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容