Skip to content

Commit

Permalink
Added Args.GetBool helper
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Jan 30, 2017
1 parent 2ada93a commit b69eba7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,19 @@ func (a *Args) GetUfloatOrZero(key string) float64 {
return f
}

// GetBool returns boolean value for the given key.
//
// true is returned for '1', 'y' and 'yes' values,
// otherwise false is returned.
func (a *Args) GetBool(key string) bool {
switch string(a.Peek(key)) {
case "1", "y", "yes":
return true
default:
return false
}
}

func visitArgs(args []argsKV, f func(k, v []byte)) {
for i, n := 0, len(args); i < n; i++ {
kv := &args[i]
Expand Down
23 changes: 23 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ func TestArgsWriteTo(t *testing.T) {
}
}

func TestArgsGetBool(t *testing.T) {
testArgsGetBool(t, "", false)
testArgsGetBool(t, "0", false)
testArgsGetBool(t, "n", false)
testArgsGetBool(t, "no", false)
testArgsGetBool(t, "1", true)
testArgsGetBool(t, "y", true)
testArgsGetBool(t, "yes", true)

testArgsGetBool(t, "123", false)
testArgsGetBool(t, "foobar", false)
}

func testArgsGetBool(t *testing.T, value string, expectedResult bool) {
var a Args
a.Parse("v=" + value)

result := a.GetBool("v")
if result != expectedResult {
t.Fatalf("unexpected result %v. Expecting %v for value %q", result, expectedResult, value)
}
}

func TestArgsUint(t *testing.T) {
var a Args
a.SetUint("foo", 123)
Expand Down

0 comments on commit b69eba7

Please sign in to comment.