Add a function to determine if a global flag is set.
This commit is contained in:
parent
f7ebb761e8
commit
b490b5e35d
24
context.go
24
context.go
@ -13,11 +13,12 @@ import (
|
|||||||
// can be used to retrieve context-specific Args and
|
// can be used to retrieve context-specific Args and
|
||||||
// parsed command-line options.
|
// parsed command-line options.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
App *App
|
App *App
|
||||||
Command Command
|
Command Command
|
||||||
flagSet *flag.FlagSet
|
flagSet *flag.FlagSet
|
||||||
globalSet *flag.FlagSet
|
globalSet *flag.FlagSet
|
||||||
setFlags map[string]bool
|
setFlags map[string]bool
|
||||||
|
globalSetFlags map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@ -105,7 +106,7 @@ func (c *Context) GlobalGeneric(name string) interface{} {
|
|||||||
return lookupGeneric(name, c.globalSet)
|
return lookupGeneric(name, c.globalSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determines if the flag was actually set exists
|
// Determines if the flag was actually set
|
||||||
func (c *Context) IsSet(name string) bool {
|
func (c *Context) IsSet(name string) bool {
|
||||||
if c.setFlags == nil {
|
if c.setFlags == nil {
|
||||||
c.setFlags = make(map[string]bool)
|
c.setFlags = make(map[string]bool)
|
||||||
@ -116,6 +117,17 @@ func (c *Context) IsSet(name string) bool {
|
|||||||
return c.setFlags[name] == true
|
return c.setFlags[name] == true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determines if the global flag was actually set
|
||||||
|
func (c *Context) GlobalIsSet(name string) bool {
|
||||||
|
if c.globalSetFlags == nil {
|
||||||
|
c.globalSetFlags = make(map[string]bool)
|
||||||
|
c.globalSet.Visit(func(f *flag.Flag) {
|
||||||
|
c.globalSetFlags[f.Name] = true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return c.globalSetFlags[name] == true
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a slice of flag names used in this context.
|
// Returns a slice of flag names used in this context.
|
||||||
func (c *Context) FlagNames() (names []string) {
|
func (c *Context) FlagNames() (names []string) {
|
||||||
for _, flag := range c.Command.Flags {
|
for _, flag := range c.Command.Flags {
|
||||||
|
@ -69,9 +69,30 @@ func TestContext_IsSet(t *testing.T) {
|
|||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
set.String("otherflag", "hello world", "doc")
|
set.String("otherflag", "hello world", "doc")
|
||||||
c := cli.NewContext(nil, set, set)
|
globalSet := flag.NewFlagSet("test", 0)
|
||||||
|
globalSet.Bool("myflagGlobal", true, "doc")
|
||||||
|
c := cli.NewContext(nil, set, globalSet)
|
||||||
set.Parse([]string{"--myflag", "bat", "baz"})
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
||||||
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
||||||
expect(t, c.IsSet("myflag"), true)
|
expect(t, c.IsSet("myflag"), true)
|
||||||
expect(t, c.IsSet("otherflag"), false)
|
expect(t, c.IsSet("otherflag"), false)
|
||||||
expect(t, c.IsSet("bogusflag"), false)
|
expect(t, c.IsSet("bogusflag"), false)
|
||||||
|
expect(t, c.IsSet("myflagGlobal"), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_GlobalIsSet(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Bool("myflag", false, "doc")
|
||||||
|
set.String("otherflag", "hello world", "doc")
|
||||||
|
globalSet := flag.NewFlagSet("test", 0)
|
||||||
|
globalSet.Bool("myflagGlobal", true, "doc")
|
||||||
|
globalSet.Bool("myflagGlobalUnset", true, "doc")
|
||||||
|
c := cli.NewContext(nil, set, globalSet)
|
||||||
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
||||||
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
||||||
|
expect(t, c.GlobalIsSet("myflag"), false)
|
||||||
|
expect(t, c.GlobalIsSet("otherflag"), false)
|
||||||
|
expect(t, c.GlobalIsSet("bogusflag"), false)
|
||||||
|
expect(t, c.GlobalIsSet("myflagGlobal"), true)
|
||||||
|
expect(t, c.GlobalIsSet("myflagGlobalUnset"), false)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user