Print global options in help for subcommands

fixes #101
This commit is contained in:
Dylan Allbee 2020-01-23 21:08:18 -08:00
parent e9c71eb4fa
commit 338e831a84
2 changed files with 25 additions and 4 deletions

View File

@ -235,7 +235,8 @@ func Example_helpTextForSubcommand() {
// Positional arguments:
// ITEM item to fetch
//
// Options:
// Global options:
// --verbose
// --help, -h display this help and exit
}

View File

@ -144,10 +144,30 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) {
}
// write the list of options
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
p.printOption(w, &spec{