This commit is contained in:
Prashanth Balasubramanian 2016-04-17 13:19:22 -07:00
parent d3a51031c3
commit 198a319fd6
1361 changed files with 239071 additions and 28102 deletions

View file

@ -21,11 +21,8 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/inconshreveable/mousetrap"
flag "github.com/spf13/pflag"
)
@ -48,8 +45,11 @@ type Command struct {
Long string
// Examples of how to use the command
Example string
// List of all valid non-flag arguments, used for bash completions *TODO* actually validate these
// List of all valid non-flag arguments that are accepted in bash completions
ValidArgs []string
// List of aliases for ValidArgs. These are not suggested to the user in the bash
// completion, but accepted if entered manually.
ArgAliases []string
// Custom functions used by the bash autocompletion generator
BashCompletionFunction string
// Is this command deprecated and should print this string when used?
@ -135,9 +135,8 @@ func (c *Command) getOut(def io.Writer) io.Writer {
if c.HasParent() {
return c.parent.Out()
} else {
return def
}
return def
}
func (c *Command) Out() io.Writer {
@ -197,14 +196,13 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
if c.HasParent() {
return c.parent.UsageFunc()
} else {
return func(c *Command) error {
err := tmpl(c.Out(), c.UsageTemplate(), c)
if err != nil {
fmt.Print(err)
}
return err
}
return func(c *Command) error {
err := tmpl(c.Out(), c.UsageTemplate(), c)
if err != nil {
fmt.Print(err)
}
return err
}
}
@ -226,35 +224,32 @@ func (c *Command) HelpFunc() func(*Command, []string) {
}
}
var minUsagePadding int = 25
var minUsagePadding = 25
func (c *Command) UsagePadding() int {
if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
return minUsagePadding
} else {
return c.parent.commandsMaxUseLen
}
return c.parent.commandsMaxUseLen
}
var minCommandPathPadding int = 11
var minCommandPathPadding = 11
//
func (c *Command) CommandPathPadding() int {
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
return minCommandPathPadding
} else {
return c.parent.commandsMaxCommandPathLen
}
return c.parent.commandsMaxCommandPathLen
}
var minNamePadding int = 11
var minNamePadding = 11
func (c *Command) NamePadding() int {
if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
return minNamePadding
} else {
return c.parent.commandsMaxNameLen
}
return c.parent.commandsMaxNameLen
}
func (c *Command) UsageTemplate() string {
@ -264,9 +259,9 @@ func (c *Command) UsageTemplate() string {
if c.HasParent() {
return c.parent.UsageTemplate()
} else {
return `Usage:{{if .Runnable}}
{{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}
}
return `Usage:{{if .Runnable}}
{{if .HasFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasSubCommands}}
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
Aliases:
@ -290,7 +285,6 @@ Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
}
}
func (c *Command) HelpTemplate() string {
@ -300,11 +294,10 @@ func (c *Command) HelpTemplate() string {
if c.HasParent() {
return c.parent.HelpTemplate()
} else {
return `{{with or .Long .Short }}{{. | trim}}
}
return `{{with or .Long .Short }}{{. | trim}}
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
}
}
// Really only used when casting a command to a commander
@ -606,9 +599,8 @@ func (c *Command) errorMsgFromParse() string {
if len(x) > 0 {
return x[0]
} else {
return ""
}
return ""
}
// Call execute to use the args (os.Args[1:] by default)
@ -626,12 +618,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
return c.Root().ExecuteC()
}
if EnableWindowsMouseTrap && runtime.GOOS == "windows" {
if mousetrap.StartedByExplorer() {
c.Print(MousetrapHelpText)
time.Sleep(5 * time.Second)
os.Exit(1)
}
// windows hook
if preExecHookFn != nil {
preExecHookFn(c)
}
// initialize help as the last point possible to allow for user
@ -641,7 +630,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
var args []string
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
if len(c.args) == 0 && filepath.Base(os.Args[0]) != "cobra.test" {
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
args = os.Args[1:]
} else {
args = c.args
@ -661,13 +650,16 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
}
err = cmd.execute(flags)
if err != nil {
// Always show help if requested, even if SilenceErrors is in
// effect
if err == flag.ErrHelp {
cmd.HelpFunc()(cmd, args)
return cmd, nil
}
// If root command has SilentErrors flagged,
// all subcommands should respect it
if !cmd.SilenceErrors && !c.SilenceErrors {
if err == flag.ErrHelp {
cmd.HelpFunc()(cmd, args)
return cmd, nil
}
c.Println("Error:", err.Error())
}
@ -747,7 +739,7 @@ func (c *Command) AddCommand(cmds ...*Command) {
if nameLen > c.commandsMaxNameLen {
c.commandsMaxNameLen = nameLen
}
// If glabal normalization function exists, update all children
// If global normalization function exists, update all children
if c.globNormFunc != nil {
x.SetGlobalNormalizationFunc(c.globNormFunc)
}
@ -789,18 +781,18 @@ main:
}
}
// Convenience method to Print to the defined output
// Print is a convenience method to Print to the defined output
func (c *Command) Print(i ...interface{}) {
fmt.Fprint(c.Out(), i...)
}
// Convenience method to Println to the defined output
// Println is a convenience method to Println to the defined output
func (c *Command) Println(i ...interface{}) {
str := fmt.Sprintln(i...)
c.Print(str)
}
// Convenience method to Printf to the defined output
// Printf is a convenience method to Printf to the defined output
func (c *Command) Printf(format string, i ...interface{}) {
str := fmt.Sprintf(format, i...)
c.Print(str)
@ -911,7 +903,7 @@ func (c *Command) Name() string {
return name
}
// Determine if a given string is an alias of the command.
// HasAlias determines if a given string is an alias of the command.
func (c *Command) HasAlias(s string) bool {
for _, a := range c.Aliases {
if a == s {
@ -929,12 +921,12 @@ func (c *Command) HasExample() bool {
return len(c.Example) > 0
}
// Determine if the command is itself runnable
// Runnable determines if the command is itself runnable
func (c *Command) Runnable() bool {
return c.Run != nil || c.RunE != nil
}
// Determine if the command has children commands
// HasSubCommands determines if the command has children commands
func (c *Command) HasSubCommands() bool {
return len(c.commands) > 0
}
@ -1126,7 +1118,7 @@ func (c *Command) HasInheritedFlags() bool {
return c.InheritedFlags().HasFlags()
}
// Climbs up the command tree looking for matching flag
// Flag climbs up the command tree looking for matching flag
func (c *Command) Flag(name string) (flag *flag.Flag) {
flag = c.Flags().Lookup(name)
@ -1149,13 +1141,14 @@ func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
return
}
// Parses persistent flag tree & local flags
// ParseFlags parses persistent flag tree & local flags
func (c *Command) ParseFlags(args []string) (err error) {
c.mergePersistentFlags()
err = c.Flags().Parse(args)
return
}
// Parent returns a commands parent command
func (c *Command) Parent() *Command {
return c.parent
}