mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #12753 from eparis/rebase-pflag
Update spf13/pflag to fix StringSlice bug
This commit is contained in:
commit
ff1d3885b4
4
Godeps/Godeps.json
generated
4
Godeps/Godeps.json
generated
@ -488,8 +488,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/spf13/pflag",
|
"ImportPath": "github.com/spf13/pflag",
|
||||||
"Comment": "v0.0.1-73-g534019b",
|
"Comment": "v0.0.1-81-g4869ec2",
|
||||||
"Rev": "534019bcaea096fc0f0641afa2aed1e80cbb0ccc"
|
"Rev": "4869ec2ae0628354eaac5bf88fccf9a7265ae475"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/stretchr/objx",
|
"ImportPath": "github.com/stretchr/objx",
|
||||||
|
2
Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go
generated
vendored
2
Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go
generated
vendored
@ -51,7 +51,7 @@ func (v *triStateValue) String() string {
|
|||||||
return fmt.Sprintf("%v", bool(*v == triStateTrue))
|
return fmt.Sprintf("%v", bool(*v == triStateTrue))
|
||||||
}
|
}
|
||||||
|
|
||||||
// The type of the flag as requred by the pflag.Value interface
|
// The type of the flag as required by the pflag.Value interface
|
||||||
func (v *triStateValue) Type() string {
|
func (v *triStateValue) Type() string {
|
||||||
return "version"
|
return "version"
|
||||||
}
|
}
|
||||||
|
9
Godeps/_workspace/src/github.com/spf13/pflag/flag.go
generated
vendored
9
Godeps/_workspace/src/github.com/spf13/pflag/flag.go
generated
vendored
@ -330,6 +330,15 @@ func (f *FlagSet) SetAnnotation(name, key string, values []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FlagSet) Changed(name string) bool {
|
||||||
|
flag := f.Lookup(name)
|
||||||
|
// If a flag doesn't exist, it wasn't changed....
|
||||||
|
if flag == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return flag.Changed
|
||||||
|
}
|
||||||
|
|
||||||
// Set sets the value of the named command-line flag.
|
// Set sets the value of the named command-line flag.
|
||||||
func Set(name, value string) error {
|
func Set(name, value string) error {
|
||||||
return CommandLine.Set(name, value)
|
return CommandLine.Set(name, value)
|
||||||
|
28
Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go
generated
vendored
28
Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go
generated
vendored
@ -381,6 +381,34 @@ func TestFlagSetParse(t *testing.T) {
|
|||||||
testParse(NewFlagSet("test", ContinueOnError), t)
|
testParse(NewFlagSet("test", ContinueOnError), t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChangedHelper(t *testing.T) {
|
||||||
|
f := NewFlagSet("changedtest", ContinueOnError)
|
||||||
|
_ = f.Bool("changed", false, "changed bool")
|
||||||
|
_ = f.Bool("settrue", true, "true to true")
|
||||||
|
_ = f.Bool("setfalse", false, "false to false")
|
||||||
|
_ = f.Bool("unchanged", false, "unchanged bool")
|
||||||
|
|
||||||
|
args := []string{"--changed", "--settrue", "--setfalse=false"}
|
||||||
|
if err := f.Parse(args); err != nil {
|
||||||
|
t.Error("f.Parse() = false after Parse")
|
||||||
|
}
|
||||||
|
if !f.Changed("changed") {
|
||||||
|
t.Errorf("--changed wasn't changed!")
|
||||||
|
}
|
||||||
|
if !f.Changed("settrue") {
|
||||||
|
t.Errorf("--settrue wasn't changed!")
|
||||||
|
}
|
||||||
|
if !f.Changed("setfalse") {
|
||||||
|
t.Errorf("--setfalse wasn't changed!")
|
||||||
|
}
|
||||||
|
if f.Changed("unchanged") {
|
||||||
|
t.Errorf("--unchanged was changed!")
|
||||||
|
}
|
||||||
|
if f.Changed("invalid") {
|
||||||
|
t.Errorf("--invalid was changed!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func replaceSeparators(name string, from []string, to string) string {
|
func replaceSeparators(name string, from []string, to string) string {
|
||||||
result := name
|
result := name
|
||||||
for _, sep := range from {
|
for _, sep := range from {
|
||||||
|
22
Godeps/_workspace/src/github.com/spf13/pflag/int_slice.go
generated
vendored
22
Godeps/_workspace/src/github.com/spf13/pflag/int_slice.go
generated
vendored
@ -7,11 +7,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// -- intSlice Value
|
// -- intSlice Value
|
||||||
type intSliceValue []int
|
type intSliceValue struct {
|
||||||
|
value *[]int
|
||||||
|
changed bool
|
||||||
|
}
|
||||||
|
|
||||||
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
|
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
|
||||||
*p = val
|
isv := new(intSliceValue)
|
||||||
return (*intSliceValue)(p)
|
isv.value = p
|
||||||
|
*isv.value = val
|
||||||
|
return isv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *intSliceValue) Set(val string) error {
|
func (s *intSliceValue) Set(val string) error {
|
||||||
@ -25,7 +30,12 @@ func (s *intSliceValue) Set(val string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
*s = append(*s, out...)
|
if !s.changed {
|
||||||
|
*s.value = out
|
||||||
|
} else {
|
||||||
|
*s.value = append(*s.value, out...)
|
||||||
|
}
|
||||||
|
s.changed = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,8 +44,8 @@ func (s *intSliceValue) Type() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *intSliceValue) String() string {
|
func (s *intSliceValue) String() string {
|
||||||
out := make([]string, len(*s))
|
out := make([]string, len(*s.value))
|
||||||
for i, d := range *s {
|
for i, d := range *s.value {
|
||||||
out[i] = fmt.Sprintf("%d", d)
|
out[i] = fmt.Sprintf("%d", d)
|
||||||
}
|
}
|
||||||
return "[" + strings.Join(out, ",") + "]"
|
return "[" + strings.Join(out, ",") + "]"
|
||||||
|
80
Godeps/_workspace/src/github.com/spf13/pflag/int_slice_test.go
generated
vendored
80
Godeps/_workspace/src/github.com/spf13/pflag/int_slice_test.go
generated
vendored
@ -17,6 +17,12 @@ func setUpISFlagSet(isp *[]int) *FlagSet {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setUpISFlagSetWithDefault(isp *[]int) *FlagSet {
|
||||||
|
f := NewFlagSet("test", ContinueOnError)
|
||||||
|
f.IntSliceVar(isp, "is", []int{0, 1}, "Command seperated list!")
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
func TestEmptyIS(t *testing.T) {
|
func TestEmptyIS(t *testing.T) {
|
||||||
var is []int
|
var is []int
|
||||||
f := setUpISFlagSet(&is)
|
f := setUpISFlagSet(&is)
|
||||||
@ -27,7 +33,7 @@ func TestEmptyIS(t *testing.T) {
|
|||||||
|
|
||||||
getIS, err := f.GetIntSlice("is")
|
getIS, err := f.GetIntSlice("is")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("got an error from GetStringSlice():", err)
|
t.Fatal("got an error from GetIntSlice():", err)
|
||||||
}
|
}
|
||||||
if len(getIS) != 0 {
|
if len(getIS) != 0 {
|
||||||
t.Fatalf("got is %v with len=%d but expected length=0", getIS, len(getIS))
|
t.Fatalf("got is %v with len=%d but expected length=0", getIS, len(getIS))
|
||||||
@ -65,6 +71,76 @@ func TestIS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestISDefault(t *testing.T) {
|
||||||
|
var is []int
|
||||||
|
f := setUpISFlagSetWithDefault(&is)
|
||||||
|
|
||||||
|
vals := []string{"0", "1"}
|
||||||
|
|
||||||
|
err := f.Parse([]string{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error; got", err)
|
||||||
|
}
|
||||||
|
for i, v := range is {
|
||||||
|
d, err := strconv.Atoi(vals[i])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("got error: %v", err)
|
||||||
|
}
|
||||||
|
if d != v {
|
||||||
|
t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getIS, err := f.GetIntSlice("is")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("got an error from GetIntSlice():", err)
|
||||||
|
}
|
||||||
|
for i, v := range getIS {
|
||||||
|
d, err := strconv.Atoi(vals[i])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("got an error from GetIntSlice():", err)
|
||||||
|
}
|
||||||
|
if d != v {
|
||||||
|
t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestISWithDefault(t *testing.T) {
|
||||||
|
var is []int
|
||||||
|
f := setUpISFlagSetWithDefault(&is)
|
||||||
|
|
||||||
|
vals := []string{"1", "2"}
|
||||||
|
arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
|
||||||
|
err := f.Parse([]string{arg})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error; got", err)
|
||||||
|
}
|
||||||
|
for i, v := range is {
|
||||||
|
d, err := strconv.Atoi(vals[i])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("got error: %v", err)
|
||||||
|
}
|
||||||
|
if d != v {
|
||||||
|
t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getIS, err := f.GetIntSlice("is")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("got an error from GetIntSlice():", err)
|
||||||
|
}
|
||||||
|
for i, v := range getIS {
|
||||||
|
d, err := strconv.Atoi(vals[i])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("got error: %v", err)
|
||||||
|
}
|
||||||
|
if d != v {
|
||||||
|
t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestISCalledTwice(t *testing.T) {
|
func TestISCalledTwice(t *testing.T) {
|
||||||
var is []int
|
var is []int
|
||||||
f := setUpISFlagSet(&is)
|
f := setUpISFlagSet(&is)
|
||||||
@ -80,7 +156,7 @@ func TestISCalledTwice(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, v := range is {
|
for i, v := range is {
|
||||||
if expected[i] != v {
|
if expected[i] != v {
|
||||||
t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v)
|
t.Fatalf("expected is[%d] to be %s but got: %s", i, expected[i], v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
Godeps/_workspace/src/github.com/spf13/pflag/string_slice.go
generated
vendored
24
Godeps/_workspace/src/github.com/spf13/pflag/string_slice.go
generated
vendored
@ -1,27 +1,41 @@
|
|||||||
package pflag
|
package pflag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ = fmt.Fprint
|
||||||
|
|
||||||
// -- stringSlice Value
|
// -- stringSlice Value
|
||||||
type stringSliceValue []string
|
type stringSliceValue struct {
|
||||||
|
value *[]string
|
||||||
|
changed bool
|
||||||
|
}
|
||||||
|
|
||||||
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
|
func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
|
||||||
*p = val
|
ssv := new(stringSliceValue)
|
||||||
return (*stringSliceValue)(p)
|
ssv.value = p
|
||||||
|
*ssv.value = val
|
||||||
|
return ssv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stringSliceValue) Set(val string) error {
|
func (s *stringSliceValue) Set(val string) error {
|
||||||
v := strings.Split(val, ",")
|
v := strings.Split(val, ",")
|
||||||
*s = append(*s, v...)
|
if !s.changed {
|
||||||
|
*s.value = v
|
||||||
|
} else {
|
||||||
|
*s.value = append(*s.value, v...)
|
||||||
|
}
|
||||||
|
s.changed = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stringSliceValue) Type() string {
|
func (s *stringSliceValue) Type() string {
|
||||||
return "stringSlice"
|
return "stringSlice"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stringSliceValue) String() string { return "[" + strings.Join(*s, ",") + "]" }
|
func (s *stringSliceValue) String() string { return "[" + strings.Join(*s.value, ",") + "]" }
|
||||||
|
|
||||||
func stringSliceConv(sval string) (interface{}, error) {
|
func stringSliceConv(sval string) (interface{}, error) {
|
||||||
sval = strings.Trim(sval, "[]")
|
sval = strings.Trim(sval, "[]")
|
||||||
|
60
Godeps/_workspace/src/github.com/spf13/pflag/string_slice_test.go
generated
vendored
60
Godeps/_workspace/src/github.com/spf13/pflag/string_slice_test.go
generated
vendored
@ -16,6 +16,12 @@ func setUpSSFlagSet(ssp *[]string) *FlagSet {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setUpSSFlagSetWithDefault(ssp *[]string) *FlagSet {
|
||||||
|
f := NewFlagSet("test", ContinueOnError)
|
||||||
|
f.StringSliceVar(ssp, "ss", []string{"default", "values"}, "Command seperated list!")
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
func TestEmptySS(t *testing.T) {
|
func TestEmptySS(t *testing.T) {
|
||||||
var ss []string
|
var ss []string
|
||||||
f := setUpSSFlagSet(&ss)
|
f := setUpSSFlagSet(&ss)
|
||||||
@ -60,6 +66,60 @@ func TestSS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSSDefault(t *testing.T) {
|
||||||
|
var ss []string
|
||||||
|
f := setUpSSFlagSetWithDefault(&ss)
|
||||||
|
|
||||||
|
vals := []string{"default", "values"}
|
||||||
|
|
||||||
|
err := f.Parse([]string{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error; got", err)
|
||||||
|
}
|
||||||
|
for i, v := range ss {
|
||||||
|
if vals[i] != v {
|
||||||
|
t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getSS, err := f.GetStringSlice("ss")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("got an error from GetStringSlice():", err)
|
||||||
|
}
|
||||||
|
for i, v := range getSS {
|
||||||
|
if vals[i] != v {
|
||||||
|
t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSSWithDefault(t *testing.T) {
|
||||||
|
var ss []string
|
||||||
|
f := setUpSSFlagSetWithDefault(&ss)
|
||||||
|
|
||||||
|
vals := []string{"one", "two", "4", "3"}
|
||||||
|
arg := fmt.Sprintf("--ss=%s", strings.Join(vals, ","))
|
||||||
|
err := f.Parse([]string{arg})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("expected no error; got", err)
|
||||||
|
}
|
||||||
|
for i, v := range ss {
|
||||||
|
if vals[i] != v {
|
||||||
|
t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getSS, err := f.GetStringSlice("ss")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("got an error from GetStringSlice():", err)
|
||||||
|
}
|
||||||
|
for i, v := range getSS {
|
||||||
|
if vals[i] != v {
|
||||||
|
t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSSCalledTwice(t *testing.T) {
|
func TestSSCalledTwice(t *testing.T) {
|
||||||
var ss []string
|
var ss []string
|
||||||
f := setUpSSFlagSet(&ss)
|
f := setUpSSFlagSet(&ss)
|
||||||
|
Loading…
Reference in New Issue
Block a user