gpt4 book ai didi

go - 从 Codegansta CLI 获取标志值

转载 作者:IT王子 更新时间:2023-10-29 01:59:14 26 4
gpt4 key购买 nike

我正在用 Go 编写一个命令行应用程序,并想指定一个 redis 端点作为标志。我添加了以下内容:

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

然而,在我的命令中,flag 总是空白的:

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)
},
}

调用方式:

./mantle-monitor --redis 127.0.0.1 listen

我做错了什么?

最佳答案

app.Flags 中定义的标志可通过 Context.Global* 方法访问。

你想要

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)
},
}

关于go - 从 Codegansta CLI 获取标志值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com