Update spf13/cobra vendor to v1.4.1
Support command traversal Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
dee3044336
commit
ac40179e8a
6 changed files with 177 additions and 37 deletions
|
@ -162,7 +162,7 @@ clone git github.com/matttproud/golang_protobuf_extensions fc2b8d3a73c4867e51861
|
|||
clone git github.com/pkg/errors 01fa4104b9c248c8945d14d9f128454d5b28d595
|
||||
|
||||
# cli
|
||||
clone git github.com/spf13/cobra 75205f23b3ea70dc7ae5e900d074e010c23c37e9 https://github.com/dnephin/cobra.git
|
||||
clone git github.com/spf13/cobra v1.4.1 https://github.com/dnephin/cobra.git
|
||||
clone git github.com/spf13/pflag cb88ea77998c3f024757528e3305022ab50b43be
|
||||
clone git github.com/inconshreveable/mousetrap 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75
|
||||
clone git github.com/flynn-archive/go-shlex 3f9db97f856818214da2e1057f8ad84803971cff
|
||||
|
|
12
vendor/src/github.com/spf13/cobra/.gitignore
vendored
12
vendor/src/github.com/spf13/cobra/.gitignore
vendored
|
@ -19,6 +19,18 @@ _cgo_export.*
|
|||
|
||||
_testmain.go
|
||||
|
||||
# Vim files https://github.com/github/gitignore/blob/master/Global/Vim.gitignore
|
||||
# swap
|
||||
[._]*.s[a-w][a-z]
|
||||
[._]s[a-w][a-z]
|
||||
# session
|
||||
Session.vim
|
||||
# temporary
|
||||
.netrwhist
|
||||
*~
|
||||
# auto-generated tag files
|
||||
tags
|
||||
|
||||
*.exe
|
||||
|
||||
cobra.test
|
||||
|
|
|
@ -116,12 +116,12 @@ __handle_reply()
|
|||
fi
|
||||
|
||||
local completions
|
||||
if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
|
||||
completions=("${must_have_one_flag[@]}")
|
||||
elif [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
|
||||
completions=("${commands[@]}")
|
||||
if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
|
||||
completions=("${must_have_one_noun[@]}")
|
||||
else
|
||||
completions=("${commands[@]}")
|
||||
fi
|
||||
if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
|
||||
completions+=("${must_have_one_flag[@]}")
|
||||
fi
|
||||
COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )
|
||||
|
||||
|
@ -167,6 +167,11 @@ __handle_flag()
|
|||
must_have_one_flag=()
|
||||
fi
|
||||
|
||||
# if you set a flag which only applies to this command, don't show subcommands
|
||||
if __contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
|
||||
commands=()
|
||||
fi
|
||||
|
||||
# keep flag value with flagname as flaghash
|
||||
if [ -n "${flagvalue}" ] ; then
|
||||
flaghash[${flagname}]=${flagvalue}
|
||||
|
@ -263,6 +268,7 @@ func postscript(w io.Writer, name string) error {
|
|||
local c=0
|
||||
local flags=()
|
||||
local two_word_flags=()
|
||||
local local_nonpersistent_flags=()
|
||||
local flags_with_completion=()
|
||||
local flags_completion=()
|
||||
local commands=("%s")
|
||||
|
@ -360,7 +366,7 @@ func writeFlagHandler(name string, annotations map[string][]string, w io.Writer)
|
|||
}
|
||||
|
||||
func writeShortFlag(flag *pflag.Flag, w io.Writer) error {
|
||||
b := (flag.Value.Type() == "bool")
|
||||
b := (len(flag.NoOptDefVal) > 0)
|
||||
name := flag.Shorthand
|
||||
format := " "
|
||||
if !b {
|
||||
|
@ -374,7 +380,7 @@ func writeShortFlag(flag *pflag.Flag, w io.Writer) error {
|
|||
}
|
||||
|
||||
func writeFlag(flag *pflag.Flag, w io.Writer) error {
|
||||
b := (flag.Value.Type() == "bool")
|
||||
b := (len(flag.NoOptDefVal) > 0)
|
||||
name := flag.Name
|
||||
format := " flags+=(\"--%s"
|
||||
if !b {
|
||||
|
@ -387,9 +393,24 @@ func writeFlag(flag *pflag.Flag, w io.Writer) error {
|
|||
return writeFlagHandler("--"+name, flag.Annotations, w)
|
||||
}
|
||||
|
||||
func writeLocalNonPersistentFlag(flag *pflag.Flag, w io.Writer) error {
|
||||
b := (len(flag.NoOptDefVal) > 0)
|
||||
name := flag.Name
|
||||
format := " local_nonpersistent_flags+=(\"--%s"
|
||||
if !b {
|
||||
format += "="
|
||||
}
|
||||
format += "\")\n"
|
||||
if _, err := fmt.Fprintf(w, format, name); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeFlags(cmd *Command, w io.Writer) error {
|
||||
_, err := fmt.Fprintf(w, ` flags=()
|
||||
two_word_flags=()
|
||||
local_nonpersistent_flags=()
|
||||
flags_with_completion=()
|
||||
flags_completion=()
|
||||
|
||||
|
@ -397,6 +418,7 @@ func writeFlags(cmd *Command, w io.Writer) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
localNonPersistentFlags := cmd.LocalNonPersistentFlags()
|
||||
var visitErr error
|
||||
cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
|
||||
if err := writeFlag(flag, w); err != nil {
|
||||
|
@ -409,6 +431,12 @@ func writeFlags(cmd *Command, w io.Writer) error {
|
|||
return
|
||||
}
|
||||
}
|
||||
if localNonPersistentFlags.Lookup(flag.Name) != nil {
|
||||
if err := writeLocalNonPersistentFlag(flag, w); err != nil {
|
||||
visitErr = err
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
if visitErr != nil {
|
||||
return visitErr
|
||||
|
|
|
@ -117,7 +117,7 @@ cmd := &cobra.Command{
|
|||
```
|
||||
|
||||
The aliases are not shown to the user on tab completion, but they are accepted as valid nouns by
|
||||
the completion aglorithm if entered manually, e.g. in:
|
||||
the completion algorithm if entered manually, e.g. in:
|
||||
|
||||
```bash
|
||||
# kubectl get rc [tab][tab]
|
||||
|
@ -175,7 +175,7 @@ So while there are many other files in the CWD it only shows me subdirs and thos
|
|||
|
||||
# Specifiy custom flag completion
|
||||
|
||||
Similar to the filename completion and filtering usingn cobra.BashCompFilenameExt, you can specifiy
|
||||
Similar to the filename completion and filtering using cobra.BashCompFilenameExt, you can specifiy
|
||||
a custom flag completion function with cobra.BashCompCustom:
|
||||
|
||||
```go
|
||||
|
|
4
vendor/src/github.com/spf13/cobra/cobra.go
vendored
4
vendor/src/github.com/spf13/cobra/cobra.go
vendored
|
@ -41,6 +41,10 @@ var initializers []func()
|
|||
// Set this to true to enable it
|
||||
var EnablePrefixMatching = false
|
||||
|
||||
//EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
|
||||
//To disable sorting, set it to false.
|
||||
var EnableCommandSorting = true
|
||||
|
||||
//AddTemplateFunc adds a template function that's available to Usage and Help
|
||||
//template generation.
|
||||
func AddTemplateFunc(name string, tmplFunc interface{}) {
|
||||
|
|
150
vendor/src/github.com/spf13/cobra/command.go
vendored
150
vendor/src/github.com/spf13/cobra/command.go
vendored
|
@ -21,6 +21,7 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
|
@ -105,6 +106,8 @@ type Command struct {
|
|||
commandsMaxUseLen int
|
||||
commandsMaxCommandPathLen int
|
||||
commandsMaxNameLen int
|
||||
// is commands slice are sorted or not
|
||||
commandsAreSorted bool
|
||||
|
||||
flagErrorBuf *bytes.Buffer
|
||||
|
||||
|
@ -126,6 +129,9 @@ type Command struct {
|
|||
|
||||
// Disable the flag parsing. If this is true all flags will be passed to the command as arguments.
|
||||
DisableFlagParsing bool
|
||||
|
||||
// TraverseChildren parses flags on all parents before executing child command
|
||||
TraverseChildren bool
|
||||
}
|
||||
|
||||
// os.Args[1:] by default, if desired, can be overridden
|
||||
|
@ -409,13 +415,14 @@ func argsMinusFirstX(args []string, x string) []string {
|
|||
return args
|
||||
}
|
||||
|
||||
// find the target command given the args and command tree
|
||||
func isFlagArg(arg string) bool {
|
||||
return ((len(arg) >= 3 && arg[1] == '-') ||
|
||||
(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
|
||||
}
|
||||
|
||||
// Find the target command given the args and command tree
|
||||
// Meant to be run on the highest node. Only searches down.
|
||||
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) {
|
||||
|
@ -424,28 +431,11 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
|||
return c, innerArgs
|
||||
}
|
||||
nextSubCmd := argsWOflags[0]
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
||||
}
|
||||
if EnablePrefixMatching {
|
||||
if strings.HasPrefix(cmd.Name(), nextSubCmd) { // prefix match
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
for _, x := range cmd.Aliases {
|
||||
if strings.HasPrefix(x, nextSubCmd) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// only accept a single prefix match - multiple matches would be ambiguous
|
||||
if len(matches) == 1 {
|
||||
return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
|
||||
cmd := c.findNext(nextSubCmd)
|
||||
if cmd != nil {
|
||||
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
||||
}
|
||||
|
||||
return c, innerArgs
|
||||
}
|
||||
|
||||
|
@ -456,6 +446,66 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
|
|||
return commandFound, a, nil
|
||||
}
|
||||
|
||||
func (c *Command) findNext(next string) *Command {
|
||||
matches := make([]*Command, 0)
|
||||
for _, cmd := range c.commands {
|
||||
if cmd.Name() == next || cmd.HasAlias(next) {
|
||||
return cmd
|
||||
}
|
||||
if EnablePrefixMatching && cmd.HasNameOrAliasPrefix(next) {
|
||||
matches = append(matches, cmd)
|
||||
}
|
||||
}
|
||||
|
||||
if len(matches) == 1 {
|
||||
return matches[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse the command tree to find the command, and parse args for
|
||||
// each parent.
|
||||
func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
||||
flags := []string{}
|
||||
inFlag := false
|
||||
|
||||
for i, arg := range args {
|
||||
switch {
|
||||
// A long flag with a space separated value
|
||||
case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
|
||||
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
||||
inFlag = !isBooleanFlag(arg[2:], c.Flags())
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// A short flag with a space separated value
|
||||
case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !isBooleanShortFlag(arg[1:], c.Flags()):
|
||||
inFlag = true
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// The value for a flag
|
||||
case inFlag:
|
||||
inFlag = false
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
// A flag without a value, or with an `=` separated value
|
||||
case isFlagArg(arg):
|
||||
flags = append(flags, arg)
|
||||
continue
|
||||
}
|
||||
|
||||
cmd := c.findNext(arg)
|
||||
if cmd == nil {
|
||||
return c, args, nil
|
||||
}
|
||||
|
||||
if err := c.ParseFlags(flags); err != nil {
|
||||
return nil, args, err
|
||||
}
|
||||
return cmd.Traverse(args[i+1:])
|
||||
}
|
||||
return c, args, nil
|
||||
}
|
||||
|
||||
func (c *Command) findSuggestions(arg string) string {
|
||||
if c.DisableSuggestions {
|
||||
return ""
|
||||
|
@ -668,7 +718,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||
args = c.args
|
||||
}
|
||||
|
||||
cmd, flags, err := c.Find(args)
|
||||
var flags []string
|
||||
if c.TraverseChildren {
|
||||
cmd, flags, err = c.Traverse(args)
|
||||
} else {
|
||||
cmd, flags, err = c.Find(args)
|
||||
}
|
||||
if err != nil {
|
||||
// If found parse to a subcommand and then failed, talk about the subcommand
|
||||
if cmd != nil {
|
||||
|
@ -680,6 +735,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
err = cmd.execute(flags)
|
||||
if err != nil {
|
||||
// Always show help if requested, even if SilenceErrors is in
|
||||
|
@ -754,8 +810,20 @@ func (c *Command) ResetCommands() {
|
|||
c.helpCommand = nil
|
||||
}
|
||||
|
||||
//Commands returns a slice of child commands.
|
||||
// Sorts commands by their names
|
||||
type commandSorterByName []*Command
|
||||
|
||||
func (c commandSorterByName) Len() int { return len(c) }
|
||||
func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }
|
||||
|
||||
// Commands returns a sorted slice of child commands.
|
||||
func (c *Command) Commands() []*Command {
|
||||
// do not sort commands if it already sorted or sorting was disabled
|
||||
if EnableCommandSorting && !c.commandsAreSorted{
|
||||
sort.Sort(commandSorterByName(c.commands))
|
||||
c.commandsAreSorted = true
|
||||
}
|
||||
return c.commands
|
||||
}
|
||||
|
||||
|
@ -784,6 +852,7 @@ func (c *Command) AddCommand(cmds ...*Command) {
|
|||
x.SetGlobalNormalizationFunc(c.globNormFunc)
|
||||
}
|
||||
c.commands = append(c.commands, x)
|
||||
c.commandsAreSorted = false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -953,6 +1022,20 @@ func (c *Command) HasAlias(s string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return true
|
||||
}
|
||||
for _, alias := range c.Aliases {
|
||||
if strings.HasPrefix(alias, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Command) NameAndAliases() string {
|
||||
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
|
||||
}
|
||||
|
@ -1065,6 +1148,19 @@ func (c *Command) Flags() *flag.FlagSet {
|
|||
return c.flags
|
||||
}
|
||||
|
||||
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands
|
||||
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
||||
persistentFlags := c.PersistentFlags()
|
||||
|
||||
out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
||||
c.LocalFlags().VisitAll(func(f *flag.Flag) {
|
||||
if persistentFlags.Lookup(f.Name) == nil {
|
||||
out.AddFlag(f)
|
||||
}
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// Get the local FlagSet specifically set in the current command
|
||||
func (c *Command) LocalFlags() *flag.FlagSet {
|
||||
c.mergePersistentFlags()
|
||||
|
|
Loading…
Reference in a new issue