func (a *App) RunAsSubcommand(ctx *Context) (err error) {
	if len(a.Commands) > 0 {
		if a.Command(helpCommand.Name) == nil && !a.HideHelp {
			a.Commands = append(a.Commands, helpCommand)
			if (HelpFlag != BoolFlag{}) {
				a.appendFlag(HelpFlag)
			}
		}
	}
	newCmds := []Command{}
	for _, c := range a.Commands {
		if c.HelpName == "" {
			c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
		}
		newCmds = append(newCmds, c)
	}
	a.Commands = newCmds
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(ctx.Args().Tail())
	nerr := normalizeFlags(a.Flags, set)
	context := NewContext(a, set, ctx)
	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		fmt.Fprintln(a.Writer)
		if len(a.Commands) > 0 {
			ShowSubcommandHelp(context)
		} else {
			ShowCommandHelp(ctx, context.Args().First())
		}
		return nerr
	}
	if checkCompletions(context) {
		return nil
	}
	if err != nil {
		if a.OnUsageError != nil {
			err = a.OnUsageError(context, err, true)
			HandleExitCoder(err)
			return err
		} else {
			fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
			ShowSubcommandHelp(context)
			return err
		}
	}
	if len(a.Commands) > 0 {
		if checkSubcommandHelp(context) {
			return nil
		}
	} else {
		if checkCommandHelp(ctx, context.Args().First()) {
			return nil
		}
	}
	if a.After != nil {
		defer func() {
			afterErr := a.After(context)
			if afterErr != nil {
				HandleExitCoder(err)
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}
	if a.Before != nil {
		beforeErr := a.Before(context)
		if beforeErr != nil {
			HandleExitCoder(beforeErr)
			err = beforeErr
			return err
		}
	}
	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}
	err = HandleAction(a.Action, context)
	HandleExitCoder(err)
	return err
}
Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
package cli
import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"sort"
	"time"
)
var (
	changeLogURL                    = "https://github.com/codegangsta/cli/blob/master/CHANGELOG.md"
	appActionDeprecationURL         = fmt.Sprintf("%s#deprecated-cli-app-action-signature", changeLogURL)
	runAndExitOnErrorDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-runandexitonerror", changeLogURL)
	contactSysadmin = "This is an error in the application.  Please contact the distributor of this application if this is not you."
	errNonFuncAction = NewExitError("ERROR invalid Action type.  "+
		fmt.Sprintf("Must be a func of type `cli.ActionFunc`.  %s", contactSysadmin)+
		fmt.Sprintf("See %s", appActionDeprecationURL), 2)
	errInvalidActionSignature = NewExitError("ERROR invalid Action signature.  "+
		fmt.Sprintf("Must be `cli.ActionFunc`.  %s", contactSysadmin)+
		fmt.Sprintf("See %s", appActionDeprecationURL), 2)
)
type App struct {
	Name string
	HelpName string
	Usage string
	UsageText string
	ArgsUsage string
	Version string
	Commands []Command
	Flags []Flag
	EnableBashCompletion bool
	HideHelp bool
	HideVersion bool
	categories CommandCategories
	BashComplete BashCompleteFunc
	Before BeforeFunc
	After AfterFunc
	Action interface{}
	CommandNotFound CommandNotFoundFunc
	OnUsageError OnUsageErrorFunc
	Compiled time.Time
	Authors []Author
	Copyright string
	Author string
	Email string
	Writer io.Writer
	Metadata map[string]interface{}
}
func compileTime() time.Time {
	info, err := os.Stat(os.Args[0])
	if err != nil {
		return time.Now()
	}
	return info.ModTime()
}
func NewApp() *App {
	return &App{
		Name:         filepath.Base(os.Args[0]),
		HelpName:     filepath.Base(os.Args[0]),
		Usage:        "A new cli application",
		UsageText:    "",
		Version:      "0.0.0",
		BashComplete: DefaultAppComplete,
		Action:       helpCommand.Action,
		Compiled:     compileTime(),
		Writer:       os.Stdout,
	}
}
func (a *App) Run(arguments []string) (err error) {
	if a.Author != "" || a.Email != "" {
		a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
	}
	newCmds := []Command{}
	for _, c := range a.Commands {
		if c.HelpName == "" {
			c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
		}
		newCmds = append(newCmds, c)
	}
	a.Commands = newCmds
	a.categories = CommandCategories{}
	for _, command := range a.Commands {
		a.categories = a.categories.AddCommand(command.Category, command)
	}
	sort.Sort(a.categories)
	if a.Command(helpCommand.Name) == nil && !a.HideHelp {
		a.Commands = append(a.Commands, helpCommand)
		if (HelpFlag != BoolFlag{}) {
			a.appendFlag(HelpFlag)
		}
	}
	if a.EnableBashCompletion {
		a.appendFlag(BashCompletionFlag)
	}
	if !a.HideVersion {
		a.appendFlag(VersionFlag)
	}
	set := flagSet(a.Name, a.Flags)
	set.SetOutput(ioutil.Discard)
	err = set.Parse(arguments[1:])
	nerr := normalizeFlags(a.Flags, set)
	context := NewContext(a, set, nil)
	if nerr != nil {
		fmt.Fprintln(a.Writer, nerr)
		ShowAppHelp(context)
		return nerr
	}
	if checkCompletions(context) {
		return nil
	}
	if err != nil {
		if a.OnUsageError != nil {
			err := a.OnUsageError(context, err, false)
			HandleExitCoder(err)
			return err
		} else {
			fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
			ShowAppHelp(context)
			return err
		}
	}
	if !a.HideHelp && checkHelp(context) {
		ShowAppHelp(context)
		return nil
	}
	if !a.HideVersion && checkVersion(context) {
		ShowVersion(context)
		return nil
	}
	if a.After != nil {
		defer func() {
			if afterErr := a.After(context); afterErr != nil {
				if err != nil {
					err = NewMultiError(err, afterErr)
				} else {
					err = afterErr
				}
			}
		}()
	}
	if a.Before != nil {
		beforeErr := a.Before(context)
		if beforeErr != nil {
			fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
			ShowAppHelp(context)
			HandleExitCoder(beforeErr)
			err = beforeErr
			return err
		}
	}
	args := context.Args()
	if args.Present() {
		name := args.First()
		c := a.Command(name)
		if c != nil {
			return c.Run(context)
		}
	}
	err = HandleAction(a.Action, context)
	HandleExitCoder(err)
	return err
}
func (a *App) RunAndExitOnError() {
	fmt.Fprintf(os.Stderr,
		"DEPRECATED cli.App.RunAndExitOnError.  %s  See %s\n",
		contactSysadmin, runAndExitOnErrorDeprecationURL)
	if err := a.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		OsExiter(1)
	}
}