Skip to content

Commit a8ff476

Browse files
committed
Updated to fix a bug
1 parent f0855e2 commit a8ff476

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

argparse_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,54 @@ func TestIntFailSimple1(t *testing.T) {
856856
}
857857
}
858858

859+
func TestEqualIntFailSimple1(t *testing.T) {
860+
testArgs := []string{"progname", "--flag-arg1=string"}
861+
862+
p := NewParser("", "description")
863+
i1 := p.Int("f", "flag-arg1", nil)
864+
865+
err := p.Parse(testArgs)
866+
errStr := "[-f|--flag-arg1] bad integer value [string]"
867+
if err == nil || err.Error() != errStr {
868+
t.Errorf("Test %s expected [%s], got [%+v]", t.Name(), errStr, err)
869+
return
870+
}
871+
872+
if i1 == nil {
873+
t.Errorf("Test %s failed with flag1 being nil pointer", t.Name())
874+
return
875+
}
876+
877+
if *i1 != 0 {
878+
t.Errorf("Test %s failed. Want: [0], got: [%d]", t.Name(), *i1)
879+
return
880+
}
881+
}
882+
883+
func TestEqualNoValFailSimple(t *testing.T) {
884+
testArgs := []string{"progname", "--flag-arg1="}
885+
886+
p := NewParser("", "description")
887+
i1 := p.Int("f", "flag-arg1", nil)
888+
889+
err := p.Parse(testArgs)
890+
errStr := "not enough arguments for -f|--flag-arg1"
891+
if err == nil || err.Error() != errStr {
892+
t.Errorf("Test %s expected [%s], got [%+v]", t.Name(), errStr, err)
893+
return
894+
}
895+
896+
if i1 == nil {
897+
t.Errorf("Test %s failed with flag1 being nil pointer", t.Name())
898+
return
899+
}
900+
901+
if *i1 != 0 {
902+
t.Errorf("Test %s failed. Want: [0], got: [%d]", t.Name(), *i1)
903+
return
904+
}
905+
}
906+
859907
func TestFileAddArgumentFail(t *testing.T) {
860908
type testCase struct {
861909
testName, shortArg, longArg, failureMessage string

command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (o *Command) parseArguments(args *[]string) error {
8787
if cnt, err := oarg.check(equalArg[0]); err != nil {
8888
return err
8989
} else if cnt > 0 {
90-
if len(equalArg) < 2 {
90+
if equalArg[1] == "" {
9191
return fmt.Errorf("not enough arguments for %s", oarg.name())
9292
}
9393
oarg.eqChar = true

0 commit comments

Comments
 (0)