diff --git a/context.go b/context.go index 0c7a07e78d..0335849a1b 100644 --- a/context.go +++ b/context.go @@ -105,13 +105,11 @@ func (cCtx *Context) Lineage() []*Context { return lineage } -// NumOccurrences returns the num of occurences of this flag +// Count returns the num of occurences of this flag func (cCtx *Context) Count(name string) int { if fs := cCtx.lookupFlagSet(name); fs != nil { - if bf, ok := fs.Lookup(name).Value.(*boolValue); ok { - if bf.count != nil { - return *bf.count - } + if cf, ok := fs.Lookup(name).Value.(Countable); ok { + return cf.Count() } } return 0 diff --git a/flag.go b/flag.go index 37c9fdab21..050bb4b1d1 100644 --- a/flag.go +++ b/flag.go @@ -139,6 +139,12 @@ type CategorizableFlag interface { GetCategory() string } +// Countable is an interface to enable detection of flag values which support +// repetitive flags +type Countable interface { + Count() int +} + func flagSet(name string, flags []Flag) (*flag.FlagSet, error) { set := flag.NewFlagSet(name, flag.ContinueOnError) diff --git a/flag_bool.go b/flag_bool.go index aad26cc556..cb937ae65d 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -51,6 +51,13 @@ func (b *boolValue) String() string { func (b *boolValue) IsBoolFlag() bool { return true } +func (b *boolValue) Count() int { + if b.count != nil { + return *b.count + } + return 0 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f *BoolFlag) TakesValue() bool { return false diff --git a/godoc-current.txt b/godoc-current.txt index 382d4f72ea..12db90eea3 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -634,7 +634,7 @@ func (cCtx *Context) Bool(name string) bool Bool looks up the value of a local BoolFlag, returns false if not found func (cCtx *Context) Count(name string) int - NumOccurrences returns the num of occurences of this flag + Count returns the num of occurences of this flag func (cCtx *Context) Duration(name string) time.Duration Duration looks up the value of a local DurationFlag, returns 0 if not found @@ -708,6 +708,12 @@ func (cCtx *Context) Uint64(name string) uint64 func (cCtx *Context) Value(name string) interface{} Value returns the value of the flag corresponding to `name` +type Countable interface { + Count() int +} + Countable is an interface to enable detection of flag values which support + repetitive flags + type DocGenerationFlag interface { Flag