Make Command accessible from Context
This commit is contained in:
parent
3fa24ca4f3
commit
c69522c960
@ -70,6 +70,7 @@ func (c Command) Run(ctx *Context) error {
|
|||||||
if checkCommandHelp(context, c.Name) {
|
if checkCommandHelp(context, c.Name) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
context.Command = c
|
||||||
c.Action(context)
|
c.Action(context)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
// parsed command-line options.
|
// parsed command-line options.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
App *App
|
App *App
|
||||||
|
Command Command
|
||||||
flagSet *flag.FlagSet
|
flagSet *flag.FlagSet
|
||||||
globalSet *flag.FlagSet
|
globalSet *flag.FlagSet
|
||||||
setFlags map[string]bool
|
setFlags map[string]bool
|
||||||
@ -20,7 +21,7 @@ type Context struct {
|
|||||||
|
|
||||||
// Creates a new context. For use in when invoking an App or Command action.
|
// Creates a new context. For use in when invoking an App or Command action.
|
||||||
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
||||||
return &Context{app, set, globalSet, nil}
|
return &Context{App: app, flagSet: set, globalSet: globalSet}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Looks up the value of a local int flag, returns 0 if no int flag exists
|
// Looks up the value of a local int flag, returns 0 if no int flag exists
|
||||||
|
@ -11,9 +11,12 @@ func TestNewContext(t *testing.T) {
|
|||||||
set.Int("myflag", 12, "doc")
|
set.Int("myflag", 12, "doc")
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
globalSet := flag.NewFlagSet("test", 0)
|
||||||
globalSet.Int("myflag", 42, "doc")
|
globalSet.Int("myflag", 42, "doc")
|
||||||
|
command := cli.Command{Name: "mycommand"}
|
||||||
c := cli.NewContext(nil, set, globalSet)
|
c := cli.NewContext(nil, set, globalSet)
|
||||||
|
c.Command = command
|
||||||
expect(t, c.Int("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
expect(t, c.GlobalInt("myflag"), 42)
|
expect(t, c.GlobalInt("myflag"), 42)
|
||||||
|
expect(t, c.Command.Name, "mycommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Int(t *testing.T) {
|
func TestContext_Int(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user