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

     1  /*
     2  Copyright (c) 2023-2023 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  )
    22  
    23  type optionalString struct {
    24  	val **string
    25  }
    26  
    27  func (s *optionalString) Set(input string) error {
    28  	*s.val = &input
    29  	return nil
    30  }
    31  
    32  func (s *optionalString) Get() interface{} {
    33  	if *s.val == nil {
    34  		return nil
    35  	}
    36  	return **s.val
    37  }
    38  
    39  func (s *optionalString) String() string {
    40  	if s.val == nil || *s.val == nil {
    41  		return "<nil>"
    42  	}
    43  	return **s.val
    44  }
    45  
    46  // NewOptionalString returns a flag.Value implementation where there is no default value.
    47  // This avoids sending a default value over the wire as using flag.StringVar() would.
    48  func NewOptionalString(v **string) flag.Value {
    49  	return &optionalString{v}
    50  }