github.com/vmware/govmomi@v0.51.0/cli/flags/int32.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package flags
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"strconv"
    11  )
    12  
    13  // This flag type is internal to stdlib:
    14  // https://github.com/golang/go/blob/master/src/cmd/internal/obj/flag.go
    15  type int32Value int32
    16  
    17  func (i *int32Value) Set(s string) error {
    18  	v, err := strconv.ParseInt(s, 0, 32)
    19  	*i = int32Value(v)
    20  	return err
    21  }
    22  
    23  func (i *int32Value) Get() any {
    24  	return int32(*i)
    25  }
    26  
    27  func (i *int32Value) String() string {
    28  	return fmt.Sprintf("%v", *i)
    29  }
    30  
    31  // NewInt32 behaves as flag.IntVar, but using an int32 type.
    32  func NewInt32(v *int32) flag.Value {
    33  	return (*int32Value)(v)
    34  }
    35  
    36  type int32ptrValue struct {
    37  	val **int32
    38  }
    39  
    40  func (i *int32ptrValue) Set(s string) error {
    41  	v, err := strconv.ParseInt(s, 0, 32)
    42  	*i.val = new(int32)
    43  	**i.val = int32(v)
    44  	return err
    45  }
    46  
    47  func (i *int32ptrValue) Get() any {
    48  	if i.val == nil || *i.val == nil {
    49  		return nil
    50  	}
    51  	return *i.val
    52  }
    53  
    54  func (i *int32ptrValue) String() string {
    55  	return fmt.Sprintf("%v", i.Get())
    56  }
    57  
    58  func NewOptionalInt32(v **int32) flag.Value {
    59  	return &int32ptrValue{val: v}
    60  }