make quantity flag work with pflag package

pull/6/head
Daniel Smith 2015-01-14 14:48:52 -08:00
parent 30be0eeac7
commit 5767b41b28
2 changed files with 15 additions and 0 deletions

View File

@ -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 {

View File

@ -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)
}
}