TUN-3922: Repoint urfave/cli/v2 library at patched branch at github.com/ipostelnik/cli/v2@fixed which correctly handles reading flags declared at multiple levels of subcommands.

This commit is contained in:
Igor Postelnik
2021-02-12 15:11:41 -06:00
parent a8ae6de213
commit 1670ee87fb
37 changed files with 307 additions and 274 deletions

View File

@@ -18,6 +18,7 @@ type Context struct {
Command *Command
shellComplete bool
flagSet *flag.FlagSet
fromFlagSet map[string]bool
parentContext *Context
}
@@ -32,6 +33,14 @@ func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
}
}
// pre-compute flag seen on the command line at this context
c.fromFlagSet = make(map[string]bool)
if set != nil {
set.Visit(func(f *flag.Flag) {
c.fromFlagSet[f.Name] = true
})
}
c.Command = &Command{}
if c.Context == nil {
@@ -48,35 +57,49 @@ func (c *Context) NumFlags() int {
// Set sets a context flag to a value.
func (c *Context) Set(name, value string) error {
return c.flagSet.Set(name, value)
err := c.flagSet.Set(name, value)
if err == nil {
c.fromFlagSet[name] = true
}
return err
}
// IsSet determines if the flag was actually set
func (c *Context) IsSet(name string) bool {
if fs := lookupFlagSet(name, c); fs != nil {
if fs := lookupFlagSet(name, c); fs != nil {
isSet := false
fs.Visit(func(f *flag.Flag) {
if f.Name == name {
isSet = true
for ctx := c; ctx != nil; ctx = ctx.parentContext {
// try flags parsed from command line first
if ctx.flagSet.Lookup(name) == nil {
// flag not defined in this context
continue
}
if ctx.flagOnCommandLine(name) {
return true
}
// now see if value was set externally via environment
definedFlags := ctx.Command.Flags
if ctx.Command.Name == "" && ctx.App != nil {
definedFlags = ctx.App.Flags
}
for _, ff := range definedFlags {
for _, fn := range ff.Names() {
if fn == name {
if ff.IsSet() {
return true
}
}
})
if isSet {
return true
}
}
f := lookupFlag(name, c)
if f == nil {
return false
}
return f.IsSet()
}
return false
}
func (c *Context) flagOnCommandLine(name string) bool {
return c.fromFlagSet[name]
}
// LocalFlagNames returns a slice of flag names used in this context.
func (c *Context) LocalFlagNames() []string {
var names []string
@@ -108,7 +131,10 @@ func (c *Context) Lineage() []*Context {
// Value returns the value of the flag corresponding to `name`
func (c *Context) Value(name string) interface{} {
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
if fs := lookupFlagSet(name, c); fs != nil {
return fs.Lookup(name).Value.(flag.Getter).Get()
}
return nil
}
// Args returns the command line arguments associated with the context.
@@ -150,6 +176,25 @@ func lookupFlag(name string, ctx *Context) Flag {
return nil
}
func (c *Context) resolveFlagDeep(name string) *flag.Flag {
var src *flag.Flag
for cur := c; cur != nil; cur = cur.parentContext {
if f := cur.flagSet.Lookup(name); f != nil {
if cur.flagOnCommandLine(name) {
// we've found most specific instance on command line, use it
src = f
break
}
if src == nil {
// flag was defined, but value is not present among flags of the current context
// remember the most specific instance of the flag not from command line as fallback
src = f
}
}
}
return src
}
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
for _, c := range ctx.Lineage() {
if f := c.flagSet.Lookup(name); f != nil {