Skip to content
Myr

Extending redirection to stdout, stderr, stdin

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

authored by Juan Leni · committed by Steve Francia · parent 5f23f55 · Browse files · 1 file+61 added−8 removed
This commit

State

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

Diff stats

1 file+61 added−8 removed

  • other:+61 added−8 removed

1 file+61 added−8 removed

modified:command.go+61 added-8 removed
175 unmodified lines
176 // globNormFunc is the global normalization function177 // that we can use on every pflag set and children commands178 globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName179180 // output is an output writer defined by user.181 output io.Writer180 // usageFunc is usage func defined by user.181 usageFunc func(*Command) error182 // usageTemplate is usage template defined by user.183 usageTemplate string
8 unmodified lines
192 // cobra uses default help command.193 helpCommand *Command194 // versionTemplate is the version template defined by user.195 versionTemplate string196197 // inReader is a reader defined by the user that replaces stdin198 inReader io.Reader199 // outWriter is a writer defined by the user that replaces stdout200 outWriter io.Writer201 // errWriter is a writer defined by the user that replaces stderr202 errWriter io.Writer203}204205// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden206// particularly useful when testing.
3 unmodified lines
210211// SetOutput sets the destination for usage and error messages.212// If output is nil, os.Stderr is used.213func (c *Command) SetOutput(output io.Writer) {209 c.output = output214 c.outWriter = output215}216217// SetOut sets the destination for usage messages.218// If newOut is nil, os.Stdout is used.219func (c *Command) SetOut(newOut io.Writer) {220 c.outWriter = newOut221}222223// SetErr sets the destination for error messages.224// If newErr is nil, os.Stderr is used.225func (c *Command) SetErr(newErr io.Writer) {226 c.errWriter = newErr227}228229// SetOut sets the source for input data230// If newIn is nil, os.Stdin is used.231func (c *Command) SetIn(newIn io.Reader) {232 c.inReader = newIn233}234235// SetUsageFunc sets usage function. Usage can be defined by application.236func (c *Command) SetUsageFunc(f func(*Command) error) {237 c.usageFunc = f238}
50 unmodified lines
289func (c *Command) OutOrStderr() io.Writer {290 return c.getOut(os.Stderr)291}292293// ErrOrStderr returns output to stderr294func (c *Command) ErrOrStderr() io.Writer {295 return c.getErr(os.Stderr)296}297298// ErrOrStderr returns output to stderr299func (c *Command) InOrStdin() io.Reader {300 return c.getIn(os.Stdin)301}302303func (c *Command) getOut(def io.Writer) io.Writer {271 if c.output != nil {272 return c.output304 if c.outWriter != nil {305 return c.outWriter306 }307 if c.HasParent() {308 return c.parent.getOut(def)309 }310 return def311}312313func (c *Command) getErr(def io.Writer) io.Writer {314 if c.errWriter != nil {315 return c.errWriter316 }317 if c.HasParent() {318 return c.parent.getErr(def)319 }320 return def321}322323func (c *Command) getIn(def io.Reader) io.Reader {324 if c.inReader != nil {325 return c.inReader326 }327 if c.HasParent() {328 return c.parent.getIn(def)329 }330 return def331}332333// UsageFunc returns either the function set by SetUsageFunc for this command334// or a parent, or it returns a default usage function.335func (c *Command) UsageFunc() (f func(*Command) error) {336 if c.usageFunc != nil {
46 unmodified lines
383}384385// UsageString return usage string.386func (c *Command) UsageString() string {334 tmpOutput := c.output387 tmpOutput := c.outWriter388 bb := new(bytes.Buffer)336 c.SetOutput(bb)389 c.outWriter = bb390 c.Usage()338 c.output = tmpOutput391 c.outWriter = tmpOutput392 return bb.String()393}394395// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
1173 unmodified lines