prevent removal of valid arguments
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by deads2k · committed by deads2k · parent 3c187e9 · Browse files · 2 files·+38 added−9 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+38 added−9 removed
- other:+38 added−9 removed
Provenance
- Commit
36aee64- Tree
cd59b91- Author
deads2k <deads@redhat.com>- Committer
deads2k <deads@redhat.com>- Parent
3c187e9
2 files changed+38 added−9 removed
modified:cobra_test.go+26 added
481 unmodified lines482 }483 484 }485 486+func TestSubCommandArgEvaluation(t *testing.T) {487+ cmd := initializeWithRootCmd()488+489+ first := &Command{490+ Use: "first",491+ Run: func(cmd *Command, args []string) {492+ },493+ }494+ cmd.AddCommand(first)495+496+ second := &Command{497+ Use: "second",498+ Run: func(cmd *Command, args []string) {499+ fmt.Fprintf(cmd.Out(), "%v", args)500+ },501+ }502+ first.AddCommand(second)503+504+ result := simpleTester(cmd, "first second first third")505+506+ expectedOutput := fmt.Sprintf("%v", []string{"first third"})507+ if result.Output != expectedOutput {508+ t.Errorf("exptected %v, got %v", expectedOutput, result.Output)509+ }510+}511+512 func TestPersistentFlags(t *testing.T) {513 fullSetupTest("echo -s something -p more here")514 515 // persistentFlag should act like normal flag on it's own command299 unmodified linesmodified:command.go+12 added-9 removed
326 unmodified lines327 328 return commands329 }330 331-func argsMinusX(args []string, x string) []string {332- newargs := []string{}333-334- for _, y := range args {335- if x != y {336- newargs = append(newargs, y)331+// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like332+// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).333+func argsMinusFirstX(args []string, x string) []string {334+ for i, y := range args {335+ if x == y {336+ ret := []string{}337+ ret = append(ret, args[:i]...)338+ ret = append(ret, args[i+1:]...)339+ return ret340 }341 }339- return newargs342+ return args343 }344 345 // find the target command given the args and command tree346 // Meant to be run on the highest node. Only searches down.14 unmodified lines361 if len(argsWOflags) > 0 {362 matches := make([]*Command, 0)363 for _, cmd := range c.commands {364 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match362- return innerfind(cmd, argsMinusX(args, argsWOflags[0]))365+ return innerfind(cmd, argsMinusFirstX(args, argsWOflags[0]))366 } else if EnablePrefixMatching {367 if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match368 matches = append(matches, cmd)369 }6 unmodified lines376 }377 378 // only accept a single prefix match - multiple matches would be ambiguous379 if len(matches) == 1 {377- return innerfind(matches[0], argsMinusX(args, argsWOflags[0]))380+ return innerfind(matches[0], argsMinusFirstX(args, argsWOflags[0]))381 }382 }383 }384 580 unmodified lines