Add command aliases
diff --git a/command.go b/command.go
index 47a7e4d..9bdac58 100644
--- a/command.go
+++ b/command.go
@@ -17,6 +17,9 @@
// Whether subcommands are optional
SubcommandsOptional bool
+ // Aliases for the command
+ Aliases []string
+
commands []*Command
hasBuiltinHelpGroup bool
}
@@ -78,7 +81,7 @@
// command can be found Find will return nil.
func (c *Command) Find(name string) *Command {
for _, cc := range c.commands {
- if cc.Name == name {
+ if cc.match(name) {
return cc
}
}
diff --git a/command_private.go b/command_private.go
index 83c04b0..bd97cc7 100644
--- a/command_private.go
+++ b/command_private.go
@@ -38,6 +38,7 @@
shortDescription := mtag.Get("description")
longDescription := mtag.Get("long-description")
subcommandsOptional := mtag.Get("subcommands-optional")
+ aliases := mtag.GetMany("alias")
subc, err := c.AddCommand(subcommand, shortDescription, longDescription, ptrval.Interface())
@@ -49,6 +50,10 @@
subc.SubcommandsOptional = true
}
+ if len(aliases) > 0 {
+ subc.Aliases = aliases
+ }
+
return true, nil
}
@@ -122,6 +127,10 @@
for _, subcommand := range c.commands {
ret.commands[subcommand.Name] = subcommand
+
+ for _, a := range subcommand.Aliases {
+ ret.commands[a] = subcommand
+ }
}
return ret
@@ -169,6 +178,20 @@
return []*Command(ret)
}
+func (c *Command) match(name string) bool {
+ if c.Name == name {
+ return true
+ }
+
+ for _, v := range c.Aliases {
+ if v == name {
+ return true
+ }
+ }
+
+ return false
+}
+
func (c *Command) hasCliOptions() bool {
ret := false
diff --git a/command_test.go b/command_test.go
index aa3a10e..b2df7e3 100644
--- a/command_test.go
+++ b/command_test.go
@@ -320,3 +320,17 @@
t.Errorf("Expected Value to be true")
}
}
+
+func TestCommandAlias(t *testing.T) {
+ var opts = struct {
+ Command struct {
+ G bool `short:"g" default:"true"`
+ } `command:"cmd" alias:"cm"`
+ }{}
+
+ assertParseSuccess(t, &opts, "cm")
+
+ if !opts.Command.G {
+ t.Errorf("Expected G to be true")
+ }
+}
diff --git a/help.go b/help.go
index f708a1f..4ded286 100644
--- a/help.go
+++ b/help.go
@@ -324,6 +324,11 @@
if len(c.ShortDescription) > 0 {
pad := strings.Repeat(" ", maxnamelen-len(c.Name))
fmt.Fprintf(wr, "%s %s", pad, c.ShortDescription)
+
+ if len(c.Aliases) > 0 {
+ fmt.Fprintf(wr, " (aliases: %s)", strings.Join(c.Aliases, ", "))
+ }
+
}
fmt.Fprintln(wr)
diff --git a/help_test.go b/help_test.go
index c0566cd..e3e84d8 100644
--- a/help_test.go
+++ b/help_test.go
@@ -59,7 +59,7 @@
Command struct {
ExtraVerbose []bool `long:"extra-verbose" description:"Use for extra verbosity"`
- } `command:"command" description:"A command"`
+ } `command:"command" alias:"cm" alias:"cmd" description:"A command"`
}
func TestHelp(t *testing.T) {
@@ -101,7 +101,7 @@
-h, --help Show this help message
Available commands:
- command A command
+ command A command (aliases: cm, cmd)
`
if e.Message != expected {
@@ -173,6 +173,9 @@
A command
Longer \fBcommand\fP description
+
+\fBAliases\fP: cm, cmd
+
.TP
\fB--extra-verbose\fP
Use for extra verbosity
diff --git a/man.go b/man.go
index cfe8037..9e1f036 100644
--- a/man.go
+++ b/man.go
@@ -98,6 +98,10 @@
}
}
+ if len(command.Aliases) > 0 {
+ fmt.Fprintf(wr, "\n\\fBAliases\\fP: %s\n\n", strings.Join(command.Aliases, ", "))
+ }
+
writeManPageOptions(wr, command.Group)
writeManPageSubcommands(wr, name, command)
}