Support parsing integer literals (#529)
This commit is contained in:
@@ -387,7 +387,7 @@ func intDecoder(bits int) MapperFunc { //nolint: dupl
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
|
return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
|
||||||
}
|
}
|
||||||
n, err := strconv.ParseInt(sv, 10, bits)
|
n, err := strconv.ParseInt(sv, 0, bits)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("expected a valid %d bit int but got %q", bits, sv)
|
return fmt.Errorf("expected a valid %d bit int but got %q", bits, sv)
|
||||||
}
|
}
|
||||||
@@ -416,7 +416,7 @@ func uintDecoder(bits int) MapperFunc { //nolint: dupl
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
|
return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
|
||||||
}
|
}
|
||||||
n, err := strconv.ParseUint(sv, 10, bits)
|
n, err := strconv.ParseUint(sv, 0, bits)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("expected a valid %d bit uint but got %q", bits, sv)
|
return fmt.Errorf("expected a valid %d bit uint but got %q", bits, sv)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -395,6 +395,31 @@ func TestNumbers(t *testing.T) {
|
|||||||
I64: math.MinInt64,
|
I64: math.MinInt64,
|
||||||
}, cli)
|
}, cli)
|
||||||
})
|
})
|
||||||
|
t.Run("Integer literals", func(t *testing.T) {
|
||||||
|
integerLiterals := "10_0"
|
||||||
|
|
||||||
|
_, err := p.Parse([]string{
|
||||||
|
fmt.Sprintf("--i-8=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--i-16=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--i-32=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--i-64=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--u-8=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--u-16=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--u-32=%s", integerLiterals),
|
||||||
|
fmt.Sprintf("--u-64=%s", integerLiterals),
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, CLI{
|
||||||
|
I8: int8(100),
|
||||||
|
I16: int16(100),
|
||||||
|
I32: int32(100),
|
||||||
|
I64: int64(100),
|
||||||
|
U8: uint8(100),
|
||||||
|
U16: uint16(100),
|
||||||
|
U32: uint32(100),
|
||||||
|
U64: uint64(100),
|
||||||
|
}, cli)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJSONLargeNumber(t *testing.T) {
|
func TestJSONLargeNumber(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user