update string_slice_flag add nil judgment

This commit is contained in:
zhiming 2021-06-16 21:25:47 +08:00
parent c9414c2bba
commit abf6584322

View File

@ -18,6 +18,7 @@ package flag
import (
goflag "flag"
"fmt"
"strings"
"github.com/spf13/pflag"
@ -45,7 +46,10 @@ func (s *StringSlice) String() string {
}
func (s *StringSlice) Set(val string) error {
if s.value == nil || !s.changed {
if s.value == nil {
return fmt.Errorf("no target (nil pointer to []string)")
}
if *s.value == nil || !s.changed {
*s.value = make([]string, 0)
}
*s.value = append(*s.value, val)