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
6 changes: 2 additions & 4 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,10 @@ func (a *ArgumentsBase[T, C, VC]) Usage() string {
func (a *ArgumentsBase[T, C, VC]) Parse(s []string) ([]string, error) {
tracef("calling arg%[1] parse with args %[2]", &a.Name, s)
if a.Max == 0 {
fmt.Printf("WARNING args %s has max 0, not parsing argument\n", a.Name)
return s, nil
return s, fmt.Errorf("args %s has max 0, not parsing argument", a.Name)
}
if a.Max != -1 && a.Min > a.Max {
fmt.Printf("WARNING args %s has min[%d] > max[%d], not parsing argument\n", a.Name, a.Min, a.Max)
return s, nil
return s, fmt.Errorf("args %s has min[%d] > max[%d], not parsing argument", a.Name, a.Min, a.Max)
}

count := 0
Expand Down
28 changes: 28 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ func TestArgNotSet(t *testing.T) {
require.Equal(t, "foo", arg.Get())
}

func TestArgsMaxNotSet(t *testing.T) {
arg := &StringArgs{
Name: "sa",
Value: "foo",
}

cmd := buildMinimalTestCommand()
cmd.Arguments = []Argument{arg}

err := cmd.Run(buildTestContext(t), []string{"foo", "bar"})
require.ErrorContains(t, err, "args sa has max 0, not parsing argument")
}

func TestArgsMinGtMax(t *testing.T) {
arg := &StringArgs{
Name: "sa",
Value: "foo",
Min: 2,
Max: 1,
}

cmd := buildMinimalTestCommand()
cmd.Arguments = []Argument{arg}

err := cmd.Run(buildTestContext(t), []string{"foo", "bar"})
require.ErrorContains(t, err, "args sa has min[2] > max[1], not parsing argument")
}

func TestArgsFloatTypes(t *testing.T) {
cmd := buildMinimalTestCommand()
var fval float64
Expand Down
10 changes: 4 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ func HandleExitCoder(err error) {
}

if exitErr, ok := err.(ExitCoder); ok {
if err.Error() != "" {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dearchap was this code change intentional? Calling Exit with an empty message now adds an unnecessary empty line to stderr:

cli.Exit("", exitError.ExitCode())

if _, ok := exitErr.(ErrorFormatter); ok {
_, _ = fmt.Fprintf(ErrWriter, "%+v\n", err)
} else {
_, _ = fmt.Fprintln(ErrWriter, err)
}
if _, ok := exitErr.(ErrorFormatter); ok {
_, _ = fmt.Fprintf(ErrWriter, "%+v\n", err)
} else {
_, _ = fmt.Fprintln(ErrWriter, err)
}
OsExiter(exitErr.ExitCode())
return
Expand Down
4 changes: 0 additions & 4 deletions flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,3 @@ func (b *boolValue) String() string {
}

func (b *boolValue) IsBoolFlag() bool { return true }

func (b *boolValue) Count() int {
return *b.count
}
32 changes: 32 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,26 @@ func TestZeroValueMutexFlag(t *testing.T) {
assert.NoError(t, fl.check(&Command{}))
}

func TestMutexFlagCategory(t *testing.T) {
cmd := buildMinimalTestCommand()
cmd.Category = "TestCmd"
cmd.MutuallyExclusiveFlags = []MutuallyExclusiveFlags{
{
Flags: [][]Flag{
{
&StringFlag{Name: "foo", Category: "Group1"},
&IntFlag{Name: "bar", Category: "Group1"},
},
{
&StringFlag{Name: "baz", Category: "Group2"},
},
},
},
}

assert.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--foo", "value"}))
}

func TestExtFlag(t *testing.T) {
var iv intValue[int64]
var ipv int64
Expand Down Expand Up @@ -3467,3 +3487,15 @@ func TestGenericValue(t *testing.T) {
assert.Nil(t, g.Get())
assert.Empty(t, g.String())
}

func TestEndValue(t *testing.T) {
cmd := buildMinimalTestCommand()
cmd.UseShortOptionHandling = true
cmd.Flags = []Flag{
&IntFlag{Name: "debug", Aliases: []string{"d"}},
&IntFlag{Name: "count", Aliases: []string{"c"}},
}

assert.Error(t, cmd.Run(buildTestContext(t), []string{"foo", "-cd="}))
assert.Error(t, cmd.Run(buildTestContext(t), []string{"foo", "-cd=s"}))
}