mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Simplify stripFlags
This commit is contained in:
parent
63121c8814
commit
f4f10f6873
1 changed files with 12 additions and 22 deletions
34
command.go
34
command.go
|
@ -366,26 +366,20 @@ func (c *Command) HelpTemplate() string {
|
||||||
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Really only used when casting a command to a commander.
|
|
||||||
func (c *Command) resetChildrensParents() {
|
|
||||||
for _, x := range c.commands {
|
|
||||||
x.parent = c
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasNoOptDefVal(name string, f *flag.FlagSet) bool {
|
func hasNoOptDefVal(name string, f *flag.FlagSet) bool {
|
||||||
flag := f.Lookup(name)
|
flag := f.Lookup(name)
|
||||||
if flag == nil {
|
if flag == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return len(flag.NoOptDefVal) > 0
|
return flag.NoOptDefVal != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||||
result := false
|
result := false
|
||||||
fs.VisitAll(func(flag *flag.Flag) {
|
fs.VisitAll(func(flag *flag.Flag) {
|
||||||
if flag.Shorthand == name && len(flag.NoOptDefVal) > 0 {
|
if flag.Shorthand == name && flag.NoOptDefVal != "" {
|
||||||
result = true
|
result = true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
|
@ -399,29 +393,27 @@ func stripFlags(args []string, c *Command) []string {
|
||||||
|
|
||||||
commands := []string{}
|
commands := []string{}
|
||||||
inQuote := false
|
inQuote := false
|
||||||
|
flags := c.Flags()
|
||||||
|
|
||||||
Loop:
|
Loop:
|
||||||
for len(args) > 0 {
|
for len(args) > 0 {
|
||||||
s := args[0]
|
s := args[0]
|
||||||
|
args = args[1:]
|
||||||
if !inQuote {
|
if !inQuote {
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
||||||
inQuote = true
|
inQuote = true
|
||||||
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], c.Flags()):
|
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
||||||
// If '--flag arg' then
|
// If '--flag arg' then
|
||||||
// delete two items from args.
|
// delete arg from args.
|
||||||
fallthrough // Do the same as below.
|
fallthrough // (do the same as below)
|
||||||
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], c.Flags()):
|
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
||||||
// If '-f arg' then
|
// If '-f arg' then
|
||||||
// delete two items from args.
|
// delete 'arg' from args or break the loop if len(args) <= 1.
|
||||||
|
if len(args) <= 1 {
|
||||||
// If there are only two elements in args or less,
|
|
||||||
// break loop, ...
|
|
||||||
if len(args) <= 2 {
|
|
||||||
break Loop
|
break Loop
|
||||||
} else {
|
} else {
|
||||||
// ... else delete first two items.
|
args = args[1:]
|
||||||
args = args[2:]
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
case s != "" && !strings.HasPrefix(s, "-"):
|
case s != "" && !strings.HasPrefix(s, "-"):
|
||||||
|
@ -432,8 +424,6 @@ Loop:
|
||||||
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
||||||
inQuote = false
|
inQuote = false
|
||||||
}
|
}
|
||||||
|
|
||||||
args = args[1:]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
|
Loading…
Reference in a new issue