Skip to content
Myr

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

2 files+48 added−17 removed

modified:cobra_test.go+31 added-4 removed
2 unmodified lines
3import (4 "bytes"5 "fmt"6 "os"7 "reflect"8 "runtime"9 "strings"10 "testing"11)1213var _ = fmt.Println14var _ = os.Stderr1515var tp, te, tt, t1 []string16var tp, te, tt, t1, tr []string17var rootPersPre, echoPre, echoPersPre, timesPersPre []string18var flagb1, flagb2, flagb3, flagbr, flagbp bool19var flags1, flags2a, flags2b, flags3 string20var flagi1, flagi2, flagi3, flagir int
78 unmodified lines
99 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 lines
182}183184func 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 lines
495 t.Errorf("invalid flag should generate error")496 }497}498497func TestInvalidSubCommandFlags(t *testing.T) {499func TestInvalidSubcommandFlags(t *testing.T) {500 cmd := initializeWithRootCmd()501 cmd.AddCommand(cmdTimes)502503 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")
5 unmodified lines
509 }510511}512511func TestSubCommandArgEvaluation(t *testing.T) {513func TestSubcommandArgEvaluation(t *testing.T) {514 cmd := initializeWithRootCmd()515516 first := &Command{517 Use: "first",
302 unmodified lines
820 return821 }822}823824func TestCommandWithoutSubcommands(t *testing.T) {825 c := initializeWithRootCmd()826827 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}833834func TestCommandWithoutSubcommandsWithArg(t *testing.T) {835 c := initializeWithRootCmd()836 expectedArgs := []string{"arg"}837838 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}848849func TestReplaceCommandWithRemove(t *testing.T) {850 versionUsed = 0851 c := initializeWithRootCmd()852 c.AddCommand(cmdVersion1)
63 unmodified lines
modified:command.go+17 added-13 removed
16 unmodified lines
1718import (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"2627 "github.com/inconshreveable/mousetrap"28 flag "github.com/spf13/pflag"29)3031// Command is just that, a command for your application.32// eg. 'go run' ... 'run' is the command. Cobra requires
327 unmodified lines
360}361362// find the target command given the args and command tree363// Meant to be run on the highest node. Only searches down.363func (c *Command) Find(arrs []string) (*Command, []string, error) {364func (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 }368369 // 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 }375376 var innerfind func(*Command, []string) (*Command, []string)377374 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 lines
396 }397398 // 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 }404401 return c, args405 return c, innerArgs406 }407404 commandFound, a := innerfind(c, arrs)408 commandFound, a := innerfind(c, args)409410 // 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 }414415 return commandFound, a, nil
592 unmodified lines