//
// The generator is run in the package's source directory.
//
-// Go generate accepts one specific flag:
+// Go generate accepts two specific flags:
//
// -run=""
// if non-empty, specifies a regular expression to select
// any trailing spaces and final newline) matches the
// expression.
//
+// -skip=""
+// if non-empty, specifies a regular expression to suppress
+// directives whose full original source text (excluding
+// any trailing spaces and final newline) matches the
+// expression. If a directive matches both the -run and
+// the -skip arguments, it is skipped.
+//
// It also accepts the standard build flags including -v, -n, and -x.
// The -v flag prints the names of packages and files as they are
// processed.
The generator is run in the package's source directory.
-Go generate accepts one specific flag:
+Go generate accepts two specific flags:
-run=""
if non-empty, specifies a regular expression to select
any trailing spaces and final newline) matches the
expression.
+ -skip=""
+ if non-empty, specifies a regular expression to suppress
+ directives whose full original source text (excluding
+ any trailing spaces and final newline) matches the
+ expression. If a directive matches both the -run and
+ the -skip arguments, it is skipped.
+
It also accepts the standard build flags including -v, -n, and -x.
The -v flag prints the names of packages and files as they are
processed.
var (
generateRunFlag string // generate -run flag
generateRunRE *regexp.Regexp // compiled expression for -run
+
+ generateSkipFlag string // generate -skip flag
+ generateSkipRE *regexp.Regexp // compiled expression for -skip
)
func init() {
work.AddBuildFlags(CmdGenerate, work.DefaultBuildFlags)
CmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "")
+ CmdGenerate.Flag.StringVar(&generateSkipFlag, "skip", "", "")
}
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
log.Fatalf("generate: %s", err)
}
}
+ if generateSkipFlag != "" {
+ var err error
+ generateSkipRE, err = regexp.Compile(generateSkipFlag)
+ if err != nil {
+ log.Fatalf("generate: %s", err)
+ }
+ }
cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "generate")
if !isGoGenerate(buf) {
continue
}
- if generateRunFlag != "" {
- if !generateRunRE.Match(bytes.TrimSpace(buf)) {
- continue
- }
+ if generateRunFlag != "" && !generateRunRE.Match(bytes.TrimSpace(buf)) {
+ continue
+ }
+ if generateSkipFlag != "" && generateSkipRE.Match(bytes.TrimSpace(buf)) {
+ continue
}
g.setEnv()
go generate './generate/substitution.go'
stdout $GOARCH' substitution.go:7 pabc xyzp/substitution.go/123'
-# Test go generate's run flag
+# Test go generate's run and skip flags
go generate -run y.s './generate/flag.go'
stdout 'yes' # flag.go should select yes
! stdout 'no' # flag.go should not select no
+go generate -skip th..sand './generate/flag.go'
+stdout 'yes' # flag.go should select yes
+! stdout 'no' # flag.go should not select no
+
+go generate -run . -skip th..sand './generate/flag.go'
+stdout 'yes' # flag.go should select yes
+! stdout 'no' # flag.go should not select no
+
# Test go generate provides the right "$GOPACKAGE" name in an x_test
go generate './generate/env_test.go'
stdout 'main_test'