1
0
mirror of https://github.com/golang/go synced 2024-11-22 06:04:39 -07:00

Add flags of type float to the flag package.

R=r, rsc
http://go/go-review/1026011
This commit is contained in:
Vish Subramanian 2009-11-07 15:52:27 -08:00 committed by Rob Pike
parent 1ac60ddd10
commit 379150c210
2 changed files with 71 additions and 2 deletions

View File

@ -166,6 +166,42 @@ func (s *stringValue) set(val string) bool {
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.
// (The default value is represented as a string.)
type FlagValue interface {
@ -359,6 +395,35 @@ func String(name, value string, usage string) *string {
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) {
s := os.Args[index];
f.first_arg = index; // until proven otherwise

View File

@ -16,6 +16,8 @@ var (
test_uint = Uint("test_uint", 0, "uint value");
test_uint64 = Uint64("test_uint64", 0, "uint64 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 {
@ -44,7 +46,7 @@ func TestEverything(t *testing.T) {
}
};
VisitAll(visitor);
if len(m) != 6 {
if len(m) != 8 {
t.Error("VisitAll misses some flags");
for k, v := range m {
t.Log(k, *v);
@ -65,9 +67,11 @@ func TestEverything(t *testing.T) {
Set("test_uint", "1");
Set("test_uint64", "1");
Set("test_string", "1");
Set("test_float", "1");
Set("test_float64", "1");
desired = "1";
Visit(visitor);
if len(m) != 6 {
if len(m) != 8 {
t.Error("Visit fails after set");
for k, v := range m {
t.Log(k, *v);