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

prevent removal of valid arguments
This commit is contained in:
Clayton Coleman 2015-04-28 10:06:27 -04:00
commit af6b555418
2 changed files with 38 additions and 9 deletions

View File

@ -483,6 +483,32 @@ func TestInvalidSubCommandFlags(t *testing.T) {
} }
func TestSubCommandArgEvaluation(t *testing.T) {
cmd := initializeWithRootCmd()
first := &Command{
Use: "first",
Run: func(cmd *Command, args []string) {
},
}
cmd.AddCommand(first)
second := &Command{
Use: "second",
Run: func(cmd *Command, args []string) {
fmt.Fprintf(cmd.Out(), "%v", args)
},
}
first.AddCommand(second)
result := simpleTester(cmd, "first second first third")
expectedOutput := fmt.Sprintf("%v", []string{"first third"})
if result.Output != expectedOutput {
t.Errorf("exptected %v, got %v", expectedOutput, result.Output)
}
}
func TestPersistentFlags(t *testing.T) { func TestPersistentFlags(t *testing.T) {
fullSetupTest("echo -s something -p more here") fullSetupTest("echo -s something -p more here")

View File

@ -328,15 +328,18 @@ func stripFlags(args []string, c *Command) []string {
return commands return commands
} }
func argsMinusX(args []string, x string) []string { // argsMinusFirstX removes only the first x from args. Otherwise, commands that look like
newargs := []string{} // openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).
func argsMinusFirstX(args []string, x string) []string {
for _, y := range args { for i, y := range args {
if x != y { if x == y {
newargs = append(newargs, y) ret := []string{}
ret = append(ret, args[:i]...)
ret = append(ret, args[i+1:]...)
return ret
} }
} }
return newargs return args
} }
// find the target command given the args and command tree // find the target command given the args and command tree
@ -359,7 +362,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
matches := make([]*Command, 0) matches := make([]*Command, 0)
for _, cmd := range c.commands { for _, cmd := range c.commands {
if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match
return innerfind(cmd, argsMinusX(args, argsWOflags[0])) return innerfind(cmd, argsMinusFirstX(args, argsWOflags[0]))
} else if EnablePrefixMatching { } else if EnablePrefixMatching {
if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match
matches = append(matches, cmd) matches = append(matches, cmd)
@ -374,7 +377,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
// only accept a single prefix match - multiple matches would be ambiguous // only accept a single prefix match - multiple matches would be ambiguous
if len(matches) == 1 { if len(matches) == 1 {
return innerfind(matches[0], argsMinusX(args, argsWOflags[0])) return innerfind(matches[0], argsMinusFirstX(args, argsWOflags[0]))
} }
} }
} }