From 5903a0a8448b15156c9f7a46dc11d41cad649b1d Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Wed, 5 Mar 2014 17:24:22 -0800 Subject: [PATCH] Add BoolTFlag type Compared to BoolFlag type, BoolTFlag treats 'true' as the default value for the flag. Without it, we have to use --no-action flag if we set the action is done in default. But sometimes it is bad to maintain flags with negative meanings. And it will be painful if we change the default value for the flag. As this implementation, it keeps all existing functionality. So it is compatible with old versions. --- context.go | 18 ++++++++++++++++++ context_test.go | 7 +++++++ flag.go | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/context.go b/context.go index d142442..dbd910f 100644 --- a/context.go +++ b/context.go @@ -38,6 +38,11 @@ func (c *Context) Bool(name string) bool { return lookupBool(name, c.flagSet) } +// Looks up the value of a local boolT flag, returns false if no bool flag exists +func (c *Context) BoolT(name string) bool { + return lookupBoolT(name, c.flagSet) +} + // Looks up the value of a local string flag, returns "" if no string flag exists func (c *Context) String(name string) string { return lookupString(name, c.flagSet) @@ -192,6 +197,19 @@ func lookupBool(name string, set *flag.FlagSet) bool { return false } +func lookupBoolT(name string, set *flag.FlagSet) bool { + f := set.Lookup(name) + if f != nil { + val, err := strconv.ParseBool(f.Value.String()) + if err != nil { + return true + } + return val + } + + return false +} + func normalizeFlags(flags []Flag, set *flag.FlagSet) error { visited := make(map[string]bool) set.Visit(func(f *flag.Flag) { diff --git a/context_test.go b/context_test.go index 63f2a70..75fdcd8 100644 --- a/context_test.go +++ b/context_test.go @@ -37,6 +37,13 @@ func TestContext_Bool(t *testing.T) { expect(t, c.Bool("myflag"), false) } +func TestContext_BoolT(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", true, "doc") + c := cli.NewContext(nil, set, set) + expect(t, c.BoolT("myflag"), true) +} + func TestContext_Args(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc") diff --git a/flag.go b/flag.go index 38c8c1c..d0af858 100644 --- a/flag.go +++ b/flag.go @@ -131,6 +131,25 @@ func (f BoolFlag) getName() string { return f.Name } +type BoolTFlag struct { + Name string + Usage string +} + +func (f BoolTFlag) String() string { + return fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage) +} + +func (f BoolTFlag) Apply(set *flag.FlagSet) { + eachName(f.Name, func(name string) { + set.Bool(name, true, f.Usage) + }) +} + +func (f BoolTFlag) getName() string { + return f.Name +} + type StringFlag struct { Name string Value string