简体   繁体   English

从Codegansta CLI获取标志值

[英]Get flag value from Codegansta CLI

I'm writing a command line app in Go and want to specify a redis endpoint as a flag. 我正在Go中编写命令行应用程序,并希望将redis端点指定为标志。 I've added the following: 我添加了以下内容:

app.Flags = []cli.Flag{
    cli.StringFlag{
        Name:   "redis, r",
        Value:  "127.0.0.1",
        Usage:  "redis host to listen to",
        EnvVar: "REDIS_URL",
    },
}

However, in my command, the flag is always blank: 但是,在我的命令中,标志始终为空:

return cli.Command{
    Name:  "listen",
    Usage: "Listen to a stream",
    Action: func(c *cli.Context) {
        redisUrl := c.String("redis")
        log.Printf("Connecting to redis: %s\n", redisUrl)
    },
}

Invoked with: 调用:

./mantle-monitor --redis 127.0.0.1 listen

What am I doing wrong? 我究竟做错了什么?

Flags defined in app.Flags are accessed with the Context.Global* methods. 使用Context.Global*方法访问app.Flags中定义的标志。

You want 你要

return cli.Command{
    Name:  "listen",
    Usage: "Listen to a stream",
    Action: func(c *cli.Context) {
        redisUrl := c.GlobalString("redis")
        log.Printf("Connecting to redis: %s\n", redisUrl)
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM