aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-05-30 21:07:46 -0400
committerEric Paris <[email protected]>2015-06-01 18:45:34 -0400
commit1e0a23de9163cb0706137856f1d060f88e3f277c (patch)
treede398763dd538847a8c32d4073ce07d5fe8bb210 /flag_test.go
parent5644820622454e71517561946e3d94b9f9db6842 (diff)
Add new FlagSet.Get{Int,String,...} accessor functions
If I declared a bool flag named "hello" I can now call b, err := f.GetBool("hello") And b will hold the value of the flag We can see this is already done in https://github.com/codegangsta/cli/blob/bcec9b08c7e5564f7512ad7e7b03778fe1923116/context.go If people use the codegangsta/cli Other projects have done it themselves using pflags (what inspired this patch) https://github.com/GoogleCloudPlatform/kubernetes/blob/cd817aebd848facda29e0befbbd6e31bf22402e6/pkg/kubectl/cmd/util/helpers.go#L176 Lets just do it ourselves...
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go57
1 files changed, 54 insertions, 3 deletions
diff --git a/flag_test.go b/flag_test.go
index d3c1714..e3c5c97 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -160,6 +160,9 @@ func testParse(f *FlagSet, t *testing.T) {
if *boolFlag != true {
t.Error("bool flag should be true, is ", *boolFlag)
}
+ if v, err := f.GetBool("bool"); err != nil || v != *boolFlag {
+ t.Error("GetBool does not work.")
+ }
if *bool2Flag != true {
t.Error("bool2 flag should be true, is ", *bool2Flag)
}
@@ -169,48 +172,96 @@ func testParse(f *FlagSet, t *testing.T) {
if *intFlag != 22 {
t.Error("int flag should be 22, is ", *intFlag)
}
+ if v, err := f.GetInt("int"); err != nil || v != *intFlag {
+ t.Error("GetInt does not work.")
+ }
if *int8Flag != -8 {
t.Error("int8 flag should be 0x23, is ", *int8Flag)
}
+ if v, err := f.GetInt8("int8"); err != nil || v != *int8Flag {
+ t.Error("GetInt8 does not work.")
+ }
if *int32Flag != -32 {
t.Error("int32 flag should be 0x23, is ", *int32Flag)
}
+ if v, err := f.GetInt32("int32"); err != nil || v != *int32Flag {
+ t.Error("GetInt32 does not work.")
+ }
if *int64Flag != 0x23 {
t.Error("int64 flag should be 0x23, is ", *int64Flag)
}
+ if v, err := f.GetInt64("int64"); err != nil || v != *int64Flag {
+ t.Error("GetInt64 does not work.")
+ }
if *uintFlag != 24 {
t.Error("uint flag should be 24, is ", *uintFlag)
}
+ if v, err := f.GetUint("uint"); err != nil || v != *uintFlag {
+ t.Error("GetUint does not work.")
+ }
if *uint8Flag != 8 {
t.Error("uint8 flag should be 8, is ", *uint8Flag)
}
+ if v, err := f.GetUint8("uint8"); err != nil || v != *uint8Flag {
+ t.Error("GetUint8 does not work.")
+ }
if *uint16Flag != 16 {
t.Error("uint16 flag should be 16, is ", *uint16Flag)
}
+ if v, err := f.GetUint16("uint16"); err != nil || v != *uint16Flag {
+ t.Error("GetUint16 does not work.")
+ }
if *uint32Flag != 32 {
t.Error("uint32 flag should be 32, is ", *uint32Flag)
}
+ if v, err := f.GetUint32("uint32"); err != nil || v != *uint32Flag {
+ t.Error("GetUint32 does not work.")
+ }
if *uint64Flag != 25 {
t.Error("uint64 flag should be 25, is ", *uint64Flag)
}
+ if v, err := f.GetUint64("uint64"); err != nil || v != *uint64Flag {
+ t.Error("GetUint64 does not work.")
+ }
if *stringFlag != "hello" {
t.Error("string flag should be `hello`, is ", *stringFlag)
}
+ if v, err := f.GetString("string"); err != nil || v != *stringFlag {
+ t.Error("GetString does not work.")
+ }
if *float32Flag != -172e12 {
- t.Error("float64 flag should be -172e12, is ", *float64Flag)
+ t.Error("float32 flag should be -172e12, is ", *float32Flag)
+ }
+ if v, err := f.GetFloat32("float32"); err != nil || v != *float32Flag {
+ t.Errorf("GetFloat32 returned %v but float32Flag was %v", v, *float32Flag)
}
if *float64Flag != 2718e28 {
t.Error("float64 flag should be 2718e28, is ", *float64Flag)
}
- if (*maskFlag).String() != ParseIPv4Mask("255.255.255.0").String() {
- t.Error("mask flag should be 255.255.255.0, is ", (*maskFlag).String())
+ if v, err := f.GetFloat64("float64"); err != nil || v != *float64Flag {
+ t.Errorf("GetFloat64 returned %v but float64Flag was %v", v, *float64Flag)
}
if !(*ipFlag).Equal(net.ParseIP("10.11.12.13")) {
t.Error("ip flag should be 10.11.12.13, is ", *ipFlag)
}
+ if v, err := f.GetIP("ip"); err != nil || !v.Equal(*ipFlag) {
+ t.Errorf("GetIP returned %v but ipFlag was %v", v, *ipFlag)
+ }
+ if (*maskFlag).String() != ParseIPv4Mask("255.255.255.0").String() {
+ t.Error("mask flag should be 255.255.255.0, is ", (*maskFlag).String())
+ }
+ if v, err := f.GetIPv4Mask("mask"); err != nil || v.String() != (*maskFlag).String() {
+ t.Errorf("GetIP returned %v but maskFlag was %v", v, *maskFlag, err)
+ }
if *durationFlag != 2*time.Minute {
t.Error("duration flag should be 2m, is ", *durationFlag)
}
+ if v, err := f.GetDuration("duration"); err != nil || v != *durationFlag {
+ t.Error("GetDuration does not work.")
+ }
+ if _, err := f.GetInt("duration"); err == nil {
+ t.Error("GetInt parsed a time.Duration?!?!")
+ }
if len(f.Args()) != 1 {
t.Error("expected one argument, got", len(f.Args()))
} else if f.Args()[0] != extra {