From a84f757ed2ae26fca9fdb8d84d6fffa605b63fae Mon Sep 17 00:00:00 2001 From: "Charlie R.C" Date: Fri, 5 May 2017 00:19:47 -0500 Subject: Allow lookup by shorthand (#106) --- flag.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'flag.go') diff --git a/flag.go b/flag.go index f9984fb..6ea0c48 100644 --- a/flag.go +++ b/flag.go @@ -319,6 +319,18 @@ func (f *FlagSet) Lookup(name string) *Flag { return f.lookup(f.normalizeFlagName(name)) } +// ShorthandLookup returns the Flag structure of the short handed flag, returning nil if none exists. +func (f *FlagSet) ShorthandLookup(name string) *Flag { + if name == "" { + return nil + } + if len(name) > 1 { + panic("can't look up for a shorthand with name more than one character") + } + c := name[0] + return f.shorthands[c] +} + // lookup returns the Flag structure of the named flag, returning nil if none exists. func (f *FlagSet) lookup(name NormalizedName) *Flag { return f.formal[name] @@ -399,6 +411,12 @@ func Lookup(name string) *Flag { return CommandLine.Lookup(name) } +// ShorthandLookup returns the Flag structure of the short handed flag, +// returning nil if none exists. +func ShorthandLookup(name string) *Flag { + return CommandLine.ShorthandLookup(name) +} + // Set sets the value of the named flag. func (f *FlagSet) Set(name, value string) error { normalName := f.normalizeFlagName(name) -- cgit v1.2.3