Skip to content
Myr

Merge pull request #192 from apriendeau/executeC

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

Creates the ExecuteC function while maintaining Execute signature

authored by Eric Paris · committed by Eric Paris · parents 9324f36, 3f60599 · Browse files · 2 files+58 added−7 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+58 added−7 removed

  • other:+58 added−7 removed

2 files+58 added−7 removed

modified:cobra_test.go+47 added
246 unmodified lines
247248 return resulter{err, output, c}249}250251func simpleTesterC(c *Command, input string) resulter {252 buf := new(bytes.Buffer)253 // Testing flag with invalid input254 c.SetOutput(buf)255 c.SetArgs(strings.Split(input, " "))256257 cmd, err := c.ExecuteC()258 output := buf.String()259260 return resulter{err, output, cmd}261}262263func fullTester(c *Command, input string) resulter {264 buf := new(bytes.Buffer)265 // Testing flag with invalid input266 c.SetOutput(buf)
305 unmodified lines
572 }573574}575576func TestSubcommandExecuteC(t *testing.T) {577 cmd := initializeWithRootCmd()578 double := &Command{579 Use: "double message",580 Run: func(c *Command, args []string) {581 msg := strings.Join(args, " ")582 c.Println(msg, msg)583 },584 }585586 echo := &Command{587 Use: "echo message",588 Run: func(c *Command, args []string) {589 msg := strings.Join(args, " ")590 c.Println(msg, msg)591 },592 }593594 cmd.AddCommand(double, echo)595596 result := simpleTesterC(cmd, "double hello world")597 checkResultContains(t, result, "hello world hello world")598599 if result.Command.Name() != "double" {600 t.Errorf("invalid cmd returned from ExecuteC: should be 'double' but got %s", result.Command.Name())601 }602603 result = simpleTesterC(cmd, "echo msg to be echoed")604 checkResultContains(t, result, "msg to be echoed")605606 if result.Command.Name() != "echo" {607 t.Errorf("invalid cmd returned from ExecuteC: should be 'echo' but got %s", result.Command.Name())608 }609}610611func TestSubcommandArgEvaluation(t *testing.T) {612 cmd := initializeWithRootCmd()613614 first := &Command{
549 unmodified lines
modified:command.go+11 added-7 removed
612 unmodified lines
613614// Call execute to use the args (os.Args[1:] by default)615// and run through the command tree finding appropriate matches616// for commands and then corresponding flags.617func (c *Command) Execute() (err error) {617func (c *Command) Execute() error {618 _, err := c.ExecuteC()619 return err620}621622func (c *Command) ExecuteC() (cmd *Command, err error) {623624 // Regardless of what command execute is called on, run on Root only625 if c.HasParent() {621 return c.Root().Execute()626 return c.Root().ExecuteC()627 }628629 if EnableWindowsMouseTrap && runtime.GOOS == "windows" {630 if mousetrap.StartedByExplorer() {
25 unmodified lines
656 if !c.SilenceErrors {657 c.Println("Error:", err.Error())658 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())659 }655 return err660 return c, err661 }657662 err = cmd.execute(flags)663 if err != nil {664 // If root command has SilentErrors flagged,665 // all subcommands should respect it666 if !cmd.SilenceErrors && !c.SilenceErrors {667 if err == flag.ErrHelp {668 cmd.HelpFunc()(cmd, args)665 return nil669 return cmd, nil670 }671 c.Println("Error:", err.Error())672 }673
1 unmodified line
675 // all subcommands should respect it676 if !cmd.SilenceUsage && !c.SilenceUsage {677 c.Println(cmd.UsageString())678 }675 return err679 return cmd, err680 }677 return681 return cmd, nil682}683684func (c *Command) initHelpFlag() {685 if c.Flags().Lookup("help") == nil {
515 unmodified lines