MCPcopy
hub / github.com/spf13/cobra / stripFlags

Function stripFlags

command.go:674–710  ·  view source on GitHub ↗
(args []string, c *Command)

Source from the content-addressed store, hash-verified

672}
673
674func 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
683Loop:
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]).

Callers 2

TestStripFlagsFunction · 0.85
FindMethod · 0.85

Calls 4

hasNoOptDefValFunction · 0.85
shortHasNoOptDefValFunction · 0.85
mergePersistentFlagsMethod · 0.80
FlagsMethod · 0.80

Tested by 1

TestStripFlagsFunction · 0.68