TypeName returns the type of the flag.
()
| 91 | |
| 92 | // TypeName returns the type of the flag. |
| 93 | func (f *FlagBase[T, C, V]) TypeName() string { |
| 94 | ty := reflect.TypeOf(f.Value) |
| 95 | if ty == nil { |
| 96 | return "" |
| 97 | } |
| 98 | // convert the typename to generic type |
| 99 | convertToGenericType := func(name string) string { |
| 100 | prefixMap := map[string]string{ |
| 101 | "float": "float", |
| 102 | "int": "int", |
| 103 | "uint": "uint", |
| 104 | } |
| 105 | for prefix, genericType := range prefixMap { |
| 106 | if strings.HasPrefix(name, prefix) { |
| 107 | return genericType |
| 108 | } |
| 109 | } |
| 110 | return strings.ToLower(name) |
| 111 | } |
| 112 | |
| 113 | switch ty.Kind() { |
| 114 | // if it is a Slice, then return the slice's inner type. Will nested slices be used in the future? |
| 115 | case reflect.Slice: |
| 116 | elemType := ty.Elem() |
| 117 | return convertToGenericType(elemType.Name()) |
| 118 | // if it is a Map, then return the map's key and value types. |
| 119 | case reflect.Map: |
| 120 | keyType := ty.Key() |
| 121 | valueType := ty.Elem() |
| 122 | return fmt.Sprintf("%s=%s", convertToGenericType(keyType.Name()), convertToGenericType(valueType.Name())) |
| 123 | default: |
| 124 | return convertToGenericType(ty.Name()) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // PostParse populates the flag given the flag set and environment |
| 129 | func (f *FlagBase[T, C, V]) PostParse() error { |