TUN-3964: Revert "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 reverts commit 1670ee87fb.
This commit is contained in:
cthuang
2021-02-23 12:23:57 +00:00
committed by Chung Ting Huang
parent e9c2afec56
commit 9d5bd256be
37 changed files with 274 additions and 307 deletions

View File

@@ -18,7 +18,6 @@ type Context struct {
Command *Command
shellComplete bool
flagSet *flag.FlagSet
fromFlagSet map[string]bool
parentContext *Context
}
@@ -33,14 +32,6 @@ 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 {
@@ -57,49 +48,35 @@ func (c *Context) NumFlags() int {
// Set sets a context flag to a value.
func (c *Context) Set(name, value string) error {
err := c.flagSet.Set(name, value)
if err == nil {
c.fromFlagSet[name] = true
}
return err
return c.flagSet.Set(name, value)
}
// IsSet determines if the flag was actually set
func (c *Context) IsSet(name string) bool {
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 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
}
})
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
@@ -131,10 +108,7 @@ func (c *Context) Lineage() []*Context {
// Value returns the value of the flag corresponding to `name`
func (c *Context) Value(name string) interface{} {
if fs := lookupFlagSet(name, c); fs != nil {
return fs.Lookup(name).Value.(flag.Getter).Get()
}
return nil
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
}
// Args returns the command line arguments associated with the context.
@@ -176,25 +150,6 @@ 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 {