@@ -9,25 +9,32 @@ import (
9
9
"os"
10
10
"regexp"
11
11
12
+ "github.com/cockroachdb/errors"
12
13
"github.com/spf13/pflag"
13
14
)
14
15
15
16
// 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 {
17
18
return Do (os .Args [1 :], name , out )
18
19
}
19
20
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.
25
25
//
26
- // This is a helper for benchmarks that want to react to flags from their
27
- // environment.
28
- func Do (args []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 {
29
29
pf := pflag .NewFlagSet ("test" , pflag .ContinueOnError )
30
- pf .StringVar (out , name , "" , "" )
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
+ }
31
38
pf .ParseErrorsWhitelist = pflag.ParseErrorsWhitelist {UnknownFlags : true }
32
39
args = append ([]string (nil ), args ... )
33
40
for i , arg := range args {
0 commit comments