diff options
| author | Tomas Aschan <[email protected]> | 2025-08-29 21:59:56 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-29 21:59:56 +0200 |
| commit | fd649b20e66a30631ee35bba78164180d02730d4 (patch) | |
| tree | 321b27604470eed0b00615ea41f0f32f9bf8104a /README.md | |
| parent | d66f0ce2a7f887f0ff5f5af48649f3a01b3f0566 (diff) | |
| parent | 1db553c27f5b9952dbb6484ed9999547b9e7dfb2 (diff) | |
Merge branch 'master' into fix-errhelp
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -284,6 +284,33 @@ func main() { } ``` +### Using pflag with go test +`pflag` does not parse the shorthand versions of go test's built-in flags (i.e., those starting with `-test.`). +For more context, see issues [#63](https://github.com/spf13/pflag/issues/63) and [#238](https://github.com/spf13/pflag/issues/238) for more details. + +For example, if you use pflag in your `TestMain` function and call `pflag.Parse()` after defining your custom flags, running a test like this: +```bash +go test /your/tests -run ^YourTest -v --your-test-pflags +``` +will result in the `-v` flag being ignored. This happens because of the way pflag handles flag parsing, skipping over go test's built-in shorthand flags. +To work around this, you can use the `ParseSkippedFlags` function, which ensures that go test's flags are parsed separately using the standard flag package. + +**Example**: You want to parse go test flags that are otherwise ignore by `pflag.Parse()` +```go +import ( + goflag "flag" + flag "github.com/spf13/pflag" +) + +var ip *int = flag.Int("flagname", 1234, "help message for flagname") + +func main() { + flag.CommandLine.AddGoFlagSet(goflag.CommandLine) + flag.ParseSkippedFlags(os.Args[1:], goflag.CommandLine) + flag.Parse() +} +``` + ## More info You can see the full reference documentation of the pflag package |
