func (c *Command) Root() *Command {
	var findRoot func(*Command) *Command
	findRoot = func(x *Command) *Command {
		if x.HasParent() {
			return findRoot(x.parent)
		}
		return x
	}
	return findRoot(c)
}
Root finds root command.
package cobra
import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strings"
	flag "github.com/spf13/pflag"
)
type Command struct {
	name string
	Use string
	Aliases []string
	SuggestFor []string
	Short string
	Long string
	Example string
	ValidArgs []string
	ArgAliases []string
	BashCompletionFunction string
	Deprecated string
	Hidden bool
	Annotations map[string]string
	flags *flag.FlagSet
	pflags *flag.FlagSet
	lflags *flag.FlagSet
	SilenceErrors bool
	SilenceUsage bool
	PersistentPreRun func(cmd *Command, args []string)
	PersistentPreRunE func(cmd *Command, args []string) error
	PreRun func(cmd *Command, args []string)
	PreRunE func(cmd *Command, args []string) error
	Run func(cmd *Command, args []string)
	RunE func(cmd *Command, args []string) error
	PostRun func(cmd *Command, args []string)
	PostRunE func(cmd *Command, args []string) error
	PersistentPostRun func(cmd *Command, args []string)
	PersistentPostRunE func(cmd *Command, args []string) error
	DisableAutoGenTag bool
	commands []*Command
	parent *Command
	commandsMaxUseLen         int
	commandsMaxCommandPathLen int
	commandsMaxNameLen        int
	commandsAreSorted bool
	flagErrorBuf *bytes.Buffer
	args          []string              
	output        *io.Writer            
	usageFunc     func(*Command) error  
	usageTemplate string                
	flagErrorFunc func(*Command, error) error
	helpTemplate  string                    
	helpFunc      func(*Command, []string)  
	helpCommand   *Command                  
	globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
	DisableSuggestions bool
	SuggestionsMinimumDistance int
	DisableFlagParsing bool
}
func (c *Command) SetArgs(a []string) {
	c.args = a
}
func (c *Command) SetOutput(output io.Writer) {
	c.output = &output
}
func (c *Command) SetUsageFunc(f func(*Command) error) {
	c.usageFunc = f
}
func (c *Command) SetUsageTemplate(s string) {
	c.usageTemplate = s
}
func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
	c.flagErrorFunc = f
}
func (c *Command) SetHelpFunc(f func(*Command, []string)) {
	c.helpFunc = f
}
func (c *Command) SetHelpCommand(cmd *Command) {
	c.helpCommand = cmd
}
func (c *Command) SetHelpTemplate(s string) {
	c.helpTemplate = s
}
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
	c.Flags().SetNormalizeFunc(n)
	c.PersistentFlags().SetNormalizeFunc(n)
	c.globNormFunc = n
	for _, command := range c.commands {
		command.SetGlobalNormalizationFunc(n)
	}
}
func (c *Command) OutOrStdout() io.Writer {
	return c.getOut(os.Stdout)
}
func (c *Command) OutOrStderr() io.Writer {
	return c.getOut(os.Stderr)
}
func (c *Command) getOut(def io.Writer) io.Writer {
	if c.output != nil {
		return *c.output
	}
	if c.HasParent() {
		return c.parent.getOut(def)
	}
	return def
}
func (c *Command) UsageFunc() (f func(*Command) error) {
	if c.usageFunc != nil {
		return c.usageFunc
	}
	if c.HasParent() {
		return c.parent.UsageFunc()
	}
	return func(c *Command) error {
		c.mergePersistentFlags()
		err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
		if err != nil {
			c.Println(err)
		}
		return err
	}
}
func (c *Command) Usage() error {
	return c.UsageFunc()(c)
}
func (c *Command) HelpFunc() func(*Command, []string) {
	if helpFunc := c.checkHelpFunc(); helpFunc != nil {
		return helpFunc
	}
	return func(*Command, []string) {
		c.mergePersistentFlags()
		err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
		if err != nil {
			c.Println(err)
		}
	}
}
func (c *Command) checkHelpFunc() func(*Command, []string) {
	if c == nil {
		return nil
	}
	if c.helpFunc != nil {
		return c.helpFunc
	}
	if c.HasParent() {
		return c.parent.checkHelpFunc()
	}
	return nil
}
func (c *Command) Help() error {
	c.HelpFunc()(c, []string{})
	return nil
}
func (c *Command) UsageString() string {
	tmpOutput := c.output
	bb := new(bytes.Buffer)
	c.SetOutput(bb)
	c.Usage()
	c.output = tmpOutput
	return bb.String()
}
func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {
	if c.flagErrorFunc != nil {
		return c.flagErrorFunc
	}
	if c.HasParent() {
		return c.parent.FlagErrorFunc()
	}
	return func(c *Command, err error) error {
		return err
	}
}
var minUsagePadding = 25
func (c *Command) UsagePadding() int {
	if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
		return minUsagePadding
	}
	return c.parent.commandsMaxUseLen
}
var minCommandPathPadding = 11
func (c *Command) CommandPathPadding() int {
	if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
		return minCommandPathPadding
	}
	return c.parent.commandsMaxCommandPathLen
}
var minNamePadding = 11
func (c *Command) NamePadding() int {
	if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
		return minNamePadding
	}
	return c.parent.commandsMaxNameLen
}
func (c *Command) UsageTemplate() string {
	if c.usageTemplate != "" {
		return c.usageTemplate
	}
	if c.HasParent() {
		return c.parent.UsageTemplate()
	}
	return `Usage:{{if .Runnable}}
  {{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
  {{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
Aliases:
  {{.NameAndAliases}}
{{end}}{{if .HasExample}}
Examples:
{{ .Example }}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
  {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
  {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}
func (c *Command) HelpTemplate() string {
	if c.helpTemplate != "" {
		return c.helpTemplate
	}
	if c.HasParent() {
		return c.parent.HelpTemplate()
	}
	return `{{with or .Long .Short }}{{. | trim}}
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
}
func (c *Command) resetChildrensParents() {
	for _, x := range c.commands {
		x.parent = c
	}
}
func hasNoOptDefVal(name string, f *flag.FlagSet) bool {
	flag := f.Lookup(name)
	if flag == nil {
		return false
	}
	return len(flag.NoOptDefVal) > 0
}
func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
	result := false
	fs.VisitAll(func(flag *flag.Flag) {
		if flag.Shorthand == name && len(flag.NoOptDefVal) > 0 {
			result = true
		}
	})
	return result
}
func stripFlags(args []string, c *Command) []string {
	if len(args) < 1 {
		return args
	}
	c.mergePersistentFlags()
	commands := []string{}
	inQuote := false
	inFlag := false
	for _, y := range args {
		if !inQuote {
			switch {
			case strings.HasPrefix(y, "\""):
				inQuote = true
			case strings.Contains(y, "=\""):
				inQuote = true
			case strings.HasPrefix(y, "--") && !strings.Contains(y, "="):
				inFlag = !hasNoOptDefVal(y[2:], c.Flags())
			case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !shortHasNoOptDefVal(y[1:], c.Flags()):
				inFlag = true
			case inFlag:
				inFlag = false
			case y == "":
			case !strings.HasPrefix(y, "-"):
				commands = append(commands, y)
				inFlag = false
			}
		}
		if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") {
			inQuote = false
		}
	}
	return commands
}
func argsMinusFirstX(args []string, x string) []string {
	for i, y := range args {
		if x == y {
			ret := []string{}
			ret = append(ret, args[:i]...)
			ret = append(ret, args[i+1:]...)
			return ret
		}
	}
	return args
}
func (c *Command) Find(args []string) (*Command, []string, error) {
	if c == nil {
		return nil, nil, fmt.Errorf("Called find() on a nil Command")
	}
	var innerfind func(*Command, []string) (*Command, []string)
	innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
		argsWOflags := stripFlags(innerArgs, c)
		if len(argsWOflags) == 0 {
			return c, innerArgs
		}
		nextSubCmd := argsWOflags[0]
		matches := make([]*Command, 0)
		for _, cmd := range c.commands {
			if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) {  
				return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
			}
			if EnablePrefixMatching {
				if strings.HasPrefix(cmd.Name(), nextSubCmd) {  
					matches = append(matches, cmd)
				}
				for _, x := range cmd.Aliases {
					if strings.HasPrefix(x, nextSubCmd) {
						matches = append(matches, cmd)
					}
				}
			}
		}
		if len(matches) == 1 {
			return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
		}
		return c, innerArgs
	}
	commandFound, a := innerfind(c, args)
	argsWOflags := stripFlags(a, commandFound)
	if !commandFound.HasSubCommands() {
		return commandFound, a, nil
	}
	if commandFound == c && len(argsWOflags) > 0 {
		suggestionsString := ""
		if !c.DisableSuggestions {
			if c.SuggestionsMinimumDistance <= 0 {
				c.SuggestionsMinimumDistance = 2
			}
			if suggestions := c.SuggestionsFor(argsWOflags[0]); len(suggestions) > 0 {
				suggestionsString += "\n\nDid you mean this?\n"
				for _, s := range suggestions {
					suggestionsString += fmt.Sprintf("\t%v\n", s)
				}
			}
		}
		return commandFound, a, fmt.Errorf("unknown command %q for %q%s", argsWOflags[0], commandFound.CommandPath(), suggestionsString)
	}
	return commandFound, a, nil
}
func (c *Command) SuggestionsFor(typedName string) []string {
	suggestions := []string{}
	for _, cmd := range c.commands {
		if cmd.IsAvailableCommand() {
			levenshteinDistance := ld(typedName, cmd.Name(), true)
			suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
			suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
			if suggestByLevenshtein || suggestByPrefix {
				suggestions = append(suggestions, cmd.Name())
			}
			for _, explicitSuggestion := range cmd.SuggestFor {
				if strings.EqualFold(typedName, explicitSuggestion) {
					suggestions = append(suggestions, cmd.Name())
				}
			}
		}
	}
	return suggestions
}
func (c *Command) VisitParents(fn func(*Command)) {
	var traverse func(*Command) *Command
	traverse = func(x *Command) *Command {
		if x != c {
			fn(x)
		}
		if x.HasParent() {
			return traverse(x.parent)
		}
		return x
	}
	traverse(c)
}