Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
379150c2
Commit
379150c2
authored
Nov 07, 2009
by
Vish Subramanian
Committed by
Rob Pike
Nov 07, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add flags of type float to the flag package.
R=r, rsc
http://go/go-review/1026011
parent
1ac60ddd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
2 deletions
+71
-2
flag.go
src/pkg/flag/flag.go
+65
-0
flag_test.go
src/pkg/flag/flag_test.go
+6
-2
No files found.
src/pkg/flag/flag.go
View file @
379150c2
...
...
@@ -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
...
...
src/pkg/flag/flag_test.go
View file @
379150c2
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment