Skip to content

Commit

Permalink
Making moreflag.Parse safe to call repeatedly. (bazelbuild#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ola Rozenfeld authored Sep 13, 2019
1 parent 7580a40 commit 6783c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions go/pkg/moreflag/moreflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
// Parse parses flags which are set in environment variables using the FLAG_ prefix. That is
// for a flag named x, x=$FLAG_x if $FLAG_x is set. If the flag is set in the command line, the
// command line value of the flag takes precedence over the environment variable value.
// It also calls flag.Parse() to parse flags sent directly as arguments.
// It also calls flag.Parse() to parse flags sent directly as arguments, unless flag.Parse
// has been previously called.
func Parse() {
ParseFromEnv()
flag.Parse()
if !flag.Parsed() {
flag.Parse()
}
}

// ParseFromEnv parses flags which are set in environment variables using the FLAG_ prefix. That is
Expand Down
6 changes: 3 additions & 3 deletions go/pkg/moreflag/moreflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestParseFromEnv(t *testing.T) {
func TestParse(t *testing.T) {
f := flag.String("value", "", "Some value")

ParseFromEnv()
Parse()
if *f != "" {
t.Errorf("Flag has wrong value, want '', got %q", *f)
}

os.Setenv("FLAG_value", "test")
defer os.Setenv("FLAG_value", "")
ParseFromEnv()
Parse()
if *f != "test" {
t.Errorf("Flag has wrong value, want 'test', got %q", *f)
}
Expand Down

0 comments on commit 6783c04

Please sign in to comment.