diff options
| author | Tomas Aschan <[email protected]> | 2025-05-14 21:39:01 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-14 21:39:01 +0200 |
| commit | 7322552c7c20c512a8b5bedeb1ad4a9504fcee00 (patch) | |
| tree | 8f65bfadb6ff474bb04d04683e7a3b3cfeab38a6 /golangflag.go | |
| parent | 957ea4b77e38a9c6673053aa3513a727393b6941 (diff) | |
| parent | 9c97fadb5c53411b3c4f0c1021f605b967632efd (diff) | |
Merge pull request #424 from valdar/issue/423
fix #423 : Add helper function and some documentation to parse shorthand go test flags.
Diffstat (limited to 'golangflag.go')
| -rw-r--r-- | golangflag.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/golangflag.go b/golangflag.go index d3dd72b..f563907 100644 --- a/golangflag.go +++ b/golangflag.go @@ -10,6 +10,15 @@ import ( "strings" ) +// go test flags prefixes +func isGotestFlag(flag string) bool { + return strings.HasPrefix(flag, "-test.") +} + +func isGotestShorthandFlag(flag string) bool { + return strings.HasPrefix(flag, "test.") +} + // flagValueWrapper implements pflag.Value around a flag.Value. The main // difference here is the addition of the Type method that returns a string // name of the type. As this is generally unknown, we approximate that with @@ -103,3 +112,16 @@ func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) { } f.addedGoFlagSets = append(f.addedGoFlagSets, newSet) } + +// ParseSkippedFlags explicitly Parses go test flags (i.e. the one starting with '-test.') with goflag.Parse(), +// since by default those are skipped by pflag.Parse(). +// Typical usage example: `ParseGoTestFlags(os.Args[1:], goflag.CommandLine)` +func ParseSkippedFlags(osArgs []string, goFlagSet *goflag.FlagSet) error { + var skippedFlags []string + for _, f := range osArgs { + if isGotestFlag(f) { + skippedFlags = append(skippedFlags, f) + } + } + return goFlagSet.Parse(skippedFlags) +} |
