]> Cypherpunks repositories - gostls13.git/commitdiff
flag: allow hexadecimal and octal input for integer flags.
authorLuuk van Dijk <lvd@golang.org>
Fri, 11 Feb 2011 11:23:54 +0000 (12:23 +0100)
committerLuuk van Dijk <lvd@golang.org>
Fri, 11 Feb 2011 11:23:54 +0000 (12:23 +0100)
R=r
CC=golang-dev
https://golang.org/cl/4182043

src/pkg/flag/flag.go
src/pkg/flag/flag_test.go

index 143a106115021d7f5d313110a3f19b3c4311d3b7..be972057ed74c08720e1bc6dd727fcd613223d58 100644 (file)
@@ -96,7 +96,7 @@ func newIntValue(val int, p *int) *intValue {
 }
 
 func (i *intValue) Set(s string) bool {
-       v, err := strconv.Atoi(s)
+       v, err := strconv.Btoi64(s, 0)
        *i = intValue(v)
        return err == nil
 }
@@ -112,7 +112,7 @@ func newInt64Value(val int64, p *int64) *int64Value {
 }
 
 func (i *int64Value) Set(s string) bool {
-       v, err := strconv.Atoi64(s)
+       v, err := strconv.Btoi64(s, 0)
        *i = int64Value(v)
        return err == nil
 }
@@ -128,7 +128,7 @@ func newUintValue(val uint, p *uint) *uintValue {
 }
 
 func (i *uintValue) Set(s string) bool {
-       v, err := strconv.Atoui(s)
+       v, err := strconv.Btoui64(s, 0)
        *i = uintValue(v)
        return err == nil
 }
@@ -144,7 +144,7 @@ func newUint64Value(val uint64, p *uint64) *uint64Value {
 }
 
 func (i *uint64Value) Set(s string) bool {
-       v, err := strconv.Atoui64(s)
+       v, err := strconv.Btoui64(s, 0)
        *i = uint64Value(v)
        return err == nil
 }
index b91a8b567956e7f762f9c1c140a2468ebc268054..30a21e61ae2d46d91be9f5e887c0d633edd46505 100644 (file)
@@ -106,7 +106,7 @@ func TestParse(t *testing.T) {
                "-bool",
                "-bool2=true",
                "--int", "22",
-               "--int64", "23",
+               "--int64", "0x23",
                "-uint", "24",
                "--uint64", "25",
                "-string", "hello",
@@ -125,8 +125,8 @@ func TestParse(t *testing.T) {
        if *intFlag != 22 {
                t.Error("int flag should be 22, is ", *intFlag)
        }
-       if *int64Flag != 23 {
-               t.Error("int64 flag should be 23, is ", *int64Flag)
+       if *int64Flag != 0x23 {
+               t.Error("int64 flag should be 0x23, is ", *int64Flag)
        }
        if *uintFlag != 24 {
                t.Error("uint flag should be 24, is ", *uintFlag)