github.com/vmware/govmomi@v0.43.0/govc/flags/int32.go (about)

     1  /*
     2  Copyright (c) 2016-2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flags
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"strconv"
    23  )
    24  
    25  // This flag type is internal to stdlib:
    26  // https://github.com/golang/go/blob/master/src/cmd/internal/obj/flag.go
    27  type int32Value int32
    28  
    29  func (i *int32Value) Set(s string) error {
    30  	v, err := strconv.ParseInt(s, 0, 32)
    31  	*i = int32Value(v)
    32  	return err
    33  }
    34  
    35  func (i *int32Value) Get() interface{} {
    36  	return int32(*i)
    37  }
    38  
    39  func (i *int32Value) String() string {
    40  	return fmt.Sprintf("%v", *i)
    41  }
    42  
    43  // NewInt32 behaves as flag.IntVar, but using an int32 type.
    44  func NewInt32(v *int32) flag.Value {
    45  	return (*int32Value)(v)
    46  }
    47  
    48  type int32ptrValue struct {
    49  	val **int32
    50  }
    51  
    52  func (i *int32ptrValue) Set(s string) error {
    53  	v, err := strconv.ParseInt(s, 0, 32)
    54  	*i.val = new(int32)
    55  	**i.val = int32(v)
    56  	return err
    57  }
    58  
    59  func (i *int32ptrValue) Get() interface{} {
    60  	if i.val == nil || *i.val == nil {
    61  		return nil
    62  	}
    63  	return *i.val
    64  }
    65  
    66  func (i *int32ptrValue) String() string {
    67  	return fmt.Sprintf("%v", i.Get())
    68  }
    69  
    70  func NewOptionalInt32(v **int32) flag.Value {
    71  	return &int32ptrValue{val: v}
    72  }