@@ -177,8 +177,6 @@ type Command struct {
177
177
// that we can use on every pflag set and children commands
178
178
globNormFunc func (f * flag.FlagSet , name string ) flag.NormalizedName
179
179
180
- // output is an output writer defined by user.
181
- output io.Writer
182
180
// usageFunc is usage func defined by user.
183
181
usageFunc func (* Command ) error
184
182
// usageTemplate is usage template defined by user.
@@ -195,6 +193,13 @@ type Command struct {
195
193
helpCommand * Command
196
194
// versionTemplate is the version template defined by user.
197
195
versionTemplate string
196
+
197
+ // inReader is a reader defined by the user that replaces stdin
198
+ inReader io.Reader
199
+ // outWriter is a writer defined by the user that replaces stdout
200
+ outWriter io.Writer
201
+ // errWriter is a writer defined by the user that replaces stderr
202
+ errWriter io.Writer
198
203
}
199
204
200
205
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
@@ -206,7 +211,25 @@ func (c *Command) SetArgs(a []string) {
206
211
// SetOutput sets the destination for usage and error messages.
207
212
// If output is nil, os.Stderr is used.
208
213
func (c * Command ) SetOutput (output io.Writer ) {
209
- c .output = output
214
+ c .outWriter = output
215
+ }
216
+
217
+ // SetOut sets the destination for usage messages.
218
+ // If newOut is nil, os.Stdout is used.
219
+ func (c * Command ) SetOut (newOut io.Writer ) {
220
+ c .outWriter = newOut
221
+ }
222
+
223
+ // SetErr sets the destination for error messages.
224
+ // If newErr is nil, os.Stderr is used.
225
+ func (c * Command ) SetErr (newErr io.Writer ) {
226
+ c .errWriter = newErr
227
+ }
228
+
229
+ // SetOut sets the source for input data
230
+ // If newIn is nil, os.Stdin is used.
231
+ func (c * Command ) SetIn (newIn io.Reader ) {
232
+ c .inReader = newIn
210
233
}
211
234
212
235
// SetUsageFunc sets usage function. Usage can be defined by application.
@@ -267,16 +290,46 @@ func (c *Command) OutOrStderr() io.Writer {
267
290
return c .getOut (os .Stderr )
268
291
}
269
292
293
+ // ErrOrStderr returns output to stderr
294
+ func (c * Command ) ErrOrStderr () io.Writer {
295
+ return c .getErr (os .Stderr )
296
+ }
297
+
298
+ // ErrOrStderr returns output to stderr
299
+ func (c * Command ) InOrStdin () io.Reader {
300
+ return c .getIn (os .Stdin )
301
+ }
302
+
270
303
func (c * Command ) getOut (def io.Writer ) io.Writer {
271
- if c .output != nil {
272
- return c .output
304
+ if c .outWriter != nil {
305
+ return c .outWriter
273
306
}
274
307
if c .HasParent () {
275
308
return c .parent .getOut (def )
276
309
}
277
310
return def
278
311
}
279
312
313
+ func (c * Command ) getErr (def io.Writer ) io.Writer {
314
+ if c .errWriter != nil {
315
+ return c .errWriter
316
+ }
317
+ if c .HasParent () {
318
+ return c .parent .getErr (def )
319
+ }
320
+ return def
321
+ }
322
+
323
+ func (c * Command ) getIn (def io.Reader ) io.Reader {
324
+ if c .inReader != nil {
325
+ return c .inReader
326
+ }
327
+ if c .HasParent () {
328
+ return c .parent .getIn (def )
329
+ }
330
+ return def
331
+ }
332
+
280
333
// UsageFunc returns either the function set by SetUsageFunc for this command
281
334
// or a parent, or it returns a default usage function.
282
335
func (c * Command ) UsageFunc () (f func (* Command ) error ) {
@@ -331,11 +384,11 @@ func (c *Command) Help() error {
331
384
332
385
// UsageString return usage string.
333
386
func (c * Command ) UsageString () string {
334
- tmpOutput := c .output
387
+ tmpOutput := c .outWriter
335
388
bb := new (bytes.Buffer )
336
- c .SetOutput ( bb )
389
+ c .outWriter = bb
337
390
c .Usage ()
338
- c .output = tmpOutput
391
+ c .outWriter = tmpOutput
339
392
return bb .String ()
340
393
}
341
394
0 commit comments