Skip to content
Myr

Making prefix matching opt in.

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

authored by spf13 · committed by spf13 · parent c2c23ac · Browse files · 3 files+20 added−1 removed
This commit

State

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

Diff stats

3 files+20 added−1 removed

  • other:+20 added−1 removed

3 files+20 added−1 removed

modified:cobra_test.go+15 added
207 unmodified lines
208 }209}210211func TestPrefixMatching(t *testing.T) {212 EnablePrefixMatching = true213 noRRSetupTest("ech times one two")214215 if te != nil || tp != nil {216 t.Error("Wrong command called")
3 unmodified lines
220 }221 if strings.Join(tt, " ") != "one two" {222 t.Error("Command didn't parse correctly")223 }224225 EnablePrefixMatching = false226}227228func TestNoPrefixMatching(t *testing.T) {229 EnablePrefixMatching = false230231 noRRSetupTest("ech times one two")232233 if !(tt == nil && te == nil && tp == nil) {234 t.Error("Wrong command called")235 }236}237238func TestAliasPrefixMatching(t *testing.T) {239 EnablePrefixMatching = true240 noRRSetupTest("sa times one two")241242 if te != nil || tp != nil {243 t.Error("Wrong command called")
3 unmodified lines
247 }248 if strings.Join(tt, " ") != "one two" {249 t.Error("Command didn't parse correctly")250 }251 EnablePrefixMatching = false252}253254func TestChildSameName(t *testing.T) {255 c := initializeWithSameName()
252 unmodified lines
modified:cobra.go+4 added
25 unmodified lines
26)2728var initializers []func()2930// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.31// Set this to true to enable it32var EnablePrefixMatching bool = false3334func OnInitialize(y ...func()) {35 for _, x := range y {36 initializers = append(initializers, x)37 }
61 unmodified lines
modified:command.go+1 added-1 removed
288 unmodified lines
289 matches := make([]*Command, 0)290 for _, cmd := range c.commands {291 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match292 return innerfind(cmd, argsMinusX(args, argsWOflags[0]))293 } else {293 } else if EnablePrefixMatching {294 if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match295 matches = append(matches, cmd)296 }297 for _, x := range cmd.Aliases {
467 unmodified lines