github.com/vmware/govmomi@v0.51.0/cli/flags/int64.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 int64Value int64
    16  
    17  func (i *int64Value) Set(s string) error {
    18  	v, err := strconv.ParseInt(s, 0, 64)
    19  	*i = int64Value(v)
    20  	return err
    21  }
    22  
    23  func (i *int64Value) Get() any {
    24  	return int64(*i)
    25  }
    26  
    27  func (i *int64Value) String() string {
    28  	return fmt.Sprintf("%v", *i)
    29  }
    30  
    31  // NewInt64 behaves as flag.IntVar, but using an int64 type.
    32  func NewInt64(v *int64) flag.Value {
    33  	return (*int64Value)(v)
    34  }
    35  
    36  type int64ptrValue struct {
    37  	val **int64
    38  }
    39  
    40  func (i *int64ptrValue) Set(s string) error {
    41  	v, err := strconv.ParseInt(s, 0, 64)
    42  	*i.val = new(int64)
    43  	**i.val = int64(v)
    44  	return err
    45  }
    46  
    47  func (i *int64ptrValue) Get() any {
    48  	if i.val == nil || *i.val == nil {
    49  		return nil
    50  	}
    51  	return **i.val
    52  }
    53  
    54  func (i *int64ptrValue) String() string {
    55  	return fmt.Sprintf("%v", i.Get())
    56  }
    57  
    58  func NewOptionalInt64(v **int64) flag.Value {
    59  	return &int64ptrValue{val: v}
    60  }