From 74ce44d76e1d7ac0dbc5e91ddf79644338b9c580 Mon Sep 17 00:00:00 2001 From: spf13 Date: Sun, 29 Sep 2013 01:57:50 -0400 Subject: [PATCH] Add ability to cast a command to a commander --- command.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/command.go b/command.go index e15476a..7c3a0a9 100644 --- a/command.go +++ b/command.go @@ -52,6 +52,29 @@ type Command struct { 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 // Meant to be run on the highest node. Only searches down. func (c *Command) Find(args []string) (cmd *Command, a []string, err error) {