Support parsing integer literals (#529)

This commit is contained in:
Sam Xie
2025-04-23 15:24:47 -07:00
committed by GitHub
parent f0b321097e
commit 66d5762b66
2 changed files with 27 additions and 2 deletions
+2 -2
View File
@@ -387,7 +387,7 @@ func intDecoder(bits int) MapperFunc { //nolint: dupl
default:
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 {
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:
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 {
return fmt.Errorf("expected a valid %d bit uint but got %q", bits, sv)
}
+25
View File
@@ -395,6 +395,31 @@ func TestNumbers(t *testing.T) {
I64: math.MinInt64,
}, 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) {