Add negatable tag to set a bool to be negatable.

If negatable, add `--[no-]` prefix to help.
This commit is contained in:
Joe Schmitt
2021-02-18 10:55:55 -05:00
committed by Alec Thomas
parent 2a0d7af9c5
commit 2fdddc4f51
7 changed files with 34 additions and 5 deletions
+6
View File
@@ -33,6 +33,7 @@ type Tag struct {
Prefix string // Optional prefix on anonymous structs. All sub-flags will have this prefix.
Embed bool
Aliases []string
Negatable bool
// Storage for all tag keys for arbitrary lookups.
items map[string][]string
@@ -158,6 +159,11 @@ func parseTag(fv reflect.Value, ft reflect.StructField) *Tag {
t.Xor = t.Get("xor")
t.Prefix = t.Get("prefix")
t.Embed = t.Has("embed")
negatable := t.Has("negatable")
if negatable && ft.Type.Name() != "bool" {
fail("negatable can only be set on booleans")
}
t.Negatable = negatable
splitFn := func(r rune) bool {
return r == ',' || r == ' '
}