Let commands access top-level flags?
I'm not sure if I'm doing something wrong or if cli doesn't support this. I have an app with lots of subcommands, and I'd like to have some global flags that apply for every command (e.g. specifying the location of a config file). (Right now the commands don't have any command-specific flags, but potentially I could want both global flags and command specific flags).
If I specify the flags in the top level app.Flags, I can't seem to extract their value from the context in the action -- I always get empty strings back. Instead, I have to repeat the flags in each command definition for it to work. Is this the intended behaviour (top-level flags don't do anything for commands)?
The global flags can be accessed like:
Action: func(c *cli.Context) {
println("User is....")
if(c.GlobalString("user") != "") {
println("User: ", c.GlobalString("user"))
}
},
Ah, my bad! I guess I was fixated on String() because of the example in the README. Thanks!
This should be in the README. I just spent WAY too long trying to figure out how to get this working. :(
Agreed. I will make sure it is added to the readme
Sent from my iPhone
On Apr 1, 2014, at 4:55 PM, Steve Rude [email protected] wrote:
This should be in the README. I just spent WAY too long trying to figure out how to get this working. :(
— Reply to this email directly or view it on GitHub.
Sorry for a possibly stupid question, but I am not able to get this working...
package main
import (
"github.com/codegangsta/cli"
"os"
"fmt"
"strconv"
"github.com/op/go-logging"
)
var LOGGER = logging.MustGetLogger("tlookup")
func main() {
app := cli.NewApp()
app.Name = "tlookup"
app.Version = Version
app.Usage = "kicking ass and taking names"
app.Author = "John Dyer"
app.Email = "[email protected]"
app.Compiled = CompileTime()
app.Commands = Commands
app.Flags = []cli.Flag {
cli.BoolFlag{"debug, d", "Run in debug mode"},
}
app.Action = func(c *cli.Context) {
foo := strconv.FormatBool(c.GlobalBool("debug"))
fmt.Println("====> ", foo) // Nothing ever happens here
}
app.Run(os.Args)
}
@johntdyer I tried running your code (minus the bits I don't have like the logger and some other things), and it seems to work fine, printing ====> false by default and ====> true when I pass -d or --debug. (It also works fine with Bool instead of GlobalBool, since you're using app.Action and a not a subcommand).
Odd, it works for me now too... Must have been a PEBCAK error :)
This is not true with subcommands apparently:
package main
import (
"fmt"
"os"
"strconv"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "tlookup"
app.Usage = "kicking ass and taking names"
app.Author = "John Dyer"
app.Email = "[email protected]"
app.Flags = []cli.Flag{
cli.BoolFlag{"debug, d", "Run in debug mode"},
}
app.Commands = []cli.Command{
{
Name: "first",
Subcommands: []cli.Command{
{
Name: "test",
Action: func(c *cli.Context) {
foo := strconv.FormatBool(c.GlobalBool("debug"))
fmt.Println("====> ", foo) // Nothing ever happens here
},
},
},
},
}
app.Run(os.Args)
}
Will fail to display "debug" correctly:
./testcli -d first test
====> false
This is indeed a bug. I have a fix, and will commit a PR today.