Skip to content

Commit 26d5060

Browse files
committed
add the ability to call targets with float64 args
1 parent bdc92f6 commit 26d5060

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

mage/args_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func TestArgs(t *testing.T) {
1212
Dir: "./testdata/args",
1313
Stderr: stderr,
1414
Stdout: stdout,
15-
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"},
15+
Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false", "doubleIt", "3.14"},
1616
}
1717
code := Invoke(inv)
1818
if code != 0 {
1919
t.Log(stderr.String())
20-
t.Fatalf("expected 1, but got %v", code)
20+
t.Fatalf("expected 0, but got %v", code)
2121
}
2222
actual := stdout.String()
2323
expected := `status
@@ -26,6 +26,7 @@ saying hi bob
2626
status
2727
waiting 5ms
2828
not coughing
29+
3.1 * 2 = 6.3
2930
`
3031
if actual != expected {
3132
t.Fatalf("output is not expected:\n%q", actual)
@@ -101,6 +102,29 @@ func TestBadDurationArg(t *testing.T) {
101102
}
102103
}
103104

105+
func TestBadFloat64Arg(t *testing.T) {
106+
stderr := &bytes.Buffer{}
107+
stdout := &bytes.Buffer{}
108+
inv := Invocation{
109+
Dir: "./testdata/args",
110+
Stderr: stderr,
111+
Stdout: stdout,
112+
Args: []string{"doubleIt", "abc123"},
113+
}
114+
code := Invoke(inv)
115+
if code != 2 {
116+
t.Log("stderr:", stderr)
117+
t.Log("stdout:", stdout)
118+
t.Fatalf("expected code 2, but got %v", code)
119+
}
120+
actual := stderr.String()
121+
expected := "can't convert argument \"abc123\" to float64\n"
122+
123+
if actual != expected {
124+
t.Fatalf("output is not expected:\n%q", actual)
125+
}
126+
}
127+
104128
func TestMissingArgs(t *testing.T) {
105129
stderr := &bytes.Buffer{}
106130
stdout := &bytes.Buffer{}

mage/testdata/args/magefile.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ func Cough(ctx context.Context, b bool) error {
5050
func HasDep() {
5151
mg.Deps(mg.F(Say, "hi", "Susan"))
5252
}
53+
54+
func DoubleIt(f float64) {
55+
fmt.Printf("%.1f * 2 = %.1f\n", f, f*2)
56+
}

parse/parse.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ func (f Function) ExecCode() string {
128128
os.Exit(2)
129129
}
130130
x++`, x)
131+
case "float64":
132+
parseargs += fmt.Sprintf(`
133+
arg%d, err := strconv.ParseFloat(args.Args[x], 64)
134+
if err != nil {
135+
logger.Printf("can't convert argument %%q to float64\n", args.Args[x])
136+
os.Exit(2)
137+
}
138+
x++`, x)
131139
case "bool":
132140
parseargs += fmt.Sprintf(`
133141
arg%d, err := strconv.ParseBool(args.Args[x])
@@ -845,6 +853,7 @@ func toOneLine(s string) string {
845853
var argTypes = map[string]string{
846854
"string": "string",
847855
"int": "int",
856+
"float64": "float64",
848857
"&{time Duration}": "time.Duration",
849858
"bool": "bool",
850859
}

0 commit comments

Comments
 (0)