Skip to content

Commit 45bb591

Browse files
committed
fix #423 : Add helper function and some documentation to parse shorthand go test flags.
1 parent 19c9c40 commit 45bb591

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,26 @@ func main() {
284284
}
285285
```
286286

287+
### Using pflag with go test
288+
`pflag` skips parsing of shorthand version of go test specific flags (i.e. the one passed as `-test.`)
289+
See issues: [#63](https://github.com/spf13/pflag/issues/63) and [#238](https://github.com/spf13/pflag/issues/238) for more details.
290+
291+
**Example**: You want to separately parse go test flags anyway
292+
```go
293+
import (
294+
goflag "flag"
295+
flag "github.com/spf13/pflag"
296+
)
297+
298+
var ip *int = flag.Int("flagname", 1234, "help message for flagname")
299+
300+
func main() {
301+
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
302+
ParseGoTestFlags(os.Args[1:], goflag.CommandLine)
303+
flag.Parse()
304+
}
305+
```
306+
287307
## More info
288308

289309
You can see the full reference documentation of the pflag package

golangflag.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {
103103
}
104104
f.addedGoFlagSets = append(f.addedGoFlagSets, newSet)
105105
}
106+
107+
// ParseGoTestFlags Parses go test flags (i.e. the one with '-test.') separately because pflag.Parse() skip flags with '-test.' prefix
108+
// Typical usage example: `ParseGoTestFlags(os.Args[1:], goflag.CommandLine)`
109+
func ParseGoTestFlags(osArgs []string, goFlagSet *goflag.FlagSet) error {
110+
var goTestFlags []string
111+
for _, f := range osArgs {
112+
if strings.HasPrefix(f, "-test.") {
113+
goTestFlags = append(goTestFlags, f)
114+
}
115+
}
116+
return goFlagSet.Parse(goTestFlags)
117+
}

golangflag_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import (
1212
func TestGoflags(t *testing.T) {
1313
goflag.String("stringFlag", "stringFlag", "stringFlag")
1414
goflag.Bool("boolFlag", false, "boolFlag")
15+
var testxxxValue string
16+
goflag.StringVar(&testxxxValue, "test.xxx", "test.xxx", "it is a test flag")
1517

1618
f := NewFlagSet("test", ContinueOnError)
1719

1820
f.AddGoFlagSet(goflag.CommandLine)
19-
err := f.Parse([]string{"--stringFlag=bob", "--boolFlag"})
21+
args := []string{"--stringFlag=bob", "--boolFlag", "-test.xxx=testvalue"}
22+
err := f.Parse(args)
2023
if err != nil {
2124
t.Fatal("expected no error; get", err)
2225
}
@@ -40,6 +43,17 @@ func TestGoflags(t *testing.T) {
4043
t.Fatal("f.Parsed() return false after f.Parse() called")
4144
}
4245

46+
if testxxxValue != "test.xxx" {
47+
t.Fatalf("expected testxxxValue to be test.xxx but got %v", testxxxValue)
48+
}
49+
err = ParseGoTestFlags(args, goflag.CommandLine)
50+
if err != nil {
51+
t.Fatal("expected no error; ParseGoTestFlags", err)
52+
}
53+
if testxxxValue != "testvalue" {
54+
t.Fatalf("expected testxxxValue to be testvalue but got %v", testxxxValue)
55+
}
56+
4357
// in fact it is useless. because `go test` called flag.Parse()
4458
if !goflag.CommandLine.Parsed() {
4559
t.Fatal("goflag.CommandLine.Parsed() return false after f.Parse() called")

0 commit comments

Comments
 (0)