| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 1 | // Copyright 2022 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package flags |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "strconv" |
| 20 | ) |
| 21 | |
| 22 | type uint32Value uint32 |
| 23 | |
| 24 | // NewUint32Value creates an uint32 instance with the provided value. |
| 25 | func NewUint32Value(v uint32) *uint32Value { |
| 26 | val := new(uint32Value) |
| 27 | *val = uint32Value(v) |
| 28 | return val |
| 29 | } |
| 30 | |
| 31 | // Set parses a command line uint32 value. |
| 32 | // Implements "flag.Value" interface. |
| 33 | func (i *uint32Value) Set(s string) error { |
| 34 | v, err := strconv.ParseUint(s, 0, 32) |
| 35 | *i = uint32Value(v) |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | func (i *uint32Value) String() string { return strconv.FormatUint(uint64(*i), 10) } |
| 40 | |
| 41 | // Uint32FromFlag return the uint32 value of a flag with the given name |
| 42 | func Uint32FromFlag(fs *flag.FlagSet, name string) uint32 { |
| 43 | val := *fs.Lookup(name).Value.(*uint32Value) |
| 44 | return uint32(val) |
| 45 | } |