mirror of
https://github.com/spf13/cobra
synced 2024-11-05 05:17:12 +00:00
Simplify code
Use a helper function for both code paths instead of duplicating the logic.
This commit is contained in:
parent
b07c5cdd6f
commit
cc1f750da2
1 changed files with 30 additions and 53 deletions
75
command.go
75
command.go
|
@ -643,6 +643,27 @@ func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
||||||
return flag.NoOptDefVal != ""
|
return flag.NoOptDefVal != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shorthandCombinationNeedsNextArg(combination string, flags *flag.FlagSet) bool {
|
||||||
|
lastPos := len(combination) - 1
|
||||||
|
for i, shorthand := range combination {
|
||||||
|
if !shortHasNoOptDefVal(string(shorthand), flags) {
|
||||||
|
// This shorthand needs a value.
|
||||||
|
//
|
||||||
|
// If we're at the end of the shorthand combination, this means that the
|
||||||
|
// value for the shorthand is given in the next argument. (e.g. '-xyzf arg',
|
||||||
|
// where -x, -y, -z are boolean flags, and -f is a flag that needs a value).
|
||||||
|
//
|
||||||
|
// Otherwise, if the shorthand combination doesn't end here, this means that the value
|
||||||
|
// for the shorthand is given in the same argument, meaning we don't have to consume the
|
||||||
|
// next one. (e.g. '-xyzfarg', where -x, -y, -z are boolean flags, and -f is a flag that
|
||||||
|
// needs a value).
|
||||||
|
return i == lastPos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func stripFlags(args []string, c *Command) ([]string, []string) {
|
func stripFlags(args []string, c *Command) ([]string, []string) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return args, nil
|
return args, nil
|
||||||
|
@ -675,36 +696,14 @@ Loop:
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
case strings.HasPrefix(s, "-") && !strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && len(s) > 2:
|
case strings.HasPrefix(s, "-") && !strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && len(s) > 2:
|
||||||
shorthandCombination := s[1:] // Skip the leading "-"
|
shorthandCombination := s[1:] // Skip leading "-"
|
||||||
lastPos := len(shorthandCombination) - 1
|
if shorthandCombinationNeedsNextArg(shorthandCombination, flags) {
|
||||||
for i, shorthand := range shorthandCombination {
|
|
||||||
if shortHasNoOptDefVal(string(shorthand), flags) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// We found a shorthand that needs a value.
|
|
||||||
|
|
||||||
if i == lastPos {
|
|
||||||
// Since we're at the end of the shorthand combination, this means that the
|
|
||||||
// value for the shorthand is given in the next argument. (e.g. '-xyzf arg',
|
|
||||||
// where -x, -y, -z are boolean flags, and -f is a flag that needs a value).
|
|
||||||
|
|
||||||
// The whole combination will take a value.
|
|
||||||
flagsThatConsumeNextArg = append(flagsThatConsumeNextArg, s)
|
flagsThatConsumeNextArg = append(flagsThatConsumeNextArg, s)
|
||||||
|
|
||||||
if len(args) <= 1 {
|
if len(args) <= 1 {
|
||||||
break Loop
|
break Loop
|
||||||
} else {
|
} else {
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Since the shorthand combination doesn't end here, this means that the
|
|
||||||
// value for the shorthand is given in the same argument, meaning we don't
|
|
||||||
// have to consume the next one. (e.g. '-xyzfarg', where -x, -y, -z are
|
|
||||||
// boolean flags, and -f is a flag that needs a value).
|
|
||||||
}
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
case s != "" && !strings.HasPrefix(s, "-"):
|
case s != "" && !strings.HasPrefix(s, "-"):
|
||||||
commands = append(commands, s)
|
commands = append(commands, s)
|
||||||
|
@ -848,38 +847,16 @@ func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
||||||
inFlag = false
|
inFlag = false
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
// A flag without a value, or with an `=` separated value
|
// A flag with an `=` separated value, or a shorthand combination, possibly with a value
|
||||||
case isFlagArg(arg):
|
case isFlagArg(arg):
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
|
|
||||||
if !strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") || strings.Contains(arg, "=") || len(arg) <= 2 {
|
if strings.HasPrefix(arg, "--") || strings.Contains(arg, "=") || len(arg) <= 2 {
|
||||||
continue // Not a shorthand combination, so nothing more to do.
|
continue // Not a shorthand combination, so nothing more to do.
|
||||||
}
|
}
|
||||||
|
|
||||||
shorthandCombination := arg[1:] // Skip leading "-"
|
shorthandCombination := arg[1:] // Skip leading "-"
|
||||||
lastPos := len(shorthandCombination) - 1
|
inFlag = shorthandCombinationNeedsNextArg(shorthandCombination, c.Flags())
|
||||||
for i, shorthand := range shorthandCombination {
|
|
||||||
if shortHasNoOptDefVal(string(shorthand), c.Flags()) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// We found a shorthand that needs a value.
|
|
||||||
|
|
||||||
if i == lastPos {
|
|
||||||
// Since we're at the end of the shorthand combination, this means that the
|
|
||||||
// value for the shorthand is given in the next argument. (e.g. '-xyzf arg',
|
|
||||||
// where -x, -y, -z are boolean flags, and -f is a flag that needs a value).
|
|
||||||
inFlag = true
|
|
||||||
} else {
|
|
||||||
// Since the shorthand combination doesn't end here, this means that the
|
|
||||||
// value for the shorthand is given in the same argument, meaning we don't
|
|
||||||
// have to consume the next one. (e.g. '-xyzfarg', where -x, -y, -z are
|
|
||||||
// boolean flags, and -f is a flag that needs a value).
|
|
||||||
}
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue