Add aliases index and subcommand transcription

pull/2/head
rwxrob 2 years ago
parent e6ee287bae
commit 906a27e5d5
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -49,6 +49,23 @@ type Cmd struct {
Hidden []string `json:"hide,omitempty"`
Completer comp.Completer `json:"-"`
Call Method `json:"-"`
_aliases map[string]*Cmd
}
func (x *Cmd) cacheAliases() {
x._aliases = map[string]*Cmd{}
if x.Commands == nil {
return
}
for _, c := range x.Commands {
if c.Aliases == nil {
continue
}
for _, a := range c.Aliases {
x._aliases[a] = c
}
}
}
// Run is for running a command within a specific runtime (shell) and
@ -63,6 +80,8 @@ type Cmd struct {
// others such a zsh and shell-less REPLs are planned.
func (x *Cmd) Run() {
x.cacheAliases()
// bash completion context
line := os.Getenv("COMP_LINE")
if line != "" {
@ -116,6 +135,7 @@ func (x *Cmd) Add(name string, aliases ...string) *Cmd {
return c
}
// Cmd looks up a given Command by name or name from Aliases.
func (x *Cmd) Cmd(name string) *Cmd {
if x.Commands == nil {
return nil
@ -125,6 +145,9 @@ func (x *Cmd) Cmd(name string) *Cmd {
return c
}
}
if c, has := x._aliases[name]; has {
return c
}
return nil
}

Loading…
Cancel
Save