From e3ace79a91d4333dc0f5cdc5bf5e1a3e58dddd21 Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Sat, 7 May 2016 16:06:28 -0700 Subject: [PATCH] Add GlobalBoolT Fixes #206 --- CHANGELOG.md | 1 + context.go | 8 ++++++++ context_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7ea0a6..3d07131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## [Unreleased] ### Added - Pluggable flag-level help text rendering via `cli.DefaultFlagStringFunc` +- `context.GlobalBoolT` was added as an analogue to `context.GlobalBool` ### Changed - `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer diff --git a/context.go b/context.go index ef3d2fc..aad2812 100644 --- a/context.go +++ b/context.go @@ -104,6 +104,14 @@ func (c *Context) GlobalBool(name string) bool { return false } +// Looks up the value of a global bool flag, returns true if no bool flag exists +func (c *Context) GlobalBoolT(name string) bool { + if fs := lookupGlobalFlagSet(name, c); fs != nil { + return lookupBoolT(name, fs) + } + return false +} + // Looks up the value of a global string flag, returns "" if no string flag exists func (c *Context) GlobalString(name string) string { if fs := lookupGlobalFlagSet(name, c); fs != nil { diff --git a/context_test.go b/context_test.go index 4c23271..7ba2ebd 100644 --- a/context_test.go +++ b/context_test.go @@ -82,6 +82,30 @@ func TestContext_BoolT(t *testing.T) { expect(t, c.BoolT("myflag"), true) } +func TestContext_GlobalBool(t *testing.T) { + set := flag.NewFlagSet("test", 0) + + globalSet := flag.NewFlagSet("test-global", 0) + globalSet.Bool("myflag", false, "doc") + globalCtx := NewContext(nil, globalSet, nil) + + c := NewContext(nil, set, globalCtx) + expect(t, c.GlobalBool("myflag"), false) + expect(t, c.GlobalBool("nope"), false) +} + +func TestContext_GlobalBoolT(t *testing.T) { + set := flag.NewFlagSet("test", 0) + + globalSet := flag.NewFlagSet("test-global", 0) + globalSet.Bool("myflag", true, "doc") + globalCtx := NewContext(nil, globalSet, nil) + + c := NewContext(nil, set, globalCtx) + expect(t, c.GlobalBoolT("myflag"), true) + expect(t, c.GlobalBoolT("nope"), false) +} + func TestContext_Args(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc")