diff options
| author | Charlie R.C <[email protected]> | 2017-05-05 00:19:47 -0500 |
|---|---|---|
| committer | Albert Nigmatzianov <[email protected]> | 2017-05-05 07:19:47 +0200 |
| commit | a84f757ed2ae26fca9fdb8d84d6fffa605b63fae (patch) | |
| tree | 24dfd1ef767c8c1b058b6928b53f2c466a89ba02 /flag.go | |
| parent | c990990ab4981d84da820b7b00c85139ca150b5f (diff) | |
Allow lookup by shorthand (#106)
Diffstat (limited to 'flag.go')
| -rw-r--r-- | flag.go | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -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) |
