Update vendor spf13/cobra to fix completion error in bash 3
This commit is contained in:
96
vendor/github.com/spf13/cobra/command.go
generated
vendored
96
vendor/github.com/spf13/cobra/command.go
generated
vendored
@@ -75,6 +75,11 @@ type Command struct {
|
||||
// group commands.
|
||||
Annotations map[string]string
|
||||
|
||||
// Version defines the version for this command. If this value is non-empty and the command does not
|
||||
// define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
|
||||
// will print content of the "Version" variable.
|
||||
Version string
|
||||
|
||||
// The *Run functions are executed in the following order:
|
||||
// * PersistentPreRun()
|
||||
// * PreRun()
|
||||
@@ -142,6 +147,11 @@ type Command struct {
|
||||
commandsMaxNameLen int
|
||||
// commandsAreSorted defines, if command slice are sorted or not.
|
||||
commandsAreSorted bool
|
||||
// commandCalledAs is the name or alias value used to call this command.
|
||||
commandCalledAs struct {
|
||||
name string
|
||||
called bool
|
||||
}
|
||||
|
||||
// args is actual args parsed from flags.
|
||||
args []string
|
||||
@@ -177,6 +187,8 @@ type Command struct {
|
||||
// helpCommand is command with usage 'help'. If it's not defined by user,
|
||||
// cobra uses default help command.
|
||||
helpCommand *Command
|
||||
// versionTemplate is the version template defined by user.
|
||||
versionTemplate string
|
||||
}
|
||||
|
||||
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
|
||||
@@ -222,6 +234,11 @@ func (c *Command) SetHelpTemplate(s string) {
|
||||
c.helpTemplate = s
|
||||
}
|
||||
|
||||
// SetVersionTemplate sets version template to be used. Application can use it to set custom template.
|
||||
func (c *Command) SetVersionTemplate(s string) {
|
||||
c.versionTemplate = s
|
||||
}
|
||||
|
||||
// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
|
||||
// The user should not have a cyclic dependency on commands.
|
||||
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
||||
@@ -411,6 +428,19 @@ func (c *Command) HelpTemplate() string {
|
||||
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|
||||
}
|
||||
|
||||
// VersionTemplate return version template for the command.
|
||||
func (c *Command) VersionTemplate() string {
|
||||
if c.versionTemplate != "" {
|
||||
return c.versionTemplate
|
||||
}
|
||||
|
||||
if c.HasParent() {
|
||||
return c.parent.VersionTemplate()
|
||||
}
|
||||
return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
|
||||
`
|
||||
}
|
||||
|
||||
func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||
flag := fs.Lookup(name)
|
||||
if flag == nil {
|
||||
@@ -532,6 +562,7 @@ func (c *Command) findNext(next string) *Command {
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == next || cmd.HasAlias(next) {
|
||||
cmd.commandCalledAs.name = next
|
||||
return cmd
|
||||
}
|
||||
if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {
|
||||
@@ -542,6 +573,7 @@ func (c *Command) findNext(next string) *Command {
|
||||
if len(matches) == 1 {
|
||||
return matches[0]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -640,9 +672,10 @@ func (c *Command) execute(a []string) (err error) {
|
||||
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
|
||||
}
|
||||
|
||||
// initialize help flag as the last point possible to allow for user
|
||||
// initialize help and version flag at the last point possible to allow for user
|
||||
// overriding
|
||||
c.InitDefaultHelpFlag()
|
||||
c.InitDefaultVersionFlag()
|
||||
|
||||
err = c.ParseFlags(a)
|
||||
if err != nil {
|
||||
@@ -659,7 +692,27 @@ func (c *Command) execute(a []string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if helpVal || !c.Runnable() {
|
||||
if helpVal {
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
||||
// for back-compat, only add version flag behavior if version is defined
|
||||
if c.Version != "" {
|
||||
versionVal, err := c.Flags().GetBool("version")
|
||||
if err != nil {
|
||||
c.Println("\"version\" flag declared as non-bool. Please correct your code")
|
||||
return err
|
||||
}
|
||||
if versionVal {
|
||||
err := tmpl(c.OutOrStdout(), c.VersionTemplate(), c)
|
||||
if err != nil {
|
||||
c.Println(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !c.Runnable() {
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
||||
@@ -782,6 +835,11 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||
return c, err
|
||||
}
|
||||
|
||||
cmd.commandCalledAs.called = true
|
||||
if cmd.commandCalledAs.name == "" {
|
||||
cmd.commandCalledAs.name = cmd.Name()
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// Always show help if requested, even if SilenceErrors is in
|
||||
@@ -848,6 +906,27 @@ func (c *Command) InitDefaultHelpFlag() {
|
||||
}
|
||||
}
|
||||
|
||||
// InitDefaultVersionFlag adds default version flag to c.
|
||||
// It is called automatically by executing the c.
|
||||
// If c already has a version flag, it will do nothing.
|
||||
// If c.Version is empty, it will do nothing.
|
||||
func (c *Command) InitDefaultVersionFlag() {
|
||||
if c.Version == "" {
|
||||
return
|
||||
}
|
||||
|
||||
c.mergePersistentFlags()
|
||||
if c.Flags().Lookup("version") == nil {
|
||||
usage := "version for "
|
||||
if c.Name() == "" {
|
||||
usage += "this command"
|
||||
} else {
|
||||
usage += c.Name()
|
||||
}
|
||||
c.Flags().Bool("version", false, usage)
|
||||
}
|
||||
}
|
||||
|
||||
// InitDefaultHelpCmd adds default help command to c.
|
||||
// It is called automatically by executing the c or by calling help and usage.
|
||||
// If c already has help command or c has no subcommands, it will do nothing.
|
||||
@@ -1068,14 +1147,25 @@ func (c *Command) HasAlias(s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// CalledAs returns the command name or alias that was used to invoke
|
||||
// this command or an empty string if the command has not been called.
|
||||
func (c *Command) CalledAs() string {
|
||||
if c.commandCalledAs.called {
|
||||
return c.commandCalledAs.name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// hasNameOrAliasPrefix returns true if the Name or any of aliases start
|
||||
// with prefix
|
||||
func (c *Command) hasNameOrAliasPrefix(prefix string) bool {
|
||||
if strings.HasPrefix(c.Name(), prefix) {
|
||||
c.commandCalledAs.name = c.Name()
|
||||
return true
|
||||
}
|
||||
for _, alias := range c.Aliases {
|
||||
if strings.HasPrefix(alias, prefix) {
|
||||
c.commandCalledAs.name = alias
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1178,7 +1268,7 @@ func (c *Command) HasParent() bool {
|
||||
return c.parent != nil
|
||||
}
|
||||
|
||||
// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists.
|
||||
// GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist.
|
||||
func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
|
||||
return c.globNormFunc
|
||||
}
|
||||
|
Reference in New Issue
Block a user