Skip to content
Myr

Merge pull request #121 from eparis/cleanups

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

Cleanups

authored by Eric Paris · committed by Eric Paris · parents 66816bc, 9a9d01c · Browse files · 2 files+47 added−62 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+47 added−62 removed

  • other:+47 added−62 removed

2 files+47 added−62 removed

modified:cobra_test.go+1 added-1 removed
766 unmodified lines
767}768769func TestRootUnknownCommand(t *testing.T) {770 r := noRRSetupTest("bogus")771 s := "Error: unknown command \"bogus\"\nRun 'cobra-test help' for usage.\n"771 s := "Error: unknown command \"bogus\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n"772773 if r.Output != s {774 t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)775 }
190 unmodified lines
modified:command.go+46 added-61 removed
82 unmodified lines
83 commandsMaxCommandPathLen int84 commandsMaxNameLen int8586 flagErrorBuf *bytes.Buffer87 cmdErrorBuf *bytes.Buffer8788 args []string // actual args parsed from flags89 output *io.Writer // nil means stderr; use Out() method instead90 usageFunc func(*Command) error // Usage can be defined by application
181 unmodified lines
272Additional help topics:273{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}274 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}275{{end}}{{ if .HasSubCommands }}277Use "{{.Root.Name}} help [command]" for more information about a command.276Use "{{.CommandPath}} [command] --help" for more information about a command.277{{end}}`278 }279}280
98 unmodified lines
379 if c == nil {380 return nil, nil, fmt.Errorf("Called find() on a nil Command")381 }382384 // If there are no arguments, return the root command. If the root has no385 // subcommands, args reflects arguments that should actually be passed to386 // the root command, so also return the root command.387 if len(args) == 0 || !c.Root().HasSubCommands() {388 return c.Root(), args, nil389 }390383 var innerfind func(*Command, []string) (*Command, []string)384385 innerfind = func(c *Command, innerArgs []string) (*Command, []string) {394 if len(innerArgs) > 0 && c.HasSubCommands() {395 argsWOflags := stripFlags(innerArgs, c)396 if len(argsWOflags) > 0 {397 matches := make([]*Command, 0)398 for _, cmd := range c.commands {399 if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match400 return innerfind(cmd, argsMinusFirstX(innerArgs, argsWOflags[0]))401 } else if EnablePrefixMatching {402 if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match403 matches = append(matches, cmd)404 }405 for _, x := range cmd.Aliases {406 if strings.HasPrefix(x, argsWOflags[0]) {407 matches = append(matches, cmd)408 }409 }386 argsWOflags := stripFlags(innerArgs, c)387 if len(argsWOflags) == 0 {388 return c, innerArgs389 }390 nextSubCmd := argsWOflags[0]391 matches := make([]*Command, 0)392 for _, cmd := range c.commands {393 if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match394 return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))395 }396 if EnablePrefixMatching {397 if strings.HasPrefix(cmd.Name(), nextSubCmd) { // prefix match398 matches = append(matches, cmd)399 }400 for _, x := range cmd.Aliases {401 if strings.HasPrefix(x, nextSubCmd) {402 matches = append(matches, cmd)403 }404 }412413 // only accept a single prefix match - multiple matches would be ambiguous414 if len(matches) == 1 {415 return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))416 }405 }406 }407408 // only accept a single prefix match - multiple matches would be ambiguous409 if len(matches) == 1 {410 return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))411 }412413 return c, innerArgs414 }415416 commandFound, a := innerfind(c, args)424425 // If we matched on the root, but we asked for a subcommand, return an error417 argsWOflags := stripFlags(a, commandFound)418419 // no subcommand, always take args420 if !commandFound.HasSubCommands() {421 return commandFound, a, nil422 }423 // root command with subcommands, do subcommand checking424 if commandFound == c && len(argsWOflags) > 0 {428 return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0])425 return commandFound, a, fmt.Errorf("unknown command %q for %q", argsWOflags[0], commandFound.CommandPath())426 }427428 return commandFound, a, nil429}
21 unmodified lines
451 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)452 }453454 err = c.ParseFlags(a)458 if err == flag.ErrHelp {459 c.Help()460 return nil461 }455 if err != nil {463 // We're writing subcommand usage to root command's error buffer to have it displayed to the user464 r := c.Root()465 if r.cmdErrorBuf == nil {466 r.cmdErrorBuf = new(bytes.Buffer)467 }468 // for writing the usage to the buffer we need to switch the output temporarily469 // since Out() returns root output, you also need to revert that on root470 out := r.Out()471 r.SetOutput(r.cmdErrorBuf)472 c.Usage()473 r.SetOutput(out)456 return err457 }476 // If help is called, regardless of other flags, we print that.477 // Print help also if c.Run is nil.458 // If help is called, regardless of other flags, return we want help459 // Also say we need help if c.Run is nil.460 if c.helpFlagVal || !c.Runnable() {479 c.Help()480 return nil461 return flag.ErrHelp462 }463464 c.preRun()465 argWoFlags := c.Flags().Args()
71 unmodified lines
537 args = c.args538 }539540 cmd, flags, err := c.Find(args)560 if err == nil {561 err = cmd.execute(flags)541 if err != nil {542 // If found parse to a subcommand and then failed, talk about the subcommand543 if cmd != nil {544 c = cmd545 }546 c.Println("Error:", err.Error())547 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())548 return err549 }550551 err = cmd.execute(flags)552 if err != nil {553 if err == flag.ErrHelp {566 c.Help()567568 } else {569 c.Println("Error:", err.Error())570 c.Printf("Run '%v help' for usage.\n", c.Root().Name())554 cmd.Help()555 return nil556 }557 c.Println(cmd.UsageString())558 c.Println("Error:", err.Error())559 }560561 return562}
20 unmodified lines
583// Used for testing584func (c *Command) ResetCommands() {585 c.commands = nil586 c.helpCommand = nil600 c.cmdErrorBuf = new(bytes.Buffer)601 c.cmdErrorBuf.Reset()587}588589//Commands returns a slice of child commands.590func (c *Command) Commands() []*Command {
427 unmodified lines