Skip to content

Commit

Permalink
cmd/starlark: add -c flag to execute cmdline program (google#47)
Browse files Browse the repository at this point in the history
-c was chosen to match Python. Sample:

$ starlark -c "print(max(range(10)))"
9
$ starlark -c "a = 'hi'" -showenv
a = "hi"
  • Loading branch information
josharian authored and adonovan committed Dec 7, 2018
1 parent 2f5aafd commit d1cdecf
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions cmd/starlark/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit d1cdecf

Please sign in to comment.