mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Fix a bug in traversal with detecting flags.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
parent
419884c85a
commit
d34f8d4094
2 changed files with 12 additions and 7 deletions
|
@ -479,8 +479,8 @@ func argsMinusFirstX(args []string, x string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isFlagArg(arg string) bool {
|
func isFlagArg(arg string) bool {
|
||||||
return ((len(arg) >= 3 && arg[0] == '-') ||
|
return ((len(arg) >= 3 && arg[1] == '-') ||
|
||||||
(len(arg) >= 2 && arg[1] == '-' && arg[1] != '-'))
|
(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the target command given the args and command tree
|
// Find the target command given the args and command tree
|
||||||
|
@ -551,19 +551,23 @@ func (c *Command) Traverse(args []string) (*Command, []string, error) {
|
||||||
|
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
switch {
|
switch {
|
||||||
|
// A long flag with a space separated value
|
||||||
case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
|
case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):
|
||||||
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
||||||
inFlag = !hasNoOptDefVal(arg[2:], c.Flags())
|
inFlag = !hasNoOptDefVal(arg[2:], c.Flags())
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
|
// A short flag with a space separated value
|
||||||
case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
|
case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):
|
||||||
inFlag = true
|
inFlag = true
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
|
// The value for a flag
|
||||||
case inFlag:
|
case inFlag:
|
||||||
inFlag = false
|
inFlag = false
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
|
// A flag without a value, or with an `=` separated value
|
||||||
case isFlagArg(arg):
|
case isFlagArg(arg):
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -354,12 +354,13 @@ func TestTraverseWithParentFlags(t *testing.T) {
|
||||||
TraverseChildren: true,
|
TraverseChildren: true,
|
||||||
}
|
}
|
||||||
cmd.Flags().String("foo", "", "foo things")
|
cmd.Flags().String("foo", "", "foo things")
|
||||||
|
cmd.Flags().BoolP("goo", "g", false, "foo things")
|
||||||
|
|
||||||
sub := &Command{Use: "next"}
|
sub := &Command{Use: "next"}
|
||||||
sub.Flags().String("add", "", "add things")
|
sub.Flags().String("add", "", "add things")
|
||||||
cmd.AddCommand(sub)
|
cmd.AddCommand(sub)
|
||||||
|
|
||||||
c, args, err := cmd.Traverse([]string{"--foo", "ok", "next", "--add"})
|
c, args, err := cmd.Traverse([]string{"-g", "--foo", "ok", "next", "--add"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected no error: %s", err)
|
t.Fatalf("Expected no error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue