Skip to content

Commit 2efcea8

Browse files
committed
fix(flag): loosen naming restrictions for flags to include periods
Allow flag names to include periods, which is often desired for organizing flags (e.g. 'web.bind-address').
1 parent 45eefc7 commit 2efcea8

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

flag/flagset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ func (f *FlagSet) Var(value Value, name string, usage string) {
324324
}
325325

326326
invalid := strings.ContainsFunc(name, func(r rune) bool {
327-
return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-'
327+
return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '.'
328328
})
329329
if invalid {
330-
panic(fmt.Sprintf("flag '%s' has invalid name (must be alphanumeric and '-')", name))
330+
panic(fmt.Sprintf("flag '%s' has invalid name (must be alphanumeric and any of '-.')", name))
331331
}
332332

333333
if f.Lookup(name) != nil {

flag/flagset_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ func TestFlagSet(t *testing.T) {
7070
t.Fatalf("count var not updated with expected default value")
7171
}
7272
})
73+
74+
t.Run("should allow flag names with periods", func(t *testing.T) {
75+
var addr string
76+
77+
fs := NewFlagSet("test", ContinueOnError)
78+
fs.StringVar(&addr, "web.listen-address", ":8080", "bind `address` for the web server")
79+
80+
if addr != ":8080" {
81+
t.Fatalf("addr var not updated with expected default value")
82+
}
83+
})
7384
})
7485

7586
t.Run("Lookup", func(t *testing.T) {

0 commit comments

Comments
 (0)