commands: Added Option helper constructors
Matt Bell committed
Nov 10, 2014 at 16:59 UTC
38b31c65f8d4b85df739905d063f431cfa8b43dc
1 file changed
+33
-1
commands/option.go
+33
-1
@@ -18,11 +18,43 @@ type Option struct {
18
Type reflect.Kind // value must be this type
19
Description string // a short string to describe this option
20
21
- // TODO: add more features(?):
21
+ // MAYBE_TODO: add more features(?):
22
//Default interface{} // the default value (ignored if `Required` is true)
23
//Required bool // whether or not the option must be provided
24
}
25
26
+// constructor helper functions
27
+func NewOption(kind reflect.Kind, names ...string) Option {
28
+ if len(names) < 2 {
29
+ panic("Options require at least two string values (name and description)")
30
+ }
31
+
32
+ desc := names[len(names)-1]
33
+ names = names[:len(names)-2]
34
+
35
+ return Option{
36
+ Names: names,
37
+ Type: kind,
38
+ Description: desc,
39
+ }
40
+}
41
+
42
+func BoolOption(names ...string) Option {
43
+ return NewOption(Bool, names...)
44
+}
45
+func IntOption(names ...string) Option {
46
+ return NewOption(Int, names...)
47
+}
48
+func UintOption(names ...string) Option {
49
+ return NewOption(Uint, names...)
50
+}
51
+func FloatOption(names ...string) Option {
52
+ return NewOption(Float, names...)
53
+}
54
+func StringOption(names ...string) Option {
55
+ return NewOption(String, names...)
56
+}
57
+
58
// Flag names
59
const (
60
EncShort = "enc"