github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/command/flag/network_protocol.go (about)

     1  package flag
     2  
     3  import (
     4  	"strings"
     5  
     6  	flags "github.com/jessevdk/go-flags"
     7  )
     8  
     9  type NetworkProtocol struct {
    10  	Protocol string
    11  }
    12  
    13  func (NetworkProtocol) Complete(prefix string) []flags.Completion {
    14  	return completions([]string{"tcp", "udp"}, prefix, false)
    15  }
    16  
    17  func (h *NetworkProtocol) UnmarshalFlag(val string) error {
    18  	valLower := strings.ToLower(val)
    19  	switch valLower {
    20  	case "tcp", "udp":
    21  		h.Protocol = valLower
    22  	default:
    23  		return &flags.Error{
    24  			Type:    flags.ErrRequired,
    25  			Message: `PROTOCOL must be "tcp" or "udp"`,
    26  		}
    27  	}
    28  	return nil
    29  }