aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--flag.go18
-rw-r--r--flag_test.go3
2 files changed, 21 insertions, 0 deletions
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)
diff --git a/flag_test.go b/flag_test.go
index 172d49f..8248277 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -936,6 +936,7 @@ const defaultOutput = ` --A for bootstrapping, allo
--Alongflagname disable bounds checking
-C, --CCC a boolean defaulting to true (default true)
--D path set relative path for local imports
+ -E, --EEE num[=1234] a num with NoOptDefVal (default 4321)
--F number a non-zero number (default 2.7)
--G float a float that defaults to zero
--IP ip IP address with no default
@@ -987,6 +988,8 @@ func TestPrintDefaults(t *testing.T) {
fs.Lookup("ND1").NoOptDefVal = "bar"
fs.Int("ND2", 1234, "a `num` with NoOptDefVal")
fs.Lookup("ND2").NoOptDefVal = "4321"
+ fs.IntP("EEE", "E", 4321, "a `num` with NoOptDefVal")
+ fs.ShorthandLookup("E").NoOptDefVal = "1234"
fs.StringSlice("StringSlice", []string{}, "string slice with zero default")
fs.StringArray("StringArray", []string{}, "string array with zero default")