forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/internal/cmd/base: add pass args
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters