Skip to content
Myr

Merge pull request #95 from deads2k/deads-prevent-extra-removal

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

prevent removal of valid arguments

authored by Clayton Coleman · committed by Clayton Coleman · parents ebb2d55, 36aee64 · 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

2 files+38 added−9 removed

modified:cobra_test.go+26 added
481 unmodified lines
482 }483484}485486func TestSubCommandArgEvaluation(t *testing.T) {487 cmd := initializeWithRootCmd()488489 first := &Command{490 Use: "first",491 Run: func(cmd *Command, args []string) {492 },493 }494 cmd.AddCommand(first)495496 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)503504 result := simpleTester(cmd, "first second first third")505506 expectedOutput := fmt.Sprintf("%v", []string{"first third"})507 if result.Output != expectedOutput {508 t.Errorf("exptected %v, got %v", expectedOutput, result.Output)509 }510}511512func TestPersistentFlags(t *testing.T) {513 fullSetupTest("echo -s something -p more here")514515 // persistentFlag should act like normal flag on it's own command
299 unmodified lines
modified:command.go+12 added-9 removed
326 unmodified lines
327328 return commands329}330331func argsMinusX(args []string, x string) []string {332 newargs := []string{}333334 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]).333func 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}344345// find the target command given the args and command tree346// Meant to be run on the highest node. Only searches down.
14 unmodified lines
361 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 lines
376 }377378 // 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