Repository: goplus/gop
Pull request #1308 (2022-06-26 01:59:50)

gop cmd pass build flags
support #1300 

```
func NewPassArgs(flag *flag.FlagSet) *PassArgs
```
```
func PassBuildFlags(cmd *Command) *PassArgs
```

**TODO**
```
func PassTestFlags
```


Patch:
diff --git a/cmd/internal/base/pass.go b/cmd/internal/base/pass.go
new file mode 100644
index 00000000..8f7e15ce
--- /dev/null
+++ b/cmd/internal/base/pass.go
@@ -0,0 +1,72 @@
+package base
+
+import (
+	"flag"
+	"fmt"
+)
+
+type stringValue struct {
+	p    *PassArgs
+	name string
+}
+
+func (p *stringValue) String() string {
+	return ""
+}
+
+func (p *stringValue) Set(v string) error {
+	p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
+	return nil
+}
+
+type boolValue struct {
+	p    *PassArgs
+	name string
+}
+
+func (p *boolValue) String() string {
+	return ""
+}
+
+func (p *boolValue) Set(v string) error {
+	p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
+	return nil
+}
+
+func (p *boolValue) IsBoolFlag() bool {
+	return true
+}
+
+type PassArgs struct {
+	Args []string
+	Flag *flag.FlagSet
+}
+
+func (p *PassArgs) Var(names ...string) {
+	for _, name := range names {
+		p.Flag.Var(&stringValue{p: p, name: name}, name, "")
+	}
+}
+
+func (p *PassArgs) Bool(names ...string) {
+	for _, name := range names {
+		p.Flag.Var(&boolValue{p: p, name: name}, name, "")
+	}
+}
+
+func NewPassArgs(flag *flag.FlagSet) *PassArgs {
+	p := &PassArgs{Flag: flag}
+	return p
+}
+
+func PassBuildFlags(cmd *Command) *PassArgs {
+	p := NewPassArgs(&cmd.Flag)
+	p.Bool("n", "x")
+	p.Bool("a")
+	p.Bool("linkshared", "race", "msan", "asan",
+		"trimpath", "work")
+	p.Var("p", "asmflags", "compiler", "buildmode",
+		"gcflags", "gccgoflags", "installsuffix",
+		"ldflags", "pkgdir", "tags", "toolexec", "buildvcs")
+	return p
+}
diff --git a/cmd/internal/build/build.go b/cmd/internal/build/build.go
index 068dc28c..7bbc993f 100644
--- a/cmd/internal/build/build.go
+++ b/cmd/internal/build/build.go
@@ -50,7 +50,8 @@ func init() {
 	Cmd.Run = runCmd
 }
 
-func runCmd(_ *base.Command, args []string) {
+func runCmd(cmd *base.Command, args []string) {
+	pass := base.PassBuildFlags(cmd)
 	err := flag.Parse(args)
 	if err != nil {
 		log.Panicln("parse input arguments failed:", err)
@@ -85,6 +86,7 @@ func runCmd(_ *base.Command, args []string) {
 		}
 		confCmd.Flags = []string{"-o", output}
 	}
+	confCmd.Flags = append(confCmd.Flags, pass.Args...)
 	build(proj, conf, confCmd)
 }
 
diff --git a/cmd/internal/install/install.go b/cmd/internal/install/install.go
index bf37a94d..7de17a69 100644
--- a/cmd/internal/install/install.go
+++ b/cmd/internal/install/install.go
@@ -48,7 +48,8 @@ func init() {
 	Cmd.Run = runCmd
 }
 
-func runCmd(_ *base.Command, args []string) {
+func runCmd(cmd *base.Command, args []string) {
+	pass := base.PassBuildFlags(cmd)
 	err := flag.Parse(args)
 	if err != nil {
 		log.Fatalln("parse input arguments failed:", err)
@@ -73,6 +74,7 @@ func runCmd(_ *base.Command, args []string) {
 	gopEnv := gopenv.Get()
 	conf := &gop.Config{Gop: gopEnv}
 	confCmd := &gocmd.Config{Gop: gopEnv}
+	confCmd.Flags = pass.Args
 	for _, proj := range projs {
 		install(proj, conf, confCmd)
 	}
diff --git a/cmd/internal/run/run.go b/cmd/internal/run/run.go
index ff6c8b4a..fc82d407 100644
--- a/cmd/internal/run/run.go
+++ b/cmd/internal/run/run.go
@@ -54,6 +54,7 @@ func init() {
 }
 
 func runCmd(cmd *base.Command, args []string) {
+	pass := base.PassBuildFlags(cmd)
 	err := flag.Parse(args)
 	if err != nil {
 		log.Fatalln("parse input arguments failed:", err)
@@ -89,6 +90,7 @@ func runCmd(cmd *base.Command, args []string) {
 	gopEnv := gopenv.Get()
 	conf := &gop.Config{Gop: gopEnv}
 	confCmd := &gocmd.Config{Gop: gopEnv}
+	confCmd.Flags = pass.Args
 	run(proj, args, !noChdir, conf, confCmd)
 }
 


Test patch:
diff --git a/cmd/internal/test/test.go b/cmd/internal/test/test.go
index c6bfb915..f6650678 100644
--- a/cmd/internal/test/test.go
+++ b/cmd/internal/test/test.go
@@ -48,7 +48,8 @@ func init() {
 	Cmd.Run = runCmd
 }
 
-func runCmd(_ *base.Command, args []string) {
+func runCmd(cmd *base.Command, args []string) {
+	pass := base.PassBuildFlags(cmd)
 	err := flag.Parse(args)
 	if err != nil {
 		log.Fatalln("parse input arguments failed:", err)
@@ -73,6 +74,7 @@ func runCmd(_ *base.Command, args []string) {
 	gopEnv := gopenv.Get()
 	conf := &gop.Config{Gop: gopEnv}
 	confCmd := &gocmd.Config{Gop: gopEnv}
+	confCmd.Flags = pass.Args
 	for _, proj := range projs {
 		test(proj, conf, confCmd)
 	}