mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
Merge 667c348dbd
into e174a40cf5
This commit is contained in:
commit
69651fcb70
2 changed files with 80 additions and 2 deletions
|
@ -47,6 +47,12 @@ var cmdRootNoRun = &Command{
|
||||||
Long: "The root description for help",
|
Long: "The root description for help",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cmdRootSameName = &Command{
|
||||||
|
Use: "print",
|
||||||
|
Short: "Root with the same name as a subcommand",
|
||||||
|
Long: "The root description for help",
|
||||||
|
}
|
||||||
|
|
||||||
var cmdRootWithRun = &Command{
|
var cmdRootWithRun = &Command{
|
||||||
Use: "cobra-test",
|
Use: "cobra-test",
|
||||||
Short: "The root can run it's own function",
|
Short: "The root can run it's own function",
|
||||||
|
@ -65,6 +71,7 @@ func flagInit() {
|
||||||
cmdPrint.ResetFlags()
|
cmdPrint.ResetFlags()
|
||||||
cmdTimes.ResetFlags()
|
cmdTimes.ResetFlags()
|
||||||
cmdRootNoRun.ResetFlags()
|
cmdRootNoRun.ResetFlags()
|
||||||
|
cmdRootSameName.ResetFlags()
|
||||||
cmdRootWithRun.ResetFlags()
|
cmdRootWithRun.ResetFlags()
|
||||||
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
cmdEcho.Flags().IntVarP(&flagi1, "intone", "i", 123, "help message for flag intone")
|
||||||
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")
|
||||||
|
@ -81,6 +88,9 @@ func commandInit() {
|
||||||
cmdEcho.ResetCommands()
|
cmdEcho.ResetCommands()
|
||||||
cmdPrint.ResetCommands()
|
cmdPrint.ResetCommands()
|
||||||
cmdTimes.ResetCommands()
|
cmdTimes.ResetCommands()
|
||||||
|
cmdRootNoRun.ResetCommands()
|
||||||
|
cmdRootSameName.ResetCommands()
|
||||||
|
cmdRootWithRun.ResetCommands()
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize() *Command {
|
func initialize() *Command {
|
||||||
|
@ -91,6 +101,14 @@ func initialize() *Command {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initializeWithSameName() *Command {
|
||||||
|
tt, tp, te = nil, nil, nil
|
||||||
|
var c = cmdRootSameName
|
||||||
|
flagInit()
|
||||||
|
commandInit()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
func initializeWithRootCmd() *Command {
|
func initializeWithRootCmd() *Command {
|
||||||
cmdRootWithRun.ResetCommands()
|
cmdRootWithRun.ResetCommands()
|
||||||
tt, tp, te, rootcalled = nil, nil, nil, false
|
tt, tp, te, rootcalled = nil, nil, nil, false
|
||||||
|
@ -136,6 +154,58 @@ func TestChildCommand(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChildCommandPrefix(t *testing.T) {
|
||||||
|
c := initialize()
|
||||||
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
|
c.AddCommand(cmdPrint, cmdEcho)
|
||||||
|
c.SetArgs(strings.Split("ech tim one two", " "))
|
||||||
|
c.Execute()
|
||||||
|
|
||||||
|
if te != nil || tp != nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if tt == nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if strings.Join(tt, " ") != "one two" {
|
||||||
|
t.Error("Command didn't parse correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChildSameName(t *testing.T) {
|
||||||
|
c := initializeWithSameName()
|
||||||
|
c.AddCommand(cmdPrint, cmdEcho)
|
||||||
|
c.SetArgs(strings.Split("print one two", " "))
|
||||||
|
c.Execute()
|
||||||
|
|
||||||
|
if te != nil || tt != nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if tp == nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if strings.Join(tp, " ") != "one two" {
|
||||||
|
t.Error("Command didn't parse correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChildSameNamePrefix(t *testing.T) {
|
||||||
|
c := initializeWithSameName()
|
||||||
|
c.AddCommand(cmdPrint, cmdEcho)
|
||||||
|
c.SetArgs(strings.Split("pr one two", " "))
|
||||||
|
c.Execute()
|
||||||
|
|
||||||
|
if te != nil || tt != nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if tp == nil {
|
||||||
|
t.Error("Wrong command called")
|
||||||
|
}
|
||||||
|
if strings.Join(tp, " ") != "one two" {
|
||||||
|
t.Error("Command didn't parse correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFlagLong(t *testing.T) {
|
func TestFlagLong(t *testing.T) {
|
||||||
c := initialize()
|
c := initialize()
|
||||||
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
c.AddCommand(cmdPrint, cmdEcho, cmdTimes)
|
||||||
|
|
12
command.go
12
command.go
|
@ -239,11 +239,19 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
||||||
|
|
||||||
innerfind = func(c *Command, args []string) (*Command, []string) {
|
innerfind = func(c *Command, args []string) (*Command, []string) {
|
||||||
if len(args) > 0 && c.HasSubCommands() {
|
if len(args) > 0 && c.HasSubCommands() {
|
||||||
|
matches := make([]*Command, 0)
|
||||||
for _, cmd := range c.commands {
|
for _, cmd := range c.commands {
|
||||||
if cmd.Name() == args[0] {
|
if cmd.Name() == args[0] { // exact name match
|
||||||
return innerfind(cmd, args[1:])
|
return innerfind(cmd, args[1:])
|
||||||
|
} else if strings.HasPrefix(cmd.Name(), args[0]) { // prefix match
|
||||||
|
matches = append(matches, cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only accept a single prefix match - multiple matches would be ambiguous
|
||||||
|
if len(matches) == 1 {
|
||||||
|
return innerfind(matches[0], args[1:])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, args
|
return c, args
|
||||||
|
@ -252,7 +260,7 @@ func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
||||||
commandFound, a := innerfind(c, arrs)
|
commandFound, a := innerfind(c, arrs)
|
||||||
|
|
||||||
// if commander returned and not appropriately matched return nil & error
|
// if commander returned and not appropriately matched return nil & error
|
||||||
if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {
|
if commandFound.Name() == c.Name() && !strings.HasPrefix(commandFound.Name(), arrs[0]) {
|
||||||
return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])
|
return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue