github.com/knieriem/gointernal@v0.2.0-pre2/cmd/go/base/flag.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package base
     6  
     7  import (
     8  	"github.com/knieriem/gointernal/cmd/quoted"
     9  )
    10  
    11  // A StringsFlag is a command-line flag that interprets its argument
    12  // as a space-separated list of possibly-quoted strings.
    13  type StringsFlag []string
    14  
    15  func (v *StringsFlag) Set(s string) error {
    16  	var err error
    17  	*v, err = quoted.Split(s)
    18  	if *v == nil {
    19  		*v = []string{}
    20  	}
    21  	return err
    22  }
    23  
    24  func (v *StringsFlag) String() string {
    25  	return "<StringsFlag>"
    26  }
    27  
    28  // explicitStringFlag is like a regular string flag, but it also tracks whether
    29  // the string was set explicitly to a non-empty value.
    30  type explicitStringFlag struct {
    31  	value    *string
    32  	explicit *bool
    33  }
    34  
    35  func (f explicitStringFlag) String() string {
    36  	if f.value == nil {
    37  		return ""
    38  	}
    39  	return *f.value
    40  }
    41  
    42  func (f explicitStringFlag) Set(v string) error {
    43  	*f.value = v
    44  	if v != "" {
    45  		*f.explicit = true
    46  	}
    47  	return nil
    48  }