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 /README.md | |
| 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 '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 |
