Merge pull request #170 from codegangsta/parse-flags-regardless

Use parsed context when running command as subcommnd
This commit is contained in:
Jesse Szwedko 2015-01-27 21:16:17 -08:00
commit e1712f3817
2 changed files with 27 additions and 5 deletions

6
app.go
View File

@ -242,11 +242,7 @@ func (a *App) RunAsSubcommand(ctx *Context) error {
} }
// Run default Action // Run default Action
if len(a.Commands) > 0 { a.Action(context)
a.Action(context)
} else {
a.Action(ctx)
}
return nil return nil
} }

View File

@ -193,6 +193,32 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
expect(t, firstArg, "my-arg") expect(t, firstArg, "my-arg")
} }
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
var context *cli.Context
a := cli.NewApp()
a.Commands = []cli.Command{
{
Name: "foo",
Action: func(c *cli.Context) {
context = c
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
},
},
Before: func(_ *cli.Context) error { return nil },
},
}
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
expect(t, context.Args().Get(0), "abcd")
expect(t, context.String("lang"), "spanish")
}
func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) { func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
var parsedOption string var parsedOption string
var args []string var args []string