forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag_options.go
151 lines (129 loc) · 3.24 KB
/
flag_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
)
type Opt func(f *Flag) error
func applyFlagOptions(f *Flag, options ...Opt) error {
for _, option := range options {
if err := option(f); err != nil {
return err
}
}
return nil
}
// OptAddNegative automatically adds a --no-<flag> option for boolean flags.
func OptAddNegative() Opt {
return func(f *Flag) error {
f.AddNegative = true
return nil
}
}
// OptShorthand one-letter abbreviated flag
func OptShorthand(shorthand rune) Opt {
return func(f *Flag) error {
f.Shorthand = shorthand
return nil
}
}
// OptShorthandStr one-letter abbreviated flag
func OptShorthandStr(shorthand string) Opt {
r, err := shorthandStrToRune(shorthand)
if err != nil {
panic(err)
}
return OptShorthand(r)
}
// OptShorthandOnly If the user set only the shorthand
func OptShorthandOnly() Opt {
return func(f *Flag) error {
f.ShorthandOnly = true
return nil
}
}
// OptUsage help message
func OptUsage(help string) Opt {
return func(f *Flag) error {
f.Usage = help
return nil
}
}
// OptUsageType flag type displayed in the help message
func OptUsageType(usageType string) Opt {
return func(f *Flag) error {
f.UsageType = usageType
return OptDisableUnquoteUsage()(f)
}
}
// OptDisableUnquoteUsage disable unquoting and extraction of type from usage
func OptDisableUnquoteUsage() Opt {
return func(f *Flag) error {
f.DisableUnquoteUsage = true
return nil
}
}
// OptDisablePrintDefault toggle printing of the default value in usage message
func OptDisablePrintDefault() Opt {
return func(f *Flag) error {
f.DisablePrintDefault = true
return nil
}
}
// OptDefValue default value (as text); for usage message
func OptDefValue(defValue string) Opt {
return func(f *Flag) error {
f.DefValue = defValue
return nil
}
}
// OptDeprecated indicated that a flag is deprecated in your program. It will
// continue to function but will not show up in help or usage messages. Using
// this flag will also print the given usageMessage.
func OptDeprecated(msg string) Opt {
return func(f *Flag) error {
if msg == "" {
return fmt.Errorf("deprecated message for flag %q must be set", f.Name)
}
f.Deprecated = msg
return OptHidden()(f)
}
}
// OptHidden used by zulu.Command to allow flags to be hidden from help/usage text
func OptHidden() Opt {
return func(f *Flag) error {
f.Hidden = true
return nil
}
}
// OptRequired ensures that a flag must be changed
func OptRequired() Opt {
return func(f *Flag) error {
f.Required = true
return nil
}
}
// OptShorthandDeprecated If the shorthand of this flag is deprecated, this string is the new or now thing to use
func OptShorthandDeprecated(msg string) Opt {
return func(f *Flag) error {
if msg == "" {
return fmt.Errorf("shorthand deprecated message for flag %q must be set", f.Name)
}
f.ShorthandDeprecated = msg
return nil
}
}
// OptGroup flag group
func OptGroup(group string) Opt {
return func(f *Flag) error {
f.Group = group
return nil
}
}
// OptAnnotation Use it to annotate this specific flag for your application
func OptAnnotation(key string, value []string) Opt {
return func(f *Flag) error {
f.SetAnnotation(key, value)
return nil
}
}