Fix root command without subcommands but with arguments
Before adoption: this commit came in with the history the project adopted, so no landing records it.
[close #108]
authored by Sam Ghods · committed by spf13 · parent c11766b · Browse files · 2 files·+48 added−17 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+48 added−17 removed
- other:+48 added−17 removed
Provenance
- Commit
e0f326d- Tree
ec3118b- Author
Sam Ghods <sam@box.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
c11766b
2 files changed+48 added−17 removed
modified:cobra_test.go+31 added-4 removed
2 unmodified lines3 import (4 "bytes"5 "fmt"6 "os"7+ "reflect"8 "runtime"9 "strings"10 "testing"11 )12 13 var _ = fmt.Println14 var _ = os.Stderr15 15-var tp, te, tt, t1 []string16+var tp, te, tt, t1, tr []string17 var rootPersPre, echoPre, echoPersPre, timesPersPre []string18 var flagb1, flagb2, flagb3, flagbr, flagbp bool19 var flags1, flags2a, flags2b, flags3 string20 var flagi1, flagi2, flagi3, flagir int78 unmodified lines99 Use: "cobra-test",100 Short: "The root can run it's own function",101 Long: "The root description for help",102 Run: func(cmd *Command, args []string) {103+ tr = args104 rootcalled = true105 },106 }107 74 unmodified lines182 }183 184 func initializeWithRootCmd() *Command {185 cmdRootWithRun.ResetCommands()184- tt, tp, te, rootcalled = nil, nil, nil, false186+ tt, tp, te, tr, rootcalled = nil, nil, nil, nil, false187 flagInit()188 cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")189 cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")190 commandInit()304 unmodified lines495 t.Errorf("invalid flag should generate error")496 }497 }498 497-func TestInvalidSubCommandFlags(t *testing.T) {499+func TestInvalidSubcommandFlags(t *testing.T) {500 cmd := initializeWithRootCmd()501 cmd.AddCommand(cmdTimes)502 503 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")5 unmodified lines509 }510 511 }512 511-func TestSubCommandArgEvaluation(t *testing.T) {513+func TestSubcommandArgEvaluation(t *testing.T) {514 cmd := initializeWithRootCmd()515 516 first := &Command{517 Use: "first",302 unmodified lines820 return821 }822 }823 824+func TestCommandWithoutSubcommands(t *testing.T) {825+ c := initializeWithRootCmd()826+827+ x := simpleTester(c, "")828+ if x.Error != nil {829+ t.Errorf("Calling command without subcommands should not have error: %v", x.Error)830+ return831+ }832+}833+834+func TestCommandWithoutSubcommandsWithArg(t *testing.T) {835+ c := initializeWithRootCmd()836+ expectedArgs := []string{"arg"}837+838+ x := simpleTester(c, "arg")839+ if x.Error != nil {840+ t.Errorf("Calling command without subcommands but with arg should not have error: %v", x.Error)841+ return842+ }843+ if !reflect.DeepEqual(expectedArgs, tr) {844+ t.Errorf("Calling command without subcommands but with arg has wrong args: expected: %v, actual: %v", expectedArgs, tr)845+ return846+ }847+}848+849 func TestReplaceCommandWithRemove(t *testing.T) {850 versionUsed = 0851 c := initializeWithRootCmd()852 c.AddCommand(cmdVersion1)63 unmodified linesmodified:command.go+17 added-13 removed
16 unmodified lines17 18 import (19 "bytes"20 "fmt"21- "github.com/inconshreveable/mousetrap"22- flag "github.com/spf13/pflag"21 "io"22 "os"23 "runtime"24 "strings"25 "time"26+27+ "github.com/inconshreveable/mousetrap"28+ flag "github.com/spf13/pflag"29 )30 31 // Command is just that, a command for your application.32 // eg. 'go run' ... 'run' is the command. Cobra requires327 unmodified lines360 }361 362 // find the target command given the args and command tree363 // Meant to be run on the highest node. Only searches down.363-func (c *Command) Find(arrs []string) (*Command, []string, error) {364+func (c *Command) Find(args []string) (*Command, []string, error) {365 if c == nil {366 return nil, nil, fmt.Errorf("Called find() on a nil Command")367 }368 369+ // If there are no arguments, return the root command. If the root has no370+ // subcommands, args reflects arguments that should actually be passed to371+ // the root command, so also return the root command.368- if len(arrs) == 0 {369- return c.Root(), arrs, nil372+ if len(args) == 0 || !c.Root().HasSubCommands() {373+ return c.Root(), args, nil374 }375 376 var innerfind func(*Command, []string) (*Command, []string)377 374- innerfind = func(c *Command, args []string) (*Command, []string) {375- if len(args) > 0 && c.HasSubCommands() {376- argsWOflags := stripFlags(args, c)378+ innerfind = func(c *Command, innerArgs []string) (*Command, []string) {379+ if len(innerArgs) > 0 && c.HasSubCommands() {380+ argsWOflags := stripFlags(innerArgs, c)381 if len(argsWOflags) > 0 {382 matches := make([]*Command, 0)383 for _, cmd := range c.commands {384 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match381- return innerfind(cmd, argsMinusFirstX(args, argsWOflags[0]))385+ return innerfind(cmd, argsMinusFirstX(innerArgs, argsWOflags[0]))386 } else if EnablePrefixMatching {387 if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match388 matches = append(matches, cmd)389 }6 unmodified lines396 }397 398 // only accept a single prefix match - multiple matches would be ambiguous399 if len(matches) == 1 {396- return innerfind(matches[0], argsMinusFirstX(args, argsWOflags[0]))400+ return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))401 }402 }403 }404 401- return c, args405+ return c, innerArgs406 }407 404- commandFound, a := innerfind(c, arrs)408+ commandFound, a := innerfind(c, args)409 410 // If we matched on the root, but we asked for a subcommand, return an error407- if commandFound.Name() == c.Name() && len(stripFlags(arrs, c)) > 0 && commandFound.Name() != arrs[0] {411+ if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] {412 return nil, a, fmt.Errorf("unknown command %q", a[0])413 }414 415 return commandFound, a, nil592 unmodified lines