blob: 496730a4549905603e99d47cbe12ef0df2b10d13 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// 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
15package flags
16
17import (
18 "flag"
19 "strconv"
20)
21
22type uint32Value uint32
23
24// NewUint32Value creates an uint32 instance with the provided value.
25func 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.
33func (i *uint32Value) Set(s string) error {
34 v, err := strconv.ParseUint(s, 0, 32)
35 *i = uint32Value(v)
36 return err
37}
38
39func (i *uint32Value) String() string { return strconv.FormatUint(uint64(*i), 10) }
40
41// Uint32FromFlag return the uint32 value of a flag with the given name
42func Uint32FromFlag(fs *flag.FlagSet, name string) uint32 {
43 val := *fs.Lookup(name).Value.(*uint32Value)
44 return uint32(val)
45}