Fix global flags in Subcommands

closes #69
main
Philippe Lafoucrière 10 years ago
parent bb9189510a
commit df5fb46048

@ -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)

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

Loading…
Cancel
Save