Commit 5767b41b authored by Daniel Smith's avatar Daniel Smith

make quantity flag work with pflag package

parent 30be0eea
......@@ -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 {
......
......@@ -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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment