Add ability to cast a command to a commander
This commit is contained in:
parent
88e482c582
commit
74ce44d76e
23
command.go
23
command.go
@ -52,6 +52,29 @@ type Command struct {
|
|||||||
flagErrorBuf *bytes.Buffer
|
flagErrorBuf *bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert a Command into an (initialized) Commander
|
||||||
|
func (cmd *Command) ToCommander() (c *Commander) {
|
||||||
|
c = NewCommander()
|
||||||
|
c.name = cmd.Name()
|
||||||
|
c.Use = cmd.Use
|
||||||
|
c.Short = cmd.Short
|
||||||
|
c.Long = cmd.Long
|
||||||
|
c.flags = cmd.flags
|
||||||
|
c.pflags = cmd.pflags
|
||||||
|
c.Run = cmd.Run
|
||||||
|
c.commands = cmd.commands
|
||||||
|
c.resetChildrensParents()
|
||||||
|
c.flagErrorBuf = cmd.flagErrorBuf
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Really only used when casting a command to a commander
|
||||||
|
func (c *Command) resetChildrensParents() {
|
||||||
|
for _, x := range c.commands {
|
||||||
|
x.parent = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// find the target command given the args and command tree
|
// find the target command given the args and command tree
|
||||||
// Meant to be run on the highest node. Only searches down.
|
// Meant to be run on the highest node. Only searches down.
|
||||||
func (c *Command) Find(args []string) (cmd *Command, a []string, err error) {
|
func (c *Command) Find(args []string) (cmd *Command, a []string, err error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user