mirror of https://github.com/spf13/cobra.git
considering stderr in UsageString
This commit is contained in:
parent
e35034f0da
commit
b635726081
11
command.go
11
command.go
|
@ -384,13 +384,22 @@ func (c *Command) Help() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UsageString return usage string.
|
// UsageString returns usage string.
|
||||||
func (c *Command) UsageString() string {
|
func (c *Command) UsageString() string {
|
||||||
|
// Storing normal writers
|
||||||
tmpOutput := c.outWriter
|
tmpOutput := c.outWriter
|
||||||
|
tmpErr := c.errWriter
|
||||||
|
|
||||||
bb := new(bytes.Buffer)
|
bb := new(bytes.Buffer)
|
||||||
c.outWriter = bb
|
c.outWriter = bb
|
||||||
|
c.errWriter = bb
|
||||||
|
|
||||||
c.Usage()
|
c.Usage()
|
||||||
|
|
||||||
|
// Setting things back to normal
|
||||||
c.outWriter = tmpOutput
|
c.outWriter = tmpOutput
|
||||||
|
c.errWriter = tmpErr
|
||||||
|
|
||||||
return bb.String()
|
return bb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1405,6 +1405,22 @@ func TestSetIn(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUsageStringRedirected(t *testing.T) {
|
||||||
|
c := &Command{}
|
||||||
|
|
||||||
|
c.usageFunc = func(cmd *Command) error {
|
||||||
|
cmd.Print("[stdout1]")
|
||||||
|
cmd.PrintErr("[stderr2]")
|
||||||
|
cmd.Print("[stdout3]")
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := "[stdout1][stderr2][stdout3]"
|
||||||
|
if got := c.UsageString(); got != expected {
|
||||||
|
t.Errorf("Expected usage string to consider both stdout and stderr")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFlagErrorFunc(t *testing.T) {
|
func TestFlagErrorFunc(t *testing.T) {
|
||||||
c := &Command{Use: "c", Run: emptyRun}
|
c := &Command{Use: "c", Run: emptyRun}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue