From 374f9978d6b452920eedabdd293deba3cf81ff47 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Thu, 22 Jan 2015 14:22:12 -0800 Subject: [PATCH] Update github.com/spf13/pflag to include spf13/pflag#3 We need support for IsBoolFlag() in order to implement `--version` (without an explicit `--version=true`) and also `--version=raw`. Updated using `godep update github.com/spf13/pflag`. Tested: $ _output/local/bin/linux/amd64/kubelet --version Kubernetes v0.8.0-590-gc61720437ec181 $ _output/local/bin/linux/amd64/kubelet --version=raw version.Info{Major:"0", Minor:"8+", GitVersion:"v0.8.0-590-gc61720437ec181", GitCommit:"c61720437ec181ae3fbbfb9d6df5efdf3e3b6c2e", GitTreeState:"clean"} --- Godeps/Godeps.json | 2 +- .../src/github.com/spf13/pflag/bool.go | 9 + .../src/github.com/spf13/pflag/bool_test.go | 163 ++++++++++++++++++ .../github.com/spf13/pflag/example_test.go | 4 + .../src/github.com/spf13/pflag/flag.go | 4 +- .../src/github.com/spf13/pflag/flag_test.go | 4 + 6 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 95994fa94e2..97505eb9dc5 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -168,7 +168,7 @@ }, { "ImportPath": "github.com/spf13/pflag", - "Rev": "463bdc838f2b35e9307e91d480878bda5fff7232" + "Rev": "f82776d6cc998e3c026baef7b24409ff49fe5c8d" }, { "ImportPath": "github.com/stretchr/objx", diff --git a/Godeps/_workspace/src/github.com/spf13/pflag/bool.go b/Godeps/_workspace/src/github.com/spf13/pflag/bool.go index 010576c3968..70e2e0a6b96 100644 --- a/Godeps/_workspace/src/github.com/spf13/pflag/bool.go +++ b/Godeps/_workspace/src/github.com/spf13/pflag/bool.go @@ -5,6 +5,13 @@ import ( "strconv" ) +// optional interface to indicate boolean flags that can be +// supplied without "=value" text +type boolFlag interface { + Value + IsBoolFlag() bool +} + // -- bool Value type boolValue bool @@ -25,6 +32,8 @@ func (b *boolValue) Type() string { func (b *boolValue) String() string { return fmt.Sprintf("%v", *b) } +func (b *boolValue) IsBoolFlag() bool { return true } + // BoolVar defines a bool flag with specified name, default value, and usage string. // The argument p points to a bool variable in which to store the value of the flag. func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) { diff --git a/Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go b/Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go new file mode 100644 index 00000000000..72a12beca98 --- /dev/null +++ b/Godeps/_workspace/src/github.com/spf13/pflag/bool_test.go @@ -0,0 +1,163 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package pflag_test + +import ( + "fmt" + "strconv" + "testing" + + . "github.com/ogier/pflag" +) + +// This value can be a boolean ("true", "false") or "maybe" +type triStateValue int + +const ( + triStateFalse triStateValue = 0 + triStateTrue triStateValue = 1 + triStateMaybe triStateValue = 2 +) + +const strTriStateMaybe = "maybe" + +func (v *triStateValue) IsBoolFlag() bool { + return true +} + +func (v *triStateValue) Get() interface{} { + return triStateValue(*v) +} + +func (v *triStateValue) Set(s string) error { + if s == strTriStateMaybe { + *v = triStateMaybe + return nil + } + boolVal, err := strconv.ParseBool(s) + if boolVal { + *v = triStateTrue + } else { + *v = triStateFalse + } + return err +} + +func (v *triStateValue) String() string { + if *v == triStateMaybe { + return strTriStateMaybe + } + return fmt.Sprintf("%v", bool(*v == triStateTrue)) +} + +// The type of the flag as requred by the pflag.Value interface +func (v *triStateValue) Type() string { + return "version" +} + +func setUpFlagSet(tristate *triStateValue) *FlagSet { + f := NewFlagSet("test", ContinueOnError) + *tristate = triStateFalse + f.VarP(tristate, "tristate", "t", "tristate value (true, maybe or false)") + return f +} + +func TestExplicitTrue(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"--tristate=true"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateTrue { + t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") + } +} + +func TestImplicitTrue(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"--tristate"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateTrue { + t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") + } +} + +func TestShortFlag(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"-t"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateTrue { + t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") + } +} + +func TestShortFlagExtraArgument(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + // The"maybe"turns into an arg, since short boolean options will only do true/false + err := f.Parse([]string{"-t", "maybe"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateTrue { + t.Fatal("expected", triStateTrue, "(triStateTrue) but got", tristate, "instead") + } + args := f.Args() + if len(args) != 1 || args[0] != "maybe" { + t.Fatal("expected an extra 'maybe' argument to stick around") + } +} + +func TestExplicitMaybe(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"--tristate=maybe"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateMaybe { + t.Fatal("expected", triStateMaybe, "(triStateMaybe) but got", tristate, "instead") + } +} + +func TestExplicitFalse(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"--tristate=false"}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateFalse { + t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead") + } +} + +func TestImplicitFalse(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{}) + if err != nil { + t.Fatal("expected no error; got", err) + } + if tristate != triStateFalse { + t.Fatal("expected", triStateFalse, "(triStateFalse) but got", tristate, "instead") + } +} + +func TestInvalidValue(t *testing.T) { + var tristate triStateValue + f := setUpFlagSet(&tristate) + err := f.Parse([]string{"--tristate=invalid"}) + if err == nil { + t.Fatal("expected an error but did not get any, tristate has value", tristate) + } +} diff --git a/Godeps/_workspace/src/github.com/spf13/pflag/example_test.go b/Godeps/_workspace/src/github.com/spf13/pflag/example_test.go index 03ebeaad459..6aaed3c8082 100644 --- a/Godeps/_workspace/src/github.com/spf13/pflag/example_test.go +++ b/Godeps/_workspace/src/github.com/spf13/pflag/example_test.go @@ -29,6 +29,10 @@ func (i *interval) String() string { return fmt.Sprint(*i) } +func (i *interval) Type() string { + return "interval" +} + // Set is the method to set the flag value, part of the flag.Value interface. // Set's argument is a string to be parsed to set the flag. // It's a comma-separated list, so we split it. diff --git a/Godeps/_workspace/src/github.com/spf13/pflag/flag.go b/Godeps/_workspace/src/github.com/spf13/pflag/flag.go index 47761a0e808..2e4cac08493 100644 --- a/Godeps/_workspace/src/github.com/spf13/pflag/flag.go +++ b/Godeps/_workspace/src/github.com/spf13/pflag/flag.go @@ -466,7 +466,7 @@ func (f *FlagSet) parseLongArg(s string, args []string) (a []string, err error) return } if len(split) == 1 { - if _, ok := flag.Value.(*boolValue); !ok { + if bv, ok := flag.Value.(boolFlag); !ok || !bv.IsBoolFlag() { err = f.failf("flag needs an argument: %s", s) return } @@ -500,7 +500,7 @@ func (f *FlagSet) parseShortArg(s string, args []string) (a []string, err error) } } if alreadythere { - if _, ok := flag.Value.(*boolValue); ok { + if bv, ok := flag.Value.(boolFlag); ok && bv.IsBoolFlag() { f.setFlag(flag, "true", s) continue } diff --git a/Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go b/Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go index 4d95b1596bc..47865bd96ba 100644 --- a/Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go +++ b/Godeps/_workspace/src/github.com/spf13/pflag/flag_test.go @@ -245,6 +245,10 @@ func (f *flagVar) Set(value string) error { return nil } +func (f *flagVar) Type() string { + return "flagVar" +} + func TestUserDefined(t *testing.T) { var flags FlagSet flags.Init("test", ContinueOnError)