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 /README.md | |
| parent | 19c9c4072e41218b18b93dbfc3798c18835d2fd5 (diff) | |
fix #423 : Add helper function and some documentation to parse shorthand go test flags.
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 |
