| 1 |
package flags |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
) |
| 6 |
|
| 7 |
func (option *Option) canArgument() bool { |
| 8 |
return !option.isBool() |
| 9 |
} |
| 10 |
|
| 11 |
func (option *Option) clear() { |
| 12 |
tp := option.Value.Type() |
| 13 |
|
| 14 |
switch tp.Kind() { |
| 15 |
case reflect.Func: |
| 16 |
|
| 17 |
case reflect.Map: |
| 18 |
|
| 19 |
option.Value.Set(reflect.MakeMap(tp)) |
| 20 |
default: |
| 21 |
zeroval := reflect.Zero(tp) |
| 22 |
option.Value.Set(zeroval) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func (option *Option) isBool() bool { |
| 27 |
tp := option.Value.Type() |
| 28 |
|
| 29 |
switch tp.Kind() { |
| 30 |
case reflect.Bool: |
| 31 |
return true |
| 32 |
case reflect.Slice: |
| 33 |
return (tp.Elem().Kind() == reflect.Bool) |
| 34 |
case reflect.Func: |
| 35 |
return tp.NumIn() == 0 |
| 36 |
} |
| 37 |
|
| 38 |
return false |
| 39 |
} |
| 40 |
|
| 41 |
func (option *Option) isFunc() bool { |
| 42 |
return option.Value.Type().Kind() == reflect.Func |
| 43 |
} |
| 44 |
|
| 45 |
func (option *Option) iniName() string { |
| 46 |
if len(option.iniUsedName) != 0 { |
| 47 |
return option.iniUsedName |
| 48 |
} |
| 49 |
|
| 50 |
name := option.tag.Get("ini-name") |
| 51 |
|
| 52 |
if len(name) != 0 { |
| 53 |
return name |
| 54 |
} |
| 55 |
|
| 56 |
return option.Field.Name |
| 57 |
} |
| 58 |
|
| 59 |
func (option *Option) call(value *string) error { |
| 60 |
var retval []reflect.Value |
| 61 |
|
| 62 |
if value == nil { |
| 63 |
retval = option.Value.Call(nil) |
| 64 |
} else { |
| 65 |
tp := option.Value.Type().In(0) |
| 66 |
|
| 67 |
val := reflect.New(tp) |
| 68 |
val = reflect.Indirect(val) |
| 69 |
|
| 70 |
if err := convert(*value, val, option.tag); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
|
| 74 |
retval = option.Value.Call([]reflect.Value{val}) |
| 75 |
} |
| 76 |
|
| 77 |
if len(retval) == 1 && retval[0].Type() == reflect.TypeOf((*error)(nil)).Elem() { |
| 78 |
if retval[0].Interface() == nil { |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
return retval[0].Interface().(error) |
| 83 |
} |
| 84 |
|
| 85 |
return nil |
| 86 |
} |