hub.fastgit.org/hashicorp/consul.git@v1.4.5/command/flags/flag_map_value.go (about)

     1  package flags
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  // Ensure implements
    10  var _ flag.Value = (*FlagMapValue)(nil)
    11  
    12  // FlagMapValue is a flag implementation used to provide key=value semantics
    13  // multiple times.
    14  type FlagMapValue map[string]string
    15  
    16  func (h *FlagMapValue) String() string {
    17  	return fmt.Sprintf("%v", *h)
    18  }
    19  
    20  func (h *FlagMapValue) Set(value string) error {
    21  	idx := strings.Index(value, "=")
    22  	if idx == -1 {
    23  		return fmt.Errorf("Missing \"=\" value in argument: %s", value)
    24  	}
    25  
    26  	key, value := value[0:idx], value[idx+1:]
    27  
    28  	if *h == nil {
    29  		*h = make(map[string]string)
    30  	}
    31  
    32  	headers := *h
    33  	headers[key] = value
    34  	*h = headers
    35  
    36  	return nil
    37  }