Merge pull request #1537 from dearchap/issue_1263
Fix:(issue_1263) FlagNames should return names set via env as well
This commit is contained in:
commit
d1ac284402
24
context.go
24
context.go
@ -82,7 +82,27 @@ func (cCtx *Context) IsSet(name string) bool {
|
|||||||
func (cCtx *Context) LocalFlagNames() []string {
|
func (cCtx *Context) LocalFlagNames() []string {
|
||||||
var names []string
|
var names []string
|
||||||
cCtx.flagSet.Visit(makeFlagNameVisitor(&names))
|
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
|
// FlagNames returns a slice of flag names used by the this context and all of
|
||||||
@ -90,7 +110,7 @@ func (cCtx *Context) LocalFlagNames() []string {
|
|||||||
func (cCtx *Context) FlagNames() []string {
|
func (cCtx *Context) FlagNames() []string {
|
||||||
var names []string
|
var names []string
|
||||||
for _, pCtx := range cCtx.Lineage() {
|
for _, pCtx := range cCtx.Lineage() {
|
||||||
pCtx.flagSet.Visit(makeFlagNameVisitor(&names))
|
names = append(names, pCtx.LocalFlagNames()...)
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
@ -239,6 +239,10 @@ func TestFlagsFromEnv(t *testing.T) {
|
|||||||
t.Errorf("Flag %s not set", f.Names()[0])
|
t.Errorf("Flag %s not set", f.Names()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check that flag names are returned when set via env as well
|
||||||
|
if !reflect.DeepEqual(ctx.FlagNames(), test.flag.Names()) {
|
||||||
|
t.Errorf("Not enough flag names %+v", ctx.FlagNames())
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -156,8 +156,8 @@ func ExampleMultiStringFlag() {
|
|||||||
//---
|
//---
|
||||||
//Setting all flags via environment...
|
//Setting all flags via environment...
|
||||||
//
|
//
|
||||||
//Flag names: []
|
//Flag names: ["flag-one" "1" "two" "2" "flag-three" "3" "flag-four" "4"]
|
||||||
//Local flag names: []
|
//Local flag names: ["flag-one" "1" "two" "2" "flag-three" "3" "flag-four" "4"]
|
||||||
//Context values:
|
//Context values:
|
||||||
//"flag-one"=["v 9" "v 10"]
|
//"flag-one"=["v 9" "v 10"]
|
||||||
//"two"=["v 11" "v 12"]
|
//"two"=["v 11" "v 12"]
|
||||||
|
Loading…
Reference in New Issue
Block a user