func (a *Application) Parse(args []string) (command string, err error) {
	context, parseErr := a.ParseContext(args)
	selected := []string{}
	var setValuesErr error
	if context == nil {
		return "", parseErr
	}
	if err = a.setDefaults(context); err != nil {
		return "", err
	}
	selected, setValuesErr = a.setValues(context)
	if err = a.applyPreActions(context, !a.completion); err != nil {
		return "", err
	}
	if a.completion {
		a.generateBashCompletion(context)
		a.terminate(0)
	} else {
		if parseErr != nil {
			return "", parseErr
		}
		a.maybeHelp(context)
		if !context.EOL() {
			return "", fmt.Errorf("unexpected argument '%s'", context.Peek())
		}
		if setValuesErr != nil {
			return "", setValuesErr
		}
		command, err = a.execute(context, selected)
		if err == ErrCommandNotSpecified {
			a.writeUsage(context, nil)
		}
	}
	return command, err
}
Parse parses command-line arguments. It returns the selected command and an
error. The selected command will be a space separated subcommand, if
subcommands have been configured.

This will populate all flag and argument values, call all callbacks, and so
on.
package kingpin
import (
	"fmt"
	"io"
	"os"
	"regexp"
	"strings"
)
var (
	ErrCommandNotSpecified = fmt.Errorf("command not specified")
)
var (
	envarTransformRegexp = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
)
type ApplicationValidator func(*Application) error
type Application struct {
	cmdMixin
	initialized bool
	Name string
	Help string
	author         string
	version        string
	errorWriter    io.Writer  
	usageWriter    io.Writer  
	usageTemplate  string
	validator      ApplicationValidator
	terminate      func(status int)  
	noInterspersed bool              
	defaultEnvars  bool
	completion     bool
	HelpFlag *FlagClause
	HelpCommand *CmdClause
	VersionFlag *FlagClause
}
func New(name, help string) *Application {
	a := &Application{
		Name:          name,
		Help:          help,
		errorWriter:   os.Stderr,  
		usageWriter:   os.Stderr,
		usageTemplate: DefaultUsageTemplate,
		terminate:     os.Exit,
	}
	a.flagGroup = newFlagGroup()
	a.argGroup = newArgGroup()
	a.cmdGroup = newCmdGroup(a)
	a.HelpFlag = a.Flag("help", "Show context-sensitive help (also try --help-long and --help-man).")
	a.HelpFlag.Bool()
	a.Flag("help-long", "Generate long help.").Hidden().PreAction(a.generateLongHelp).Bool()
	a.Flag("help-man", "Generate a man page.").Hidden().PreAction(a.generateManPage).Bool()
	a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().BoolVar(&a.completion)
	a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).Bool()
	a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).Bool()
	return a
}
func (a *Application) generateLongHelp(c *ParseContext) error {
	a.Writer(os.Stdout)
	if err := a.UsageForContextWithTemplate(c, 2, LongHelpTemplate); err != nil {
		return err
	}
	a.terminate(0)
	return nil
}
func (a *Application) generateManPage(c *ParseContext) error {
	a.Writer(os.Stdout)
	if err := a.UsageForContextWithTemplate(c, 2, ManPageTemplate); err != nil {
		return err
	}
	a.terminate(0)
	return nil
}
func (a *Application) generateBashCompletionScript(c *ParseContext) error {
	a.Writer(os.Stdout)
	if err := a.UsageForContextWithTemplate(c, 2, BashCompletionTemplate); err != nil {
		return err
	}
	a.terminate(0)
	return nil
}
func (a *Application) generateZSHCompletionScript(c *ParseContext) error {
	a.Writer(os.Stdout)
	if err := a.UsageForContextWithTemplate(c, 2, ZshCompletionTemplate); err != nil {
		return err
	}
	a.terminate(0)
	return nil
}
func (a *Application) DefaultEnvars() *Application {
	a.defaultEnvars = true
	return a
}
func (a *Application) Terminate(terminate func(int)) *Application {
	if terminate == nil {
		terminate = func(int) {}
	}
	a.terminate = terminate
	return a
}
func (a *Application) Writer(w io.Writer) *Application {
	a.errorWriter = w
	a.usageWriter = w
	return a
}
func (a *Application) ErrorWriter(w io.Writer) *Application {
	a.errorWriter = w
	return a
}
func (a *Application) UsageWriter(w io.Writer) *Application {
	a.usageWriter = w
	return a
}
func (a *Application) UsageTemplate(template string) *Application {
	a.usageTemplate = template
	return a
}
func (a *Application) Validate(validator ApplicationValidator) *Application {
	a.validator = validator
	return a
}
func (a *Application) ParseContext(args []string) (*ParseContext, error) {
	return a.parseContext(false, args)
}
func (a *Application) parseContext(ignoreDefault bool, args []string) (*ParseContext, error) {
	if err := a.init(); err != nil {
		return nil, err
	}
	context := tokenize(args, ignoreDefault)
	err := parse(context, a)
	return context, err
}