@@ -260,23 +260,32 @@ func (o *Command) Happened() bool {
260
260
// Since Parser is a Command as well, they work in exactly same way. Meaning that usage string
261
261
// can be retrieved for any level of commands. It will only include information about this Command,
262
262
// its sub-commands, current Command arguments and arguments of all preceding commands (if any)
263
- func (o * Command ) Usage (err interface {}) string {
263
+ //
264
+ // Accepts an interface that can be error, string or fmt.Stringer that will be prepended to a message.
265
+ // All other interface types will be ignored
266
+ func (o * Command ) Usage (msg interface {}) string {
267
+ var result string
264
268
// Stay classy
265
269
maxWidth := 80
266
270
// List of arguments from all preceding commands
267
271
arguments := make ([]* arg , 0 )
268
272
// First get line of commands until root
269
273
var chain []string
270
274
current := o
271
- if err != nil {
272
- switch err .(type ) {
275
+ if msg != nil {
276
+ switch msg .(type ) {
273
277
case subCommandError :
274
- fmt .Println ( err .(error ).Error ())
275
- if err .(subCommandError ).cmd != nil {
276
- return err .(subCommandError ).cmd .Usage (nil )
278
+ result = fmt .Sprintf ( "%s \n " , msg .(error ).Error ())
279
+ if msg .(subCommandError ).cmd != nil {
280
+ result += msg .(subCommandError ).cmd .Usage (nil )
277
281
}
282
+ return result
278
283
case error :
279
- fmt .Println (err .(error ).Error ())
284
+ result = fmt .Sprintf ("%s\n " , msg .(error ).Error ())
285
+ case string :
286
+ result = fmt .Sprintf ("%s\n " , msg .(string ))
287
+ case fmt.Stringer :
288
+ result = fmt .Sprintf ("%s\n " , msg .(fmt.Stringer ).String ())
280
289
}
281
290
}
282
291
for current != nil {
@@ -301,8 +310,8 @@ func (o *Command) Usage(err interface{}) string {
301
310
}
302
311
}
303
312
304
- // Build result description
305
- var result = "usage:"
313
+ // Build usage description
314
+ result + = "usage:"
306
315
leftPadding := len ("usage: " + chain [0 ] + "" )
307
316
// Add preceding commands
308
317
for _ , v := range chain {
0 commit comments