Commit fd132282 authored by Eric Eisner's avatar Eric Eisner Committed by Andrew Gerrand

cmd/go: Pass arguments to command for run

Command arguments are separated from input .go file arguments
by a -- separator.

R=rsc, golang-dev, adg
CC=golang-dev
https://golang.org/cl/5514046
parent 94439492
...@@ -157,6 +157,7 @@ type action struct { ...@@ -157,6 +157,7 @@ type action struct {
deps []*action // actions that must happen before this one deps []*action // actions that must happen before this one
triggers []*action // inverse of deps triggers []*action // inverse of deps
cgo *action // action for cgo binary if needed cgo *action // action for cgo binary if needed
args []string // additional args for runProgram
f func(*builder, *action) error // the action itself (nil = no-op) f func(*builder, *action) error // the action itself (nil = no-op)
ignoreFail bool // whether to run f even if dependencies fail ignoreFail bool // whether to run f even if dependencies fail
......
...@@ -12,7 +12,7 @@ func init() { ...@@ -12,7 +12,7 @@ func init() {
} }
var cmdRun = &Command{ var cmdRun = &Command{
UsageLine: "run [-a] [-n] [-x] gofiles...", UsageLine: "run [-a] [-n] [-x] gofiles... [-- arguments...]",
Short: "compile and run Go program", Short: "compile and run Go program",
Long: ` Long: `
Run compiles and runs the main package comprising the named Go source files. Run compiles and runs the main package comprising the named Go source files.
...@@ -32,16 +32,34 @@ var runX = cmdRun.Flag.Bool("x", false, "") ...@@ -32,16 +32,34 @@ var runX = cmdRun.Flag.Bool("x", false, "")
func runRun(cmd *Command, args []string) { func runRun(cmd *Command, args []string) {
var b builder var b builder
b.init(*runA, *runN, *runX) b.init(*runA, *runN, *runX)
p := goFilesPackage(args, "") files, args := splitArgs(args)
p := goFilesPackage(files, "")
p.target = "" // must build - not up to date p.target = "" // must build - not up to date
a1 := b.action(modeBuild, modeBuild, p) a1 := b.action(modeBuild, modeBuild, p)
a := &action{f: (*builder).runProgram, deps: []*action{a1}} a := &action{f: (*builder).runProgram, args: args, deps: []*action{a1}}
b.do(a) b.do(a)
} }
// runProgram is the action for running a binary that has already // runProgram is the action for running a binary that has already
// been compiled. We ignore exit status. // been compiled. We ignore exit status.
func (b *builder) runProgram(a *action) error { func (b *builder) runProgram(a *action) error {
run(a.deps[0].target) args := append([]string{a.deps[0].target}, a.args...)
run(args...)
return nil return nil
} }
// Return the argument slices before and after the "--"
func splitArgs(args []string) (before, after []string) {
dashes := len(args)
for i, arg := range args {
if arg == "--" {
dashes = i
break
}
}
before = args[:dashes]
if dashes < len(args) {
after = args[dashes+1:]
}
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment