From b83ace34b5933e0f06ede0ed87abee0d6e1fe2cc Mon Sep 17 00:00:00 2001 From: Jose Luis Tallon Date: Mon, 15 Aug 2016 20:16:03 +0200 Subject: [PATCH] Add the ability to tell which of the aliases was the Command actually called with Introduces a CalledName() method on Command (plus a private member in Command) which stores the alias that the command was originally called as (or Name() if it was the "Use" name) --- command.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index 083e4ea7..97884ca4 100644 --- a/command.go +++ b/command.go @@ -96,6 +96,8 @@ type Command struct { PersistentPostRunE func(cmd *Command, args []string) error // DisableAutoGenTag remove DisableAutoGenTag bool + // Contains the 'calledName' rather than the alias that matched + calledName string // Commands is the list of commands supported by this program. commands []*Command // Parent Command for this command @@ -431,7 +433,11 @@ func (c *Command) Find(args []string) (*Command, []string, error) { nextSubCmd := argsWOflags[0] matches := make([]*Command, 0) for _, cmd := range c.commands { - if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match + if cmd.Name() == nextSubCmd { // exact name + return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd)) + } + if cmd.HasAlias(nextSubCmd) { // alias match + cmd.calledName = nextSubCmd return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd)) } if EnablePrefixMatching { @@ -926,6 +932,14 @@ func (c *Command) Name() string { return name } +// CalledName returns how this command was called as (rather than its name) +func (c *Command) CalledName() string { + if "" == c.calledName { + return c.Name() + } + return c.calledName +} + // HasAlias determines if a given string is an alias of the command. func (c *Command) HasAlias(s string) bool { for _, a := range c.Aliases {