diff --git a/app.go b/app.go index 4efba5e..9ba043e 100644 --- a/app.go +++ b/app.go @@ -151,7 +151,7 @@ func (a *App) RunAsSubcommand(ctx *Context) error { set.SetOutput(ioutil.Discard) err := set.Parse(ctx.Args().Tail()) nerr := normalizeFlags(a.Flags, set) - context := NewContext(a, set, set) + context := NewContext(a, set, ctx.globalSet) if nerr != nil { fmt.Println(nerr) diff --git a/app_test.go b/app_test.go index 0b9e154..cf0e3d0 100644 --- a/app_test.go +++ b/app_test.go @@ -2,9 +2,10 @@ package cli_test import ( "fmt" - "github.com/codegangsta/cli" "os" "testing" + + "github.com/codegangsta/cli" ) func ExampleApp() { @@ -369,3 +370,32 @@ func TestAppCommandNotFound(t *testing.T) { expect(t, beforeRun, true) expect(t, subcommandRun, false) } + +func TestGlobalFlagsInSubcommands(t *testing.T) { + subcommandRun := false + app := cli.NewApp() + + app.Flags = []cli.Flag{ + cli.BoolFlag{Name: "debug, d", Usage: "Enable debugging"}, + } + + app.Commands = []cli.Command{ + cli.Command{ + Name: "foo", + Subcommands: []cli.Command{ + { + Name: "bar", + Action: func(c *cli.Context) { + if c.GlobalBool("debug") { + subcommandRun = true + } + }, + }, + }, + }, + } + + app.Run([]string{"command", "-d", "foo", "bar"}) + + expect(t, subcommandRun, true) +}