diff options
| author | Andrea Tarocchi <[email protected]> | 2025-04-13 11:25:48 +0200 |
|---|---|---|
| committer | Andrea Tarocchi <[email protected]> | 2025-05-14 11:59:31 +0200 |
| commit | 9c97fadb5c53411b3c4f0c1021f605b967632efd (patch) | |
| tree | 9dda321340d46f7f2d86aeaa0d23ceeacb46aa9b /golangflag.go | |
| parent | 19c9c4072e41218b18b93dbfc3798c18835d2fd5 (diff) | |
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) +} |
