Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ArgClause struct {
hidden bool
required bool
validator OptionValidator
setByUser *bool
}

func newArg(name, help string) *ArgClause {
Expand Down Expand Up @@ -147,6 +148,21 @@ func (a *ArgClause) Required() *ArgClause {
return a
}

// IsSetByUser let to know if the flag was set by the user
func (a *ArgClause) IsSetByUser(setByUser *bool) *ArgClause {
if setByUser != nil {
*setByUser = false
}
a.setByUser = setByUser
return a
}

func (a *ArgClause) isSetByUser() {
if a.setByUser != nil {
*a.setByUser = true
}
}

// Default values for this argument. They *must* be parseable by the value of the argument.
func (a *ArgClause) Default(values ...string) *ArgClause {
a.defaultValues = values
Expand Down
34 changes: 34 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,37 @@ func TestSubcommandArgRequiredWithEnvar(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 123, *flag)
}

func TestArgIsSetByUser(t *testing.T) {
app := newTestApp()
var isSet bool
var b bool
app.Arg("b", "").IsSetByUser(&isSet).Required().BoolVar(&b)
_, err := app.Parse([]string{"true"})
assert.NoError(t, err)
assert.True(t, b)
assert.True(t, isSet)

isSet = false
b = false
_, err = app.Parse([]string{})
assert.Error(t, err)
assert.False(t, b)
assert.False(t, isSet)

app = newTestApp()
app.Arg("b", "").BoolVar(&b)
isSet = false
_, err = app.Parse([]string{"false"})
assert.NoError(t, err)
assert.False(t, b)
assert.False(t, isSet)

app = newTestApp()
app.Arg("b", "").Default("false").BoolVar(&b)
isSet = false
_, err = app.Parse([]string{})
assert.NoError(t, err)
assert.False(t, b)
assert.False(t, isSet)
}
2 changes: 1 addition & 1 deletion flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func TestCombinationEnumOptions(t *testing.T) {

}

func TestIsSetByUser(t *testing.T) {
func TestFlagIsSetByUser(t *testing.T) {
app := newTestApp()
var isSet bool
b := app.Flag("b", "").IsSetByUser(&isSet).Bool()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.0

require (
github.com/stretchr/testify v1.10.0
golang.org/x/text v0.23.0
golang.org/x/text v0.24.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ loop:
break loop
}
context.matchedArg(arg, token.String())
arg.isSetByUser()
context.Next()
} else {
break loop
Expand Down