Set applies given value from string
(_ string, val string)
| 178 | |
| 179 | // Set applies given value from string |
| 180 | func (f *FlagBase[T, C, V]) Set(_ string, val string) error { |
| 181 | tracef("apply (flag=%[1]q)", f.Name) |
| 182 | |
| 183 | // TODO move this phase into a separate flag initialization function |
| 184 | // if flag has been applied previously then it would have already been set |
| 185 | // from env or file. So no need to apply the env set again. However |
| 186 | // lots of units tests prior to persistent flags assumed that the |
| 187 | // flag can be applied to different flag sets multiple times while still |
| 188 | // keeping the env set. |
| 189 | if !f.applied { |
| 190 | if err := f.PreParse(); err != nil { |
| 191 | return err |
| 192 | } |
| 193 | f.applied = true |
| 194 | } |
| 195 | |
| 196 | if f.count == 1 && f.OnlyOnce { |
| 197 | return fmt.Errorf("can't duplicate this flag") |
| 198 | } |
| 199 | |
| 200 | f.count++ |
| 201 | if err := f.value.Set(val); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | f.hasBeenSet = true |
| 205 | if f.Validator != nil { |
| 206 | if err := f.Validator(f.value.Get().(T)); err != nil { |
| 207 | return err |
| 208 | } |
| 209 | } |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | func (f *FlagBase[T, C, V]) Get() any { |
| 214 | if f.value != nil { |