1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:24:40 -07:00

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
This commit is contained in:
Eric Eisner 2012-01-06 09:23:00 +11:00 committed by Andrew Gerrand
parent 9443949207
commit fd1322828c
2 changed files with 23 additions and 4 deletions

View File

@ -157,6 +157,7 @@ type action struct {
deps []*action // actions that must happen before this one
triggers []*action // inverse of deps
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)
ignoreFail bool // whether to run f even if dependencies fail

View File

@ -12,7 +12,7 @@ func init() {
}
var cmdRun = &Command{
UsageLine: "run [-a] [-n] [-x] gofiles...",
UsageLine: "run [-a] [-n] [-x] gofiles... [-- arguments...]",
Short: "compile and run Go program",
Long: `
Run compiles and runs the main package comprising the named Go source files.
@ -32,16 +32,34 @@ var runX = cmdRun.Flag.Bool("x", false, "")
func runRun(cmd *Command, args []string) {
var b builder
b.init(*runA, *runN, *runX)
p := goFilesPackage(args, "")
files, args := splitArgs(args)
p := goFilesPackage(files, "")
p.target = "" // must build - not up to date
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)
}
// runProgram is the action for running a binary that has already
// been compiled. We ignore exit status.
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 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
}