github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/commands/option.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "reflect" 6 ) 7 8 // Types of Command options 9 const ( 10 Invalid = reflect.Invalid 11 Bool = reflect.Bool 12 Int = reflect.Int 13 Uint = reflect.Uint 14 Float = reflect.Float64 15 String = reflect.String 16 ) 17 18 // Option is used to specify a field that will be provided by a consumer 19 type Option struct { 20 Names []string // a list of unique names to 21 Type reflect.Kind // value must be this type 22 Description string // a short string to describe this option 23 24 // MAYBE_TODO: add more features(?): 25 //Default interface{} // the default value (ignored if `Required` is true) 26 //Required bool // whether or not the option must be provided 27 } 28 29 // constructor helper functions 30 func NewOption(kind reflect.Kind, names ...string) Option { 31 if len(names) < 2 { 32 panic("Options require at least two string values (name and description)") 33 } 34 35 desc := names[len(names)-1] 36 names = names[:len(names)-1] 37 38 return Option{ 39 Names: names, 40 Type: kind, 41 Description: desc, 42 } 43 } 44 45 func BoolOption(names ...string) Option { 46 return NewOption(Bool, names...) 47 } 48 func IntOption(names ...string) Option { 49 return NewOption(Int, names...) 50 } 51 func UintOption(names ...string) Option { 52 return NewOption(Uint, names...) 53 } 54 func FloatOption(names ...string) Option { 55 return NewOption(Float, names...) 56 } 57 func StringOption(names ...string) Option { 58 return NewOption(String, names...) 59 } 60 61 type OptionValue struct { 62 value interface{} 63 found bool 64 } 65 66 // Found returns true if the option value was provided by the user (not a default value) 67 func (ov OptionValue) Found() bool { 68 return ov.found 69 } 70 71 // value accessor methods, gets the value as a certain type 72 func (ov OptionValue) Bool() (bool, error) { 73 val, ok := ov.value.(bool) 74 if !ok { 75 var err error 76 if ov.value != nil { 77 err = errors.New("error casting to bool") 78 } 79 return false, err 80 } 81 return val, nil 82 } 83 func (ov OptionValue) Int() (int, error) { 84 val, ok := ov.value.(int) 85 if !ok { 86 var err error 87 if ov.value != nil { 88 err = errors.New("error casting to int") 89 } 90 return 0, err 91 } 92 return val, nil 93 } 94 func (ov OptionValue) Uint() (uint, error) { 95 val, ok := ov.value.(uint) 96 if !ok { 97 var err error 98 if ov.value != nil { 99 err = errors.New("error casting to uint") 100 } 101 return 0, err 102 } 103 return val, nil 104 } 105 func (ov OptionValue) Float() (float64, error) { 106 val, ok := ov.value.(float64) 107 if !ok { 108 var err error 109 if ov.value != nil { 110 err = errors.New("error casting to float64") 111 } 112 return 0.0, err 113 } 114 return val, nil 115 } 116 func (ov OptionValue) String() (string, error) { 117 val, ok := ov.value.(string) 118 if !ok { 119 var err error 120 if ov.value != nil { 121 err = errors.New("error casting to string") 122 } 123 return "", err 124 } 125 return val, nil 126 } 127 128 // Flag names 129 const ( 130 EncShort = "enc" 131 EncLong = "encoding" 132 ) 133 134 // options that are used by this package 135 var globalOptions = []Option{ 136 Option{[]string{EncShort, EncLong}, String, 137 "The encoding type the output should be encoded with (json, xml, or text)"}, 138 } 139 140 // the above array of Options, wrapped in a Command 141 var globalCommand = &Command{ 142 Options: globalOptions, 143 }