2015-10-30 40 views
0

我正在Go編寫命令行應用程序,並且希望將redis端點指定爲標誌。我已經添加了以下情況:從Codegansta獲取標誌值CLI

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

然而,在我的命令,該標誌始終是空白:

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 

我在做什麼錯?

回答

3

使用Context.Global*方法訪問app.Flags中定義的標誌。

你想

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

就是這樣。謝謝! – brandonhilkert