Skip to content
Myr

Subcommands flag parsing errors print subcommand usage and not root's command usage

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

authored by Maciej Szulik · committed by spf13 · parent b825817 · Browse files · 1 file+16 added−1 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+16 added−1 removed

  • other:+16 added−1 removed

1 file+16 added−1 removed

modified:command.go+16 added-1 removed
55 unmodified lines
56 commandsMaxUseLen int57 commandsMaxCommandPathLen int5859 flagErrorBuf *bytes.Buffer60 cmdErrorBuf *bytes.Buffer6162 args []string63 output *io.Writer // nil means stderr; use Out() method instead64 usageFunc func(*Command) error // Usage can be defined by application
290 unmodified lines
355356 err = c.ParseFlags(a)357358 if err != nil {359 // We're writing subcommand usage to root command's error buffer to have it displayed to the user360 if c.Root().cmdErrorBuf == nil {361 c.Root().cmdErrorBuf = new(bytes.Buffer)362 }363 // for writing the usage to the buffer we need to switch the output temporarily364 out := c.Out()365 c.SetOutput(c.Root().cmdErrorBuf)366 c.Usage()367 c.SetOutput(out)368 return err369 } else {370 // If help is called, regardless of other flags, we print that371 if c.helpFlagVal {
67 unmodified lines
439 if e != nil {440 // Flags parsing had an error.441 // If an error happens here, we have to report it to the user442 c.Println(c.errorMsgFromParse())443 // If an error happens search also for subcommand info about that444 if c.cmdErrorBuf != nil && c.cmdErrorBuf.Len() > 0 {445 c.Println(c.cmdErrorBuf.String())446 } else {433 c.Usage()447 c.Usage()448 }449 return e450 } else {451 // If help is called, regardless of other flags, we print that452 if c.helpFlagVal {
332 unmodified lines