jasper的技术小窝

关注DevOps、运维监控、Python、Golang、开源、大数据、web开发、互联网

使用Cli构建Go的命令行应用

作者:jasper | 分类:Golang | 标签:   | 阅读 1886 次 | 发布:2015-07-26 5:51 p.m.

在Go里面应用中flag这一标准库,提供了很多我们在写命令行时需要的interface,然而如果你需要更强大更好的结构,可以试一下cli.go这个库。

利用cli.go来写命令行应用

定义命令和子命令

下面是一个例子:

package main

import (
    "fmt"
    "os"

    "github.com/codegangsta/cli"
)

func main() {
    app := cli.NewApp()
    app.Name = "jasperapp"
    app.Usage = "sample command-line app by jasper"
    app.Author = "Carter"
    app.Email = "jasper@xxx.com"
    app.Commands = []cli.Command{
        {
            Name:      "read",
            ShortName: "r",
            Usage:     "read something",
            Subcommands: []cli.Command{
                {
                    Name:   "articles",
                    Usage:  "read articles",
                    Action: readArticles,
                },
                {
                    Name:   "tweets",
                    Usage:  "read Tweets",
                    Action: readTweets,
                },
            },
        },
    }
    app.Run(os.Args)
}

func readArticles(ctx *cli.Context) {
    fmt.Println("Go to http://www.opscoder.info to read articles!")
}

func readTweets(ctx *cli.Context) {
    fmt.Println("Go to http://www.opscoder.info to read our tweets!")
}

如果你不带参数去运行,你将会得到下面的帮助信息:

NAME:
   jasperapp - sample command-line app by jasper

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

VERSION:
   0.0.0

AUTHOR:
  Carter - <jasper@xxx.com>

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

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

我们定义一个read子命令,它有articlestweet两个命令:

jasperapp read articles

输出为:

Go to http://www.opscoder.info to read articles!

或是运行:

jasperapp read tweets

输出为:

Go to http://www.opscoder.info to read our tweets!

使用context

每个动作都有一个context,在下面的例子中函数readArticles和readTweets接收一个参数,我们可以这样使用:

func readArticles(ctx *cli.Context) {
    fmt.Printf("The first argument was: %s", ctx.Args().First())
}

现在运行jasperapp read articles please将会输出:

The first argument was: please

使用flags

为了展示flags的用法,我们稍微修改一下tweets子命令:

{
        Name:   "tweets",
        Usage:  "read Tweets",
        Flags: []cli.Flag{
                cli.StringFlag{
                        Name: "account",
                        Value: "TheJasper",
                        Usage: "name of Twitter account",
                },
        },
        Action: readTwitter,
},

其中readTweets函数这么写:

func readTwitter(ctx *cli.Context) {
        fmt.Printf("Go to http://www.opscoder.info/%s to read tweets!", ctx.String("account"))
}

现在让我们编译结果并查看下tweets子命令的帮助信息:

jasperapp read tweets --help

输出如下:

NAME:
   tweets - read Tweets

USAGE:
   command tweets [command options] [arguments...]

OPTIONS:
   --account 'TheJasper' name of Twitter account

现在让我们试试:

$ jasperapp read tweets
Go to  http://www.opscoder.info/TheJasper to read tweets!

$ jasperapp read tweets --account codegangsta
Go to  http://www.opscoder.info/codegangsta to read tweets!

总结

Cli.go还有很多自定义的东西,具体可以参考Godoc的文档


转载请注明出处:http://www.opscoder.info/cli.html

其他分类: