Skip to content
Myr

fix: don't remove flag value that matches subcommand name (#1781)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When the command searches args to find the arg matching a particular subcommand name, it needs to ignore flag values, as it is possible that the value for a flag might match the name of the sub command. This change improves argsMinusFirstX() to ignore flag values when it searches for the X to exclude from the result.

authored by Brian Pursley · committed by GitHub · parent cc7e235 · Browse files · 2 files+121 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+121 added−8 removed

  • other:+121 added−8 removed

2 files+121 added−8 removed

modified:command_test.go+89 added
2601 unmodified lines
2602 checkStringContains(t, output, rootCmd.Long)2603 checkStringContains(t, output, HelpFlag)2604 checkStringOmits(t, output, VersionFlag)2605}26062607func TestFind(t *testing.T) {2608 var foo, bar string2609 root := &Command{2610 Use: "root",2611 }2612 root.PersistentFlags().StringVarP(&foo, "foo", "f", "", "")2613 root.PersistentFlags().StringVarP(&bar, "bar", "b", "something", "")26142615 child := &Command{2616 Use: "child",2617 }2618 root.AddCommand(child)26192620 testCases := []struct {2621 args []string2622 expectedFoundArgs []string2623 }{2624 {2625 []string{"child"},2626 []string{},2627 },2628 {2629 []string{"child", "child"},2630 []string{"child"},2631 },2632 {2633 []string{"child", "foo", "child", "bar", "child", "baz", "child"},2634 []string{"foo", "child", "bar", "child", "baz", "child"},2635 },2636 {2637 []string{"-f", "child", "child"},2638 []string{"-f", "child"},2639 },2640 {2641 []string{"child", "-f", "child"},2642 []string{"-f", "child"},2643 },2644 {2645 []string{"-b", "child", "child"},2646 []string{"-b", "child"},2647 },2648 {2649 []string{"child", "-b", "child"},2650 []string{"-b", "child"},2651 },2652 {2653 []string{"child", "-b"},2654 []string{"-b"},2655 },2656 {2657 []string{"-b", "-f", "child", "child"},2658 []string{"-b", "-f", "child"},2659 },2660 {2661 []string{"-f", "child", "-b", "something", "child"},2662 []string{"-f", "child", "-b", "something"},2663 },2664 {2665 []string{"-f", "child", "child", "-b"},2666 []string{"-f", "child", "-b"},2667 },2668 {2669 []string{"-f=child", "-b=something", "child"},2670 []string{"-f=child", "-b=something"},2671 },2672 {2673 []string{"--foo", "child", "--bar", "something", "child"},2674 []string{"--foo", "child", "--bar", "something"},2675 },2676 }26772678 for _, tc := range testCases {2679 t.Run(fmt.Sprintf("%v", tc.args), func(t *testing.T) {2680 cmd, foundArgs, err := root.Find(tc.args)2681 if err != nil {2682 t.Fatal(err)2683 }26842685 if cmd != child {2686 t.Fatal("Expected cmd to be child, but it was not")2687 }26882689 if !reflect.DeepEqual(tc.expectedFoundArgs, foundArgs) {2690 t.Fatalf("Wrong args\nExpected: %v\nGot: %v", tc.expectedFoundArgs, foundArgs)2691 }2692 })2693 }2694}
modified:command.go+32 added-8 removed
653 unmodified lines
654}655656// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like657// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).658// Special care needs to be taken not to remove a flag value.659func (c *Command) argsMinusFirstX(args []string, x string) []string {660 if len(args) == 0 {661 return args662 }663 c.mergePersistentFlags()664 flags := c.Flags()665666Loop:667 for pos := 0; pos < len(args); pos++ {668 s := args[pos]669 switch {670 case s == "--":671 // -- means we have reached the end of the parseable args. Break out of the loop now.672 break Loop673 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):674 fallthrough675 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):676 // This is a flag without a default value, and an equal sign is not used. Increment pos in order to skip677 // over the next arg, because that is the value of this flag.678 pos++679 continue680 case !strings.HasPrefix(s, "-"):658func argsMinusFirstX(args []string, x string) []string {659 for i, y := range args {660 if x == y {661 ret := []string{}662 ret = append(ret, args[:i]...)663 ret = append(ret, args[i+1:]...)664 return ret681 // This is not a flag or a flag value. Check to see if it matches what we're looking for, and if so,682 // return the args, excluding the one at this position.683 if s == x {684 ret := []string{}685 ret = append(ret, args[:pos]...)686 ret = append(ret, args[pos+1:]...)687 return ret688 }689 }690 }691 return args692}
16 unmodified lines
709 nextSubCmd := argsWOflags[0]710711 cmd := c.findNext(nextSubCmd)712 if cmd != nil {689 return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))713 return innerfind(cmd, c.argsMinusFirstX(innerArgs, nextSubCmd))714 }715 return c, innerArgs716 }717
1117 unmodified lines