| 672 | } |
| 673 | |
| 674 | func stripFlags(args []string, c *Command) []string { |
| 675 | if len(args) == 0 { |
| 676 | return args |
| 677 | } |
| 678 | c.mergePersistentFlags() |
| 679 | |
| 680 | commands := []string{} |
| 681 | flags := c.Flags() |
| 682 | |
| 683 | Loop: |
| 684 | for len(args) > 0 { |
| 685 | s := args[0] |
| 686 | args = args[1:] |
| 687 | switch { |
| 688 | case s == "--": |
| 689 | // "--" terminates the flags |
| 690 | break Loop |
| 691 | case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags): |
| 692 | // If '--flag arg' then |
| 693 | // delete arg from args. |
| 694 | fallthrough // (do the same as below) |
| 695 | case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags): |
| 696 | // If '-f arg' then |
| 697 | // delete 'arg' from args or break the loop if len(args) <= 1. |
| 698 | if len(args) <= 1 { |
| 699 | break Loop |
| 700 | } else { |
| 701 | args = args[1:] |
| 702 | continue |
| 703 | } |
| 704 | case s != "" && !strings.HasPrefix(s, "-"): |
| 705 | commands = append(commands, s) |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | return commands |
| 710 | } |
| 711 | |
| 712 | // argsMinusFirstX removes only the first x from args. Otherwise, commands that look like |
| 713 | // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]). |