Skip to content

Commit 724f1d5

Browse files
craig[bot]tbg
andcommitted
Merge #150521
150521: sniffarg: simplify and add bool parsing r=tbg a=tbg See commits. This is in preparation for adding plotted charts (as images) to the asim datadriven tests, for which I'll want to detect the `-rewrite` flag from outside the `datadriven` package. Epic: CRDB-25222 Co-authored-by: Tobias Grieger <[email protected]>
2 parents dafaf1d + 511b4df commit 724f1d5

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

pkg/testutils/sniffarg/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ go_library(
55
srcs = ["sniffarg.go"],
66
importpath = "github.com/cockroachdb/cockroach/pkg/testutils/sniffarg",
77
visibility = ["//visibility:public"],
8-
deps = ["@com_github_spf13_pflag//:pflag"],
8+
deps = [
9+
"@com_github_cockroachdb_errors//:errors",
10+
"@com_github_spf13_pflag//:pflag",
11+
],
912
)
1013

1114
go_test(

pkg/testutils/sniffarg/sniffarg.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,39 @@ import (
99
"os"
1010
"regexp"
1111

12+
"github.com/cockroachdb/errors"
1213
"github.com/spf13/pflag"
1314
)
1415

1516
// DoEnv calls Do with os.Args[1:] as the first argument.
16-
func DoEnv(name string, out *string) error {
17+
func DoEnv(name string, out interface{}) error {
1718
return Do(os.Args[1:], name, out)
1819
}
1920

20-
// Do looks for the flags specified in `names` (no leading dashes) and
21-
// sets the corresponding `out` values to the values found in `args`. This only
22-
// works for string flags and in particular does not reliably work for
23-
// "presence" flags, such as bools, since these flags don't carry an explicit
24-
// value in the args.
21+
// Do looks for the flag `name` (no leading dashes) and sets the corresponding
22+
// `out` values to the value found in `args`.
23+
// Currently, `out` must be of type `*string` or `*bool`, though additional
24+
// types should be straightforward to add as needed.
2525
//
26-
// This is a helper for benchmarks that want to react to flags from their
27-
// environment.
28-
func Do(inArgs []string, name string, out *string) error {
26+
// This is a helper for tests and benchmarks that want to react to flags from
27+
// their environment.
28+
func Do(args []string, name string, out interface{}) error {
2929
pf := pflag.NewFlagSet("test", pflag.ContinueOnError)
30-
pf.StringVar(out, name, "", "")
31-
var args []string
32-
var addNext bool
33-
for _, arg := range inArgs {
34-
if addNext {
35-
addNext = false
36-
args = append(args, arg)
37-
}
30+
switch t := out.(type) {
31+
case *string:
32+
pf.StringVar(t, name, "", "")
33+
case *bool:
34+
pf.BoolVar(t, name, false, "")
35+
default:
36+
return errors.Errorf("unsupported type %T", t)
37+
}
38+
pf.ParseErrorsWhitelist = pflag.ParseErrorsWhitelist{UnknownFlags: true}
39+
args = append([]string(nil), args...)
40+
for i, arg := range args {
3841
re := regexp.MustCompile(`^(-{1,2})` + regexp.QuoteMeta(name) + `(=|$)`)
39-
if matches := re.FindStringSubmatch(arg); len(matches) > 0 {
40-
if len(matches[1]) == 1 {
41-
// Transform `-foo` into `--foo` for pflag-style flag.
42-
arg = "-" + arg
43-
}
44-
if len(matches[2]) == 0 {
45-
// The matched flag is of form `--foo bar` (vs `--foo=bar`), so value
46-
// is next arg.
47-
addNext = true
48-
}
49-
args = append(args, arg)
42+
if matches := re.FindStringSubmatch(arg); len(matches) > 0 && len(matches[1]) == 1 {
43+
// Transform `-foo` into `--foo` for pflag-style flag.
44+
args[i] = "-" + arg
5045
}
5146
}
5247
return pf.Parse(args)

pkg/testutils/sniffarg/sniffarg_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,28 @@ func TestDo(t *testing.T) {
2121
"-test.outputdir", "banana",
2222
"something",
2323
"--somethingelse", "foo",
24-
"--boolflag",
24+
"--falseflag=false",
25+
"--trueflag",
2526
}
2627
var benchMem string
2728
var outputDir string
2829
var somethingElse string
30+
var trueFlag bool
31+
var falseFlag bool
32+
var wrongType struct{}
2933
notFound := "hello"
34+
require.Error(t, Do(args, "test.benchmem", &wrongType))
3035
require.NoError(t, Do(args, "test.benchmem", &benchMem))
3136
require.NoError(t, Do(args, "test.outputdir", &outputDir))
3237
require.NoError(t, Do(args, "somethingelse", &somethingElse))
3338
require.NoError(t, Do(args, "notfound", &notFound))
39+
require.NoError(t, Do(args, "falseflag", &falseFlag))
40+
require.NoError(t, Do(args, "trueflag", &trueFlag))
3441
assert.Equal(t, "5", benchMem)
3542
assert.Equal(t, "banana", outputDir)
3643
assert.Equal(t, "foo", somethingElse)
44+
assert.False(t, falseFlag)
45+
assert.True(t, trueFlag)
3746
assert.Zero(t, notFound)
47+
3848
}

0 commit comments

Comments
 (0)