github.com/vmware/govmomi@v0.43.0/govc/flags/int64.go (about) 1 /* 2 Copyright (c) 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 int64Value int64 28 29 func (i *int64Value) Set(s string) error { 30 v, err := strconv.ParseInt(s, 0, 64) 31 *i = int64Value(v) 32 return err 33 } 34 35 func (i *int64Value) Get() interface{} { 36 return int64(*i) 37 } 38 39 func (i *int64Value) String() string { 40 return fmt.Sprintf("%v", *i) 41 } 42 43 // NewInt64 behaves as flag.IntVar, but using an int64 type. 44 func NewInt64(v *int64) flag.Value { 45 return (*int64Value)(v) 46 } 47 48 type int64ptrValue struct { 49 val **int64 50 } 51 52 func (i *int64ptrValue) Set(s string) error { 53 v, err := strconv.ParseInt(s, 0, 64) 54 *i.val = new(int64) 55 **i.val = int64(v) 56 return err 57 } 58 59 func (i *int64ptrValue) Get() interface{} { 60 if i.val == nil || *i.val == nil { 61 return nil 62 } 63 return **i.val 64 } 65 66 func (i *int64ptrValue) String() string { 67 return fmt.Sprintf("%v", i.Get()) 68 } 69 70 func NewOptionalInt64(v **int64) flag.Value { 71 return &int64ptrValue{val: v} 72 }