Skip to content

SetOut and SetErr don't seem to work as expeceted? #1100

@hypergig

Description

@hypergig

Apologies if my implementation is busted but if I do the following:

var rootCmd = &cobra.Command{
    Use:   "root",
    Run: func(cmd *cobra.Command, args []string) {
      cmd.Println("PRINT 1")
      cmd.PrintErrln("PRINTERR 1")
      cmd.SetOut(os.Stdout)
      cmd.SetErr(os.Stderr)
      cmd.Println("PRINT 2")
      cmd.PrintErrln("PRINTERR 2")
    },
  }

I see this behaviour:

13:57:20 ▶ root
PRINT 1
PRINTERR 1
PRINT 2
PRINTERR 2
13:57:29 ▶ root > /dev/null
PRINT 1
PRINTERR 1
13:57:33 ▶ root 2> /dev/null
PRINT 2
PRINTERR 2

I know cmd.Println sends to stderr by default, so I expect ^this to look more like this maybe:

13:57:20 ▶ root
PRINT 1
PRINTERR 1
PRINT 2
PRINTERR 2
13:57:29 ▶ root > /dev/null
PRINT 1
PRINTERR 1
PRINTERR 2
13:57:33 ▶ root 2> /dev/null
PRINT 2

Am I using the Set functions incorrectly or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions