From d1cdecf78e879f31cc80cd5931df18af04cff9d8 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 7 Dec 2018 14:21:37 -0800 Subject: [PATCH] cmd/starlark: add -c flag to execute cmdline program (#47) -c was chosen to match Python. Sample: $ starlark -c "print(max(range(10)))" 9 $ starlark -c "a = 'hi'" -showenv a = "hi" --- cmd/starlark/starlark.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/starlark/starlark.go b/cmd/starlark/starlark.go index d9dbd02b..46dc5983 100644 --- a/cmd/starlark/starlark.go +++ b/cmd/starlark/starlark.go @@ -24,6 +24,7 @@ import ( var ( cpuprofile = flag.String("cpuprofile", "", "gather CPU profile in this file") showenv = flag.Bool("showenv", false, "on success, print final global environment") + execprog = flag.String("c", "", "execute program `prog`") ) // non-standard dialect flags @@ -55,19 +56,29 @@ func main() { thread := &starlark.Thread{Load: repl.MakeLoad()} globals := make(starlark.StringDict) - switch len(flag.Args()) { - case 0: - fmt.Println("Welcome to Starlark (go.starlark.net)") - repl.REPL(thread, globals) - case 1: - // Execute specified file. - filename := flag.Args()[0] - var err error - globals, err = starlark.ExecFile(thread, filename, nil, nil) + switch { + case flag.NArg() == 1 || *execprog != "": + var ( + filename string + src interface{} + err error + ) + if *execprog != "" { + // Execute provided program. + filename = "cmdline" + src = *execprog + } else { + // Execute specified file. + filename = flag.Arg(0) + } + globals, err = starlark.ExecFile(thread, filename, src, nil) if err != nil { repl.PrintError(err) os.Exit(1) } + case flag.NArg() == 0: + fmt.Println("Welcome to Starlark (go.starlark.net)") + repl.REPL(thread, globals) default: log.Fatal("want at most one Starlark file name") }