利用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的文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值