aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-05-08 11:54:10 -0400
committerEric Paris <[email protected]>2015-06-22 15:29:55 -0400
commitb319e90e90fa3261384806182c8f147a4d16c46a (patch)
tree3aa6fe2c2f76e0360b07c2e1d31d9aa7c6262a4e /flag_test.go
parent9d0766ead9e237647941f8b8970522f5f0d30aa3 (diff)
Set default values if no arg given
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/flag_test.go b/flag_test.go
index 9fbb785..67a6a27 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -27,6 +27,7 @@ var (
test_string = String("test_string", "0", "string value")
test_float64 = Float64("test_float64", 0, "float64 value")
test_duration = Duration("test_duration", 0, "time.Duration value")
+ test_optional_int = Int("test_optional_int", 0, "optional int value")
normalizeFlagNameInvocations = 0
)
@@ -58,7 +59,7 @@ func TestEverything(t *testing.T) {
}
}
VisitAll(visitor)
- if len(m) != 8 {
+ if len(m) != 9 {
t.Error("VisitAll misses some flags")
for k, v := range m {
t.Log(k, *v)
@@ -81,9 +82,10 @@ func TestEverything(t *testing.T) {
Set("test_string", "1")
Set("test_float64", "1")
Set("test_duration", "1s")
+ Set("test_optional_int", "1")
desired = "1"
Visit(visitor)
- if len(m) != 8 {
+ if len(m) != 9 {
t.Error("Visit fails after set")
for k, v := range m {
t.Log(k, *v)
@@ -161,6 +163,10 @@ func testParse(f *FlagSet, t *testing.T) {
ipFlag := f.IP("ip", net.ParseIP("127.0.0.1"), "ip value")
maskFlag := f.IPMask("mask", ParseIPv4Mask("0.0.0.0"), "mask value")
durationFlag := f.Duration("duration", 5*time.Second, "time.Duration value")
+ optionalIntNoValueFlag := f.Int("optional-int-no-value", 0, "int value")
+ f.Lookup("optional-int-no-value").NoOptDefVal = "9"
+ optionalIntWithValueFlag := f.Int("optional-int-with-value", 0, "int value")
+ f.Lookup("optional-int-no-value").NoOptDefVal = "9"
extra := "one-extra-argument"
args := []string{
"--bool",
@@ -181,6 +187,8 @@ func testParse(f *FlagSet, t *testing.T) {
"--ip=10.11.12.13",
"--mask=255.255.255.0",
"--duration=2m",
+ "--optional-int-no-value",
+ "--optional-int-with-value=42",
extra,
}
if err := f.Parse(args); err != nil {
@@ -294,6 +302,12 @@ func testParse(f *FlagSet, t *testing.T) {
if _, err := f.GetInt("duration"); err == nil {
t.Error("GetInt parsed a time.Duration?!?!")
}
+ if *optionalIntNoValueFlag != 9 {
+ t.Error("optional int flag should be the default value, is ", *optionalIntNoValueFlag)
+ }
+ if *optionalIntWithValueFlag != 42 {
+ t.Error("optional int flag should be 42, is ", *optionalIntWithValueFlag)
+ }
if len(f.Args()) != 1 {
t.Error("expected one argument, got", len(f.Args()))
} else if f.Args()[0] != extra {