diff options
| author | Dylan Allbee <[email protected]> | 2020-01-23 21:08:18 -0800 |
|---|---|---|
| committer | Dylan Allbee <[email protected]> | 2020-01-25 11:53:34 -0800 |
| commit | 338e831a8474c442b2c5e75eefed7b602276faa8 (patch) | |
| tree | 3928166c5324c4ba73043d80168b7540a5927453 /usage.go | |
| parent | e9c71eb4fa701960bd054d4034d155717aef8373 (diff) | |
Print global options in help for subcommands
fixes #101
Diffstat (limited to 'usage.go')
| -rw-r--r-- | usage.go | 26 |
1 files changed, 23 insertions, 3 deletions
@@ -144,9 +144,29 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) { } // write the list of options - fmt.Fprint(w, "\nOptions:\n") - for _, spec := range options { - p.printOption(w, spec) + if len(options) > 0 || cmd.parent == nil { + fmt.Fprint(w, "\nOptions:\n") + for _, spec := range options { + p.printOption(w, spec) + } + } + + // obtain a flattened list of options from all ancestors + var globals []*spec + ancestor := cmd.parent + for ancestor != nil { + for _, spec := range ancestor.specs { + globals = append(globals, spec) + } + ancestor = ancestor.parent + } + + // write the list of global options + if len(globals) > 0 { + fmt.Fprint(w, "\nGlobal options:\n") + for _, spec := range globals { + p.printOption(w, spec) + } } // write the list of built in options |
