Skip to content

Commit

Permalink
Fix:(issue_1731) Add fix for checking if aliases are set
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed May 16, 2023
1 parent c0cc5c2 commit dd8ce75
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
19 changes: 18 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (cCtx *Context) Set(name, value string) error {

// IsSet determines if the flag was actually set
func (cCtx *Context) IsSet(name string) bool {

if fs := cCtx.lookupFlagSet(name); fs != nil {
isSet := false
fs.Visit(func(f *flag.Flag) {
Expand All @@ -72,7 +73,23 @@ func (cCtx *Context) IsSet(name string) bool {
return false
}

return f.IsSet()
if f.IsSet() {
return true
}

// now redo flagset search on aliases
aliases := f.Names()
fs.Visit(func(f *flag.Flag) {
for _, alias := range aliases {
if f.Name == alias {
isSet = true
}
}
})

if isSet {
return true
}
}

return false
Expand Down
33 changes: 33 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,39 @@ func TestContext_IsSet(t *testing.T) {
expect(t, ctx.IsSet("bogus"), false)
}

func TestContext_IsSet_Aliases(t *testing.T) {

var fooSet, fSet, tSet, iglooSet bool

a := App{
Flags: []Flag{
&Int64Flag{
Name: "foo",
Aliases: []string{"f", "t", "igloo"},
},
},
Action: func(ctx *Context) error {
fooSet = ctx.IsSet("foo")
fSet = ctx.IsSet("f")
tSet = ctx.IsSet("t")
iglooSet = ctx.IsSet("igloo")
return nil
},
}

_ = a.Run([]string{"run"})
expect(t, fooSet, false)
expect(t, fSet, false)
expect(t, tSet, false)
expect(t, iglooSet, false)

_ = a.Run([]string{"run", "--t", "10"})
expect(t, fooSet, true)
expect(t, fSet, true)
expect(t, tSet, true)
expect(t, iglooSet, true)
}

// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
// Should be moved to `flag_test` in v2
func TestContext_IsSet_fromEnv(t *testing.T) {
Expand Down

0 comments on commit dd8ce75

Please sign in to comment.