Skip to content

Commit

Permalink
Merge pull request #1732 from dearchap/issue_1731
Browse files Browse the repository at this point in the history
Fix:(issue_1731) Add fix for checking if aliases are set
  • Loading branch information
dearchap committed May 27, 2023
2 parents c1bfd14 + 1ba66cf commit 24fa0fd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
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
5 changes: 2 additions & 3 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ func (a *App) RunAndExitOnError()
code in the cli.ExitCoder

func (a *App) RunAsSubcommand(ctx *Context) (err error)
This is a stub function to keep public API unchanged from old code

Deprecated: use App.Run or App.RunContext
RunAsSubcommand is for legacy/compatibility purposes only. New code should
only use App.RunContextMost. This function is slated to be removed in v3

func (a *App) RunContext(ctx context.Context, arguments []string) (err error)
RunContext is like Run except it takes a Context that will be passed to
Expand Down
5 changes: 2 additions & 3 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ func (a *App) RunAndExitOnError()
code in the cli.ExitCoder

func (a *App) RunAsSubcommand(ctx *Context) (err error)
This is a stub function to keep public API unchanged from old code

Deprecated: use App.Run or App.RunContext
RunAsSubcommand is for legacy/compatibility purposes only. New code should
only use App.RunContextMost. This function is slated to be removed in v3

func (a *App) RunContext(ctx context.Context, arguments []string) (err error)
RunContext is like Run except it takes a Context that will be passed to
Expand Down

0 comments on commit 24fa0fd

Please sign in to comment.