github.com/vmware/govmomi@v0.51.0/cli/flags/optional_bool.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  type optionalBool struct {
    14  	val **bool
    15  }
    16  
    17  func (b *optionalBool) Set(s string) error {
    18  	v, err := strconv.ParseBool(s)
    19  	*b.val = &v
    20  	return err
    21  }
    22  
    23  func (b *optionalBool) Get() any {
    24  	if *b.val == nil {
    25  		return nil
    26  	}
    27  	return **b.val
    28  }
    29  
    30  func (b *optionalBool) String() string {
    31  	if b.val == nil || *b.val == nil {
    32  		return "<nil>"
    33  	}
    34  	return fmt.Sprintf("%v", **b.val)
    35  }
    36  
    37  func (b *optionalBool) IsBoolFlag() bool { return true }
    38  
    39  // NewOptionalBool returns a flag.Value implementation where there is no default value.
    40  // This avoids sending a default value over the wire as using flag.BoolVar() would.
    41  func NewOptionalBool(v **bool) flag.Value {
    42  	return &optionalBool{v}
    43  }