From 5767b41b28cb110fb0a171d7c0987ecb9f0e7ce2 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Wed, 14 Jan 2015 14:48:52 -0800 Subject: [PATCH] make quantity flag work with pflag package --- pkg/api/resource/quantity.go | 7 +++++++ pkg/api/resource/quantity_test.go | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/api/resource/quantity.go b/pkg/api/resource/quantity.go index 71c89fe12a..03558aeaa8 100644 --- a/pkg/api/resource/quantity.go +++ b/pkg/api/resource/quantity.go @@ -386,6 +386,7 @@ type qFlag struct { dest *Quantity } +// Sets the value of the internal Quantity. (used by flag & pflag) func (qf qFlag) Set(val string) error { q, err := ParseQuantity(val) if err != nil { @@ -396,10 +397,16 @@ func (qf qFlag) Set(val string) error { return nil } +// Converts the value of the internal Quantity to a string. (used by flag & pflag) func (qf qFlag) String() string { return qf.dest.String() } +// States the type of flag this is (Quantity). (used by pflag) +func (qf qFlag) Type() string { + return "quantity" +} + // QuantityFlag is a helper that makes a quantity flag (using standard flag package). // Will panic if defaultValue is not a valid quantity. func QuantityFlag(flagName, defaultValue, description string) *Quantity { diff --git a/pkg/api/resource/quantity_test.go b/pkg/api/resource/quantity_test.go index 9b0ab7281b..c550acfc60 100644 --- a/pkg/api/resource/quantity_test.go +++ b/pkg/api/resource/quantity_test.go @@ -22,6 +22,7 @@ import ( "testing" fuzz "github.com/google/gofuzz" + "github.com/spf13/pflag" "speter.net/go/exp/math/dec/inf" ) @@ -487,3 +488,10 @@ func TestQFlagSet(t *testing.T) { t.Errorf("Unexpected result %v != %v", e, a) } } + +func TestQFlagIsPFlag(t *testing.T) { + var pfv pflag.Value = qFlag{} + if e, a := "quantity", pfv.Type(); e != a { + t.Errorf("Unexpected result %v != %v", e, a) + } +}