argsMinusFirstX removes only the first x from args. Otherwise, commands that look like openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]). Special care needs to be taken not to remove a flag value.
(args []string, x string)
| 713 | // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]). |
| 714 | // Special care needs to be taken not to remove a flag value. |
| 715 | func (c *Command) argsMinusFirstX(args []string, x string) []string { |
| 716 | if len(args) == 0 { |
| 717 | return args |
| 718 | } |
| 719 | c.mergePersistentFlags() |
| 720 | flags := c.Flags() |
| 721 | |
| 722 | Loop: |
| 723 | for pos := 0; pos < len(args); pos++ { |
| 724 | s := args[pos] |
| 725 | switch { |
| 726 | case s == "--": |
| 727 | // -- means we have reached the end of the parseable args. Break out of the loop now. |
| 728 | break Loop |
| 729 | case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags): |
| 730 | fallthrough |
| 731 | case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags): |
| 732 | // This is a flag without a default value, and an equal sign is not used. Increment pos in order to skip |
| 733 | // over the next arg, because that is the value of this flag. |
| 734 | pos++ |
| 735 | continue |
| 736 | case !strings.HasPrefix(s, "-"): |
| 737 | // This is not a flag or a flag value. Check to see if it matches what we're looking for, and if so, |
| 738 | // return the args, excluding the one at this position. |
| 739 | if s == x { |
| 740 | ret := make([]string, 0, len(args)-1) |
| 741 | ret = append(ret, args[:pos]...) |
| 742 | ret = append(ret, args[pos+1:]...) |
| 743 | return ret |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | return args |
| 748 | } |
| 749 | |
| 750 | func isFlagArg(arg string) bool { |
| 751 | return ((len(arg) >= 3 && arg[0:2] == "--") || |
no test coverage detected