github.com/vmware/govmomi@v0.37.1/govc/flags/optional_bool.go (about)

     1  /*
     2  Copyright (c) 2015 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  type optionalBool struct {
    26  	val **bool
    27  }
    28  
    29  func (b *optionalBool) Set(s string) error {
    30  	v, err := strconv.ParseBool(s)
    31  	*b.val = &v
    32  	return err
    33  }
    34  
    35  func (b *optionalBool) Get() interface{} {
    36  	if *b.val == nil {
    37  		return nil
    38  	}
    39  	return **b.val
    40  }
    41  
    42  func (b *optionalBool) String() string {
    43  	if b.val == nil || *b.val == nil {
    44  		return "<nil>"
    45  	}
    46  	return fmt.Sprintf("%v", **b.val)
    47  }
    48  
    49  func (b *optionalBool) IsBoolFlag() bool { return true }
    50  
    51  // NewOptionalBool returns a flag.Value implementation where there is no default value.
    52  // This avoids sending a default value over the wire as using flag.BoolVar() would.
    53  func NewOptionalBool(v **bool) flag.Value {
    54  	return &optionalBool{v}
    55  }