|
| 1 | +package cmdsutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" |
| 9 | +) |
| 10 | + |
| 11 | +// Types of Command options |
| 12 | +const ( |
| 13 | + Invalid = reflect.Invalid |
| 14 | + Bool = reflect.Bool |
| 15 | + Int = reflect.Int |
| 16 | + Uint = reflect.Uint |
| 17 | + Float = reflect.Float64 |
| 18 | + String = reflect.String |
| 19 | +) |
| 20 | + |
| 21 | +type OptMap map[string]interface{} |
| 22 | + |
| 23 | +// Option is used to specify a field that will be provided by a consumer |
| 24 | +type Option interface { |
| 25 | + Names() []string // a list of unique names matched with user-provided flags |
| 26 | + Type() reflect.Kind // value must be this type |
| 27 | + Description() string // a short string that describes this option |
| 28 | + Default(interface{}) Option // sets the default value of the option |
| 29 | + DefaultVal() interface{} |
| 30 | +} |
| 31 | + |
| 32 | +type option struct { |
| 33 | + names []string |
| 34 | + kind reflect.Kind |
| 35 | + description string |
| 36 | + defaultVal interface{} |
| 37 | +} |
| 38 | + |
| 39 | +func (o *option) Names() []string { |
| 40 | + return o.names |
| 41 | +} |
| 42 | + |
| 43 | +func (o *option) Type() reflect.Kind { |
| 44 | + return o.kind |
| 45 | +} |
| 46 | + |
| 47 | +func (o *option) Description() string { |
| 48 | + if len(o.description) == 0 { |
| 49 | + return "" |
| 50 | + } |
| 51 | + if !strings.HasSuffix(o.description, ".") { |
| 52 | + o.description += "." |
| 53 | + } |
| 54 | + if o.defaultVal != nil { |
| 55 | + if strings.Contains(o.description, "<<default>>") { |
| 56 | + return strings.Replace(o.description, "<<default>>", |
| 57 | + fmt.Sprintf("Default: %v.", o.defaultVal), -1) |
| 58 | + } else { |
| 59 | + return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal) |
| 60 | + } |
| 61 | + } |
| 62 | + return o.description |
| 63 | +} |
| 64 | + |
| 65 | +// constructor helper functions |
| 66 | +func NewOption(kind reflect.Kind, names ...string) Option { |
| 67 | + if len(names) < 2 { |
| 68 | + // FIXME(btc) don't panic (fix_before_merge) |
| 69 | + panic("Options require at least two string values (name and description)") |
| 70 | + } |
| 71 | + |
| 72 | + desc := names[len(names)-1] |
| 73 | + names = names[:len(names)-1] |
| 74 | + |
| 75 | + return &option{ |
| 76 | + names: names, |
| 77 | + kind: kind, |
| 78 | + description: desc, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (o *option) Default(v interface{}) Option { |
| 83 | + o.defaultVal = v |
| 84 | + return o |
| 85 | +} |
| 86 | + |
| 87 | +func (o *option) DefaultVal() interface{} { |
| 88 | + return o.defaultVal |
| 89 | +} |
| 90 | + |
| 91 | +// TODO handle description separately. this will take care of the panic case in |
| 92 | +// NewOption |
| 93 | + |
| 94 | +// For all func {Type}Option(...string) functions, the last variadic argument |
| 95 | +// is treated as the description field. |
| 96 | + |
| 97 | +func BoolOption(names ...string) Option { |
| 98 | + return NewOption(Bool, names...) |
| 99 | +} |
| 100 | +func IntOption(names ...string) Option { |
| 101 | + return NewOption(Int, names...) |
| 102 | +} |
| 103 | +func UintOption(names ...string) Option { |
| 104 | + return NewOption(Uint, names...) |
| 105 | +} |
| 106 | +func FloatOption(names ...string) Option { |
| 107 | + return NewOption(Float, names...) |
| 108 | +} |
| 109 | +func StringOption(names ...string) Option { |
| 110 | + return NewOption(String, names...) |
| 111 | +} |
| 112 | + |
| 113 | +type OptionValue struct { |
| 114 | + Value interface{} |
| 115 | + ValueFound bool |
| 116 | + Def Option |
| 117 | +} |
| 118 | + |
| 119 | +// Found returns true if the option value was provided by the user (not a default value) |
| 120 | +func (ov OptionValue) Found() bool { |
| 121 | + return ov.ValueFound |
| 122 | +} |
| 123 | + |
| 124 | +// Definition returns the option definition for the provided value |
| 125 | +func (ov OptionValue) Definition() Option { |
| 126 | + return ov.Def |
| 127 | +} |
| 128 | + |
| 129 | +// value accessor methods, gets the value as a certain type |
| 130 | +func (ov OptionValue) Bool() (value bool, found bool, err error) { |
| 131 | + if !ov.ValueFound && ov.Value == nil { |
| 132 | + return false, false, nil |
| 133 | + } |
| 134 | + val, ok := ov.Value.(bool) |
| 135 | + if !ok { |
| 136 | + err = util.ErrCast() |
| 137 | + } |
| 138 | + return val, ov.ValueFound, err |
| 139 | +} |
| 140 | + |
| 141 | +func (ov OptionValue) Int() (value int, found bool, err error) { |
| 142 | + if !ov.ValueFound && ov.Value == nil { |
| 143 | + return 0, false, nil |
| 144 | + } |
| 145 | + val, ok := ov.Value.(int) |
| 146 | + if !ok { |
| 147 | + err = util.ErrCast() |
| 148 | + } |
| 149 | + return val, ov.ValueFound, err |
| 150 | +} |
| 151 | + |
| 152 | +func (ov OptionValue) Uint() (value uint, found bool, err error) { |
| 153 | + if !ov.ValueFound && ov.Value == nil { |
| 154 | + return 0, false, nil |
| 155 | + } |
| 156 | + val, ok := ov.Value.(uint) |
| 157 | + if !ok { |
| 158 | + err = util.ErrCast() |
| 159 | + } |
| 160 | + return val, ov.ValueFound, err |
| 161 | +} |
| 162 | + |
| 163 | +func (ov OptionValue) Float() (value float64, found bool, err error) { |
| 164 | + if !ov.ValueFound && ov.Value == nil { |
| 165 | + return 0, false, nil |
| 166 | + } |
| 167 | + val, ok := ov.Value.(float64) |
| 168 | + if !ok { |
| 169 | + err = util.ErrCast() |
| 170 | + } |
| 171 | + return val, ov.ValueFound, err |
| 172 | +} |
| 173 | + |
| 174 | +func (ov OptionValue) String() (value string, found bool, err error) { |
| 175 | + if !ov.ValueFound && ov.Value == nil { |
| 176 | + return "", false, nil |
| 177 | + } |
| 178 | + val, ok := ov.Value.(string) |
| 179 | + if !ok { |
| 180 | + err = util.ErrCast() |
| 181 | + } |
| 182 | + return val, ov.ValueFound, err |
| 183 | +} |
| 184 | + |
| 185 | +// Flag names |
| 186 | +const ( |
| 187 | + EncShort = "enc" |
| 188 | + EncLong = "encoding" |
| 189 | + RecShort = "r" |
| 190 | + RecLong = "recursive" |
| 191 | + ChanOpt = "stream-channels" |
| 192 | + TimeoutOpt = "timeout" |
| 193 | +) |
| 194 | + |
| 195 | +// options that are used by this package |
| 196 | +var OptionEncodingType = StringOption(EncLong, EncShort, "The encoding type the output should be encoded with (json, xml, or text)") |
| 197 | +var OptionRecursivePath = BoolOption(RecLong, RecShort, "Add directory paths recursively").Default(false) |
| 198 | +var OptionStreamChannels = BoolOption(ChanOpt, "Stream channel output") |
| 199 | +var OptionTimeout = StringOption(TimeoutOpt, "set a global timeout on the command") |
0 commit comments