1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2025-07-14 02:39:11 +00:00

go: updating modules

It seems this may be the last update to urfave/cli for go1.17, as their
v2.25 uses generics of go1.18 and didn't partition it with build tags
😵😵😵

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2023-03-22 10:42:20 -04:00
parent c6a7295705
commit 45591ed121
Signed by: vbatts
GPG key ID: E30EFAA812C6E5ED
40 changed files with 2475 additions and 731 deletions

View file

@ -3,6 +3,7 @@ package cli
import (
"context"
"flag"
"fmt"
"strings"
)
@ -46,7 +47,11 @@ func (cCtx *Context) NumFlags() int {
// Set sets a context flag to a value.
func (cCtx *Context) Set(name, value string) error {
return cCtx.flagSet.Set(name, value)
if fs := cCtx.lookupFlagSet(name); fs != nil {
return fs.Set(name, value)
}
return fmt.Errorf("no such flag -%s", name)
}
// IsSet determines if the flag was actually set
@ -77,7 +82,27 @@ func (cCtx *Context) IsSet(name string) bool {
func (cCtx *Context) LocalFlagNames() []string {
var names []string
cCtx.flagSet.Visit(makeFlagNameVisitor(&names))
return names
// Check the flags which have been set via env or file
if cCtx.Command != nil && cCtx.Command.Flags != nil {
for _, f := range cCtx.Command.Flags {
if f.IsSet() {
names = append(names, f.Names()...)
}
}
}
// Sort out the duplicates since flag could be set via multiple
// paths
m := map[string]struct{}{}
var unames []string
for _, name := range names {
if _, ok := m[name]; !ok {
m[name] = struct{}{}
unames = append(unames, name)
}
}
return unames
}
// FlagNames returns a slice of flag names used by the this context and all of
@ -85,7 +110,7 @@ func (cCtx *Context) LocalFlagNames() []string {
func (cCtx *Context) FlagNames() []string {
var names []string
for _, pCtx := range cCtx.Lineage() {
pCtx.flagSet.Visit(makeFlagNameVisitor(&names))
names = append(names, pCtx.LocalFlagNames()...)
}
return names
}
@ -102,6 +127,16 @@ func (cCtx *Context) Lineage() []*Context {
return lineage
}
// Count returns the num of occurences of this flag
func (cCtx *Context) Count(name string) int {
if fs := cCtx.lookupFlagSet(name); fs != nil {
if cf, ok := fs.Lookup(name).Value.(Countable); ok {
return cf.Count()
}
}
return 0
}
// Value returns the value of the flag corresponding to `name`
func (cCtx *Context) Value(name string) interface{} {
if fs := cCtx.lookupFlagSet(name); fs != nil {
@ -158,7 +193,7 @@ func (cCtx *Context) lookupFlagSet(name string) *flag.FlagSet {
return c.flagSet
}
}
cCtx.onInvalidFlag(name)
return nil
}
@ -170,9 +205,7 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
var flagName string
for _, key := range f.Names() {
if len(key) > 1 {
flagName = key
}
flagName = key
if cCtx.IsSet(strings.TrimSpace(key)) {
flagPresent = true
@ -192,6 +225,16 @@ func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
return nil
}
func (cCtx *Context) onInvalidFlag(name string) {
for cCtx != nil {
if cCtx.App != nil && cCtx.App.InvalidFlagAccessHandler != nil {
cCtx.App.InvalidFlagAccessHandler(cCtx, name)
break
}
cCtx = cCtx.parentContext
}
}
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
return func(f *flag.Flag) {
nameParts := strings.Split(f.Name, ",")