summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVish Subramanian <vish@google.com>2009-11-07 15:52:27 -0800
committerVish Subramanian <vish@google.com>2009-11-07 15:52:27 -0800
commit5ed26239c2a7a3be8a5f2d99a6212ca4bf993d41 (patch)
tree64c85408f83eec9ec40673e74ab22519063a1ff0
parentc5d1ab2aed7adb17e46a6ec9c4adbb5984c29740 (diff)
downloadgolang-5ed26239c2a7a3be8a5f2d99a6212ca4bf993d41.tar.gz
Add flags of type float to the flag package.
R=r, rsc http://go/go-review/1026011 Committer: Rob Pike <r@golang.org>
-rw-r--r--src/pkg/flag/flag.go65
-rw-r--r--src/pkg/flag/flag_test.go8
2 files changed, 71 insertions, 2 deletions
diff --git a/src/pkg/flag/flag.go b/src/pkg/flag/flag.go
index 1f5f6434a..011b48b69 100644
--- a/src/pkg/flag/flag.go
+++ b/src/pkg/flag/flag.go
@@ -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
diff --git a/src/pkg/flag/flag_test.go b/src/pkg/flag/flag_test.go
index 59014dcd3..9a8b087b5 100644
--- a/src/pkg/flag/flag_test.go
+++ b/src/pkg/flag/flag_test.go
@@ -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);