summaryrefslogtreecommitdiff
path: root/src/flag/flag_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/flag/flag_test.go')
-rw-r--r--src/flag/flag_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/flag/flag_test.go b/src/flag/flag_test.go
index 8c88c8c27..80d92a5e7 100644
--- a/src/flag/flag_test.go
+++ b/src/flag/flag_test.go
@@ -377,3 +377,63 @@ func TestHelp(t *testing.T) {
t.Fatal("help was called; should not have been for defined help flag")
}
}
+
+// Test that a standard flag can be set only once. Need to verify every flag type.
+// User-defined flags can be set multiple times, and this is verified in the tests above.
+func TestErrorOnMultipleSettings(t *testing.T) {
+ check := func(typ string, err error) {
+ if err == nil {
+ t.Errorf("%s flag can be set multiple times")
+ } else if !strings.Contains(err.Error(), "flag set multiple") {
+ t.Fatalf("expected multiple setting error, got %q", err)
+ }
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Bool("v", false, "usage")
+ check("bool", flags.Parse([]string{"-v", "-v"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Int("v", 0, "usage")
+ check("int", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Int64("v", 0, "usage")
+ check("int64", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Uint("v", 0, "usage")
+ check("uint", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Uint64("v", 0, "usage")
+ check("uint64", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.String("v", "", "usage")
+ check("string", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Float64("v", 0, "usage")
+ check("float64", flags.Parse([]string{"-v", "1", "-v", "2"}))
+ }
+ {
+ var flags FlagSet
+ flags.Init("test", ContinueOnError)
+ flags.Duration("v", 0, "usage")
+ check("duration", flags.Parse([]string{"-v", "1s", "-v", "2s"}))
+ }
+}