diff --git a/mage/args_test.go b/mage/args_test.go index 6997df8..d8ef8ab 100644 --- a/mage/args_test.go +++ b/mage/args_test.go @@ -12,12 +12,12 @@ func TestArgs(t *testing.T) { Dir: "./testdata/args", Stderr: stderr, Stdout: stdout, - Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false"}, + Args: []string{"status", "say", "hi", "bob", "count", "5", "status", "wait", "5ms", "cough", "false", "doubleIt", "3.1"}, } code := Invoke(inv) if code != 0 { t.Log(stderr.String()) - t.Fatalf("expected 1, but got %v", code) + t.Fatalf("expected 0, but got %v", code) } actual := stdout.String() expected := `status @@ -26,6 +26,7 @@ saying hi bob status waiting 5ms not coughing +3.1 * 2 = 6.2 ` if actual != expected { t.Fatalf("output is not expected:\n%q", actual) @@ -101,6 +102,29 @@ func TestBadDurationArg(t *testing.T) { } } +func TestBadFloat64Arg(t *testing.T) { + stderr := &bytes.Buffer{} + stdout := &bytes.Buffer{} + inv := Invocation{ + Dir: "./testdata/args", + Stderr: stderr, + Stdout: stdout, + Args: []string{"doubleIt", "abc123"}, + } + code := Invoke(inv) + if code != 2 { + t.Log("stderr:", stderr) + t.Log("stdout:", stdout) + t.Fatalf("expected code 2, but got %v", code) + } + actual := stderr.String() + expected := "can't convert argument \"abc123\" to float64\n" + + if actual != expected { + t.Fatalf("output is not expected:\n%q", actual) + } +} + func TestMissingArgs(t *testing.T) { stderr := &bytes.Buffer{} stdout := &bytes.Buffer{} diff --git a/mage/testdata/args/magefile.go b/mage/testdata/args/magefile.go index de0f262..b74483c 100644 --- a/mage/testdata/args/magefile.go +++ b/mage/testdata/args/magefile.go @@ -50,3 +50,7 @@ func Cough(ctx context.Context, b bool) error { func HasDep() { mg.Deps(mg.F(Say, "hi", "Susan")) } + +func DoubleIt(f float64) { + fmt.Printf("%.1f * 2 = %.1f\n", f, f*2) +} diff --git a/parse/parse.go b/parse/parse.go index e68703b..c64e7cc 100644 --- a/parse/parse.go +++ b/parse/parse.go @@ -128,6 +128,14 @@ func (f Function) ExecCode() string { os.Exit(2) } x++`, x) + case "float64": + parseargs += fmt.Sprintf(` + arg%d, err := strconv.ParseFloat(args.Args[x], 64) + if err != nil { + logger.Printf("can't convert argument %%q to float64\n", args.Args[x]) + os.Exit(2) + } + x++`, x) case "bool": parseargs += fmt.Sprintf(` arg%d, err := strconv.ParseBool(args.Args[x]) @@ -845,6 +853,7 @@ func toOneLine(s string) string { var argTypes = map[string]string{ "string": "string", "int": "int", + "float64": "float64", "&{time Duration}": "time.Duration", "bool": "bool", }