Merge pull request #253 from databus23/fix-isssue-252
Fix global flags processing on top level
This commit is contained in:
commit
b6d983e02c
17
app_test.go
17
app_test.go
@ -627,6 +627,23 @@ func TestAppCommandNotFound(t *testing.T) {
|
|||||||
expect(t, subcommandRun, false)
|
expect(t, subcommandRun, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGlobalFlag(t *testing.T) {
|
||||||
|
var globalFlag string
|
||||||
|
var globalFlagSet bool
|
||||||
|
app := cli.NewApp()
|
||||||
|
app.Flags = []cli.Flag{
|
||||||
|
cli.StringFlag{Name: "global, g", Usage: "global"},
|
||||||
|
}
|
||||||
|
app.Action = func(c *cli.Context) {
|
||||||
|
globalFlag = c.GlobalString("global")
|
||||||
|
globalFlagSet = c.GlobalIsSet("global")
|
||||||
|
}
|
||||||
|
app.Run([]string{"command", "-g", "foo"})
|
||||||
|
expect(t, globalFlag, "foo")
|
||||||
|
expect(t, globalFlagSet, true)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestGlobalFlagsInSubcommands(t *testing.T) {
|
func TestGlobalFlagsInSubcommands(t *testing.T) {
|
||||||
subcommandRun := false
|
subcommandRun := false
|
||||||
parentFlag := false
|
parentFlag := false
|
||||||
|
27
context.go
27
context.go
@ -73,7 +73,7 @@ func (c *Context) Generic(name string) interface{} {
|
|||||||
|
|
||||||
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
// Looks up the value of a global int flag, returns 0 if no int flag exists
|
||||||
func (c *Context) GlobalInt(name string) int {
|
func (c *Context) GlobalInt(name string) int {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupInt(name, fs)
|
return lookupInt(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
@ -81,7 +81,7 @@ func (c *Context) GlobalInt(name string) int {
|
|||||||
|
|
||||||
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
||||||
func (c *Context) GlobalDuration(name string) time.Duration {
|
func (c *Context) GlobalDuration(name string) time.Duration {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupDuration(name, fs)
|
return lookupDuration(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
@ -89,7 +89,7 @@ func (c *Context) GlobalDuration(name string) time.Duration {
|
|||||||
|
|
||||||
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
// Looks up the value of a global bool flag, returns false if no bool flag exists
|
||||||
func (c *Context) GlobalBool(name string) bool {
|
func (c *Context) GlobalBool(name string) bool {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupBool(name, fs)
|
return lookupBool(name, fs)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -97,7 +97,7 @@ func (c *Context) GlobalBool(name string) bool {
|
|||||||
|
|
||||||
// Looks up the value of a global string flag, returns "" if no string flag exists
|
// Looks up the value of a global string flag, returns "" if no string flag exists
|
||||||
func (c *Context) GlobalString(name string) string {
|
func (c *Context) GlobalString(name string) string {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupString(name, fs)
|
return lookupString(name, fs)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
@ -105,7 +105,7 @@ func (c *Context) GlobalString(name string) string {
|
|||||||
|
|
||||||
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
|
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
|
||||||
func (c *Context) GlobalStringSlice(name string) []string {
|
func (c *Context) GlobalStringSlice(name string) []string {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupStringSlice(name, fs)
|
return lookupStringSlice(name, fs)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -113,7 +113,7 @@ func (c *Context) GlobalStringSlice(name string) []string {
|
|||||||
|
|
||||||
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
|
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
|
||||||
func (c *Context) GlobalIntSlice(name string) []int {
|
func (c *Context) GlobalIntSlice(name string) []int {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupIntSlice(name, fs)
|
return lookupIntSlice(name, fs)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -121,7 +121,7 @@ func (c *Context) GlobalIntSlice(name string) []int {
|
|||||||
|
|
||||||
// Looks up the value of a global generic flag, returns nil if no generic flag exists
|
// Looks up the value of a global generic flag, returns nil if no generic flag exists
|
||||||
func (c *Context) GlobalGeneric(name string) interface{} {
|
func (c *Context) GlobalGeneric(name string) interface{} {
|
||||||
if fs := lookupParentFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
return lookupGeneric(name, fs)
|
return lookupGeneric(name, fs)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -147,7 +147,11 @@ func (c *Context) IsSet(name string) bool {
|
|||||||
func (c *Context) GlobalIsSet(name string) bool {
|
func (c *Context) GlobalIsSet(name string) bool {
|
||||||
if c.globalSetFlags == nil {
|
if c.globalSetFlags == nil {
|
||||||
c.globalSetFlags = make(map[string]bool)
|
c.globalSetFlags = make(map[string]bool)
|
||||||
for ctx := c.parentContext; ctx != nil && c.globalSetFlags[name] == false; ctx = ctx.parentContext {
|
ctx := c
|
||||||
|
if ctx.parentContext != nil {
|
||||||
|
ctx = ctx.parentContext
|
||||||
|
}
|
||||||
|
for ; ctx != nil && c.globalSetFlags[name] == false; ctx = ctx.parentContext {
|
||||||
ctx.flagSet.Visit(func(f *flag.Flag) {
|
ctx.flagSet.Visit(func(f *flag.Flag) {
|
||||||
c.globalSetFlags[f.Name] = true
|
c.globalSetFlags[f.Name] = true
|
||||||
})
|
})
|
||||||
@ -229,8 +233,11 @@ func (a Args) Swap(from, to int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookupParentFlagSet(name string, ctx *Context) *flag.FlagSet {
|
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
||||||
for ctx := ctx.parentContext; ctx != nil; ctx = ctx.parentContext {
|
if ctx.parentContext != nil {
|
||||||
|
ctx = ctx.parentContext
|
||||||
|
}
|
||||||
|
for ; ctx != nil; ctx = ctx.parentContext {
|
||||||
if f := ctx.flagSet.Lookup(name); f != nil {
|
if f := ctx.flagSet.Lookup(name); f != nil {
|
||||||
return ctx.flagSet
|
return ctx.flagSet
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user