Commit 379150c2 authored by Vish Subramanian's avatar Vish Subramanian Committed by Rob Pike

Add flags of type float to the flag package.

R=r, rsc
http://go/go-review/1026011
parent 1ac60ddd
...@@ -166,6 +166,42 @@ func (s *stringValue) set(val string) bool { ...@@ -166,6 +166,42 @@ func (s *stringValue) set(val string) bool {
func (s *stringValue) String() string { return fmt.Sprintf("%s", *s.p) } func (s *stringValue) String() string { return fmt.Sprintf("%s", *s.p) }
// -- Float Value
type floatValue struct {
p *float;
}
func newFloatValue(val float, p *float) *floatValue {
*p = val;
return &floatValue{p};
}
func (f *floatValue) set(s string) bool {
v, err := strconv.Atof(s);
*f.p = v;
return err == nil;
}
func (f *floatValue) String() string { return fmt.Sprintf("%v", *f.p) }
// -- Float64 Value
type float64Value struct {
p *float64;
}
func newFloat64Value(val float64, p *float64) *float64Value {
*p = val;
return &float64Value{p};
}
func (f *float64Value) set(s string) bool {
v, err := strconv.Atof64(s);
*f.p = v;
return err == nil;
}
func (f *float64Value) String() string { return fmt.Sprintf("%v", *f.p) }
// FlagValue is the interface to the dynamic value stored in a flag. // FlagValue is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.) // (The default value is represented as a string.)
type FlagValue interface { type FlagValue interface {
...@@ -359,6 +395,35 @@ func String(name, value string, usage string) *string { ...@@ -359,6 +395,35 @@ func String(name, value string, usage string) *string {
return p; return p;
} }
// FloatVar defines a float flag with specified name, default value, and usage string.
// The argument p points to a float variable in which to store the value of the flag.
func FloatVar(p *float, name string, value float, usage string) {
add(name, newFloatValue(value, p), usage);
}
// Float defines a float flag with specified name, default value, and usage string.
// The return value is the address of a float variable that stores the value of the flag.
func Float(name string, value float, usage string) *float {
p := new(float);
FloatVar(p, name, value, usage);
return p;
}
// Float64Var defines a float64 flag with specified name, default value, and usage string.
// The argument p points to a float64 variable in which to store the value of the flag.
func Float64Var(p *float64, name string, value float64, usage string) {
add(name, newFloat64Value(value, p), usage);
}
// Float64 defines a float64 flag with specified name, default value, and usage string.
// The return value is the address of a float64 variable that stores the value of the flag.
func Float64(name string, value float64, usage string) *float64 {
p := new(float64);
Float64Var(p, name, value, usage);
return p;
}
func (f *allFlags) parseOne(index int) (ok bool, next int) { func (f *allFlags) parseOne(index int) (ok bool, next int) {
s := os.Args[index]; s := os.Args[index];
f.first_arg = index; // until proven otherwise f.first_arg = index; // until proven otherwise
......
...@@ -16,6 +16,8 @@ var ( ...@@ -16,6 +16,8 @@ var (
test_uint = Uint("test_uint", 0, "uint value"); test_uint = Uint("test_uint", 0, "uint value");
test_uint64 = Uint64("test_uint64", 0, "uint64 value"); test_uint64 = Uint64("test_uint64", 0, "uint64 value");
test_string = String("test_string", "0", "string value"); test_string = String("test_string", "0", "string value");
test_float = Float("test_float", 0, "float value");
test_float64 = Float("test_float64", 0, "float64 value");
) )
func boolString(s string) string { func boolString(s string) string {
...@@ -44,7 +46,7 @@ func TestEverything(t *testing.T) { ...@@ -44,7 +46,7 @@ func TestEverything(t *testing.T) {
} }
}; };
VisitAll(visitor); VisitAll(visitor);
if len(m) != 6 { if len(m) != 8 {
t.Error("VisitAll misses some flags"); t.Error("VisitAll misses some flags");
for k, v := range m { for k, v := range m {
t.Log(k, *v); t.Log(k, *v);
...@@ -65,9 +67,11 @@ func TestEverything(t *testing.T) { ...@@ -65,9 +67,11 @@ func TestEverything(t *testing.T) {
Set("test_uint", "1"); Set("test_uint", "1");
Set("test_uint64", "1"); Set("test_uint64", "1");
Set("test_string", "1"); Set("test_string", "1");
Set("test_float", "1");
Set("test_float64", "1");
desired = "1"; desired = "1";
Visit(visitor); Visit(visitor);
if len(m) != 6 { if len(m) != 8 {
t.Error("Visit fails after set"); t.Error("Visit fails after set");
for k, v := range m { for k, v := range m {
t.Log(k, *v); t.Log(k, *v);
......
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