@@ -38,7 +38,14 @@ But if you instead want to handle one-of-a-set flags as properly typed
38
38
enumerations instead of strings, or if you need (multiple-of-a-set) slice
39
39
support, then please read on.
40
40
41
- ## How To Use: Properly Typed Enum Flag
41
+ ## How To Use
42
+
43
+ - [ start with your own enum types] ( #start-with-your-own-enum-types ) ,
44
+ - [ use existing enum types and non-zero defaults] ( #use-existing-enum-types ) ,
45
+ - [ CLI flag with default] ( #cli-flag-with-default ) ,
46
+ - [ slice of enums] ( #slice-of-enums ) .
47
+
48
+ ### Start With Your Own Enum Types
42
49
43
50
Without further ado, here's how to define and use enum flags in your own
44
51
applications...
@@ -69,7 +76,8 @@ var FooModeIds = map[FooMode][]string{
69
76
Bar : {" bar" },
70
77
}
71
78
72
- // ④ Now use the FooMode enum flag.
79
+ // ④ Now use the FooMode enum flag. If you want a non-zero default, then simply
80
+ // set it here, such as in "foomode = Bar".
73
81
var foomode FooMode
74
82
75
83
func main () {
@@ -98,9 +106,104 @@ The boilerplate pattern is always the same:
98
106
3 . Define the mapping of the constants onto enum values (textual
99
107
representations).
100
108
4 . Somewhere, declare a flag variable of your enum flag type.
109
+ - If you want to use a non-zero default enum value, just go ahead and set
110
+ it: ` var foomode = Bar ` . It will be used correctly.
101
111
5 . Wire up your flag variable to its flag long and short names, et cetera.
102
112
103
- ## How To Use: Slice of Enums
113
+ ### Use Existing Enum Types
114
+
115
+ A typical example might be your application using a 3rd party logging package
116
+ and you want to offer a ` -v ` log level CLI flag. Here, we use the existing 3rd
117
+ party enum values and set a non-zero default for our logging CLI flag.
118
+
119
+ Considering the boiler plate shown above, we can now leave out steps ① and ②,
120
+ because these definitions come from a 3rd party package. We only need to
121
+ supply the textual enum names as ③.
122
+
123
+ ``` go
124
+ import (
125
+ " fmt"
126
+ " os"
127
+
128
+ log " github.com/sirupsen/logrus"
129
+ " github.com/spf13/cobra"
130
+ " github.com/thediveo/enumflag"
131
+ )
132
+
133
+ func main () {
134
+ // ①+② skip "define your own enum flag type" and enumeration values, as we
135
+ // already have a 3rd party one.
136
+
137
+ // ③ Map 3rd party enumeration values to their textual representations
138
+ var LoglevelIds = map [log.Level ][]string {
139
+ log.TraceLevel : {" trace" },
140
+ log.DebugLevel : {" debug" },
141
+ log.InfoLevel : {" info" },
142
+ log.WarnLevel : {" warning" , " warn" },
143
+ log.ErrorLevel : {" error" },
144
+ log.FatalLevel : {" fatal" },
145
+ log.PanicLevel : {" panic" },
146
+ }
147
+
148
+ // ④ Define your enum flag value and set the your logging default value.
149
+ var loglevel log.Level = log.WarnLevel
150
+
151
+ rootCmd := &cobra.Command {
152
+ Run: func (cmd *cobra.Command , _ []string ) {
153
+ fmt.Printf (" logging level is: %d =%q \n " ,
154
+ loglevel,
155
+ cmd.PersistentFlags ().Lookup (" log" ).Value .String ())
156
+ },
157
+ }
158
+
159
+ // ⑤ Define the CLI flag parameters for your wrapped enum flag.
160
+ rootCmd.PersistentFlags ().Var (
161
+ enumflag.New (&loglevel, " log" , LoglevelIds, enumflag.EnumCaseInsensitive ),
162
+ " log" ,
163
+ " sets logging level; can be 'trace', 'debug', 'info', 'warn', 'error', 'fatal', 'panic'" )
164
+
165
+ // Defaults to what we set above: warn level.
166
+ _ = rootCmd.Execute ()
167
+
168
+ // User specifies a specific level, such as log level.
169
+ rootCmd.SetArgs ([]string {" --log" , " debug" })
170
+ _ = rootCmd.Execute ()
171
+ }
172
+ ```
173
+
174
+ ### CLI Flag With Default
175
+
176
+ Sometimes you might want a CLI enum flag to have a default value when the user
177
+ specifies the CLI flag, but not its value. A good example is the ` --color `
178
+ flag of the ` ls ` command:
179
+
180
+ - if just specified as ` --color ` without a value, it
181
+ will default to the value of ` auto ` ;
182
+ - otherwise, as specific value can be given, such as
183
+ - ` --color=always ` ,
184
+ - ` --color=never ` ,
185
+ - or even ` --color=auto ` .
186
+
187
+ In such situations, use spf13/pflags's
188
+ [ ` NoOptDefVal ` ] ( https://godoc.org/github.com/spf13/pflag#Flag ) to set the
189
+ flag's default value * as text* , if the flag is on the command line without any
190
+ options.
191
+
192
+ The gist here is as follows, please see also
193
+ [ colormode.go] ( https://github.com/TheDiveO/lxkns/blob/master/cmd/internal/pkg/style/colormode.go )
194
+ from my [ lxkns] ( https://github.com/TheDiveO/lxkns ) Linux namespaces discovery
195
+ project:
196
+
197
+ ``` go
198
+ rootCmd.PersistentFlags ().VarP (
199
+ enumflag.New (&colorize, " color" , colorModeIds, enumflag.EnumCaseSensitive ),
200
+ " color" , " c" ,
201
+ " colorize the output; can be 'always' (default if omitted), 'auto',\n " +
202
+ " or 'never'" )
203
+ rootCmd.PersistentFlags ().Lookup (" color" ).NoOptDefVal = " always"
204
+ ```
205
+
206
+ ### Slice of Enums
104
207
105
208
For a slice of enumerations, simply declare your variable to be a slice of your
106
209
enumeration type and then use ` enumflag.NewSlice(...) ` instead of
0 commit comments