Implement scanning of version flag in specs for usage generation
This commit is contained in:
parent
ea5a49acda
commit
d8114f0410
18
usage.go
18
usage.go
|
@ -62,29 +62,39 @@ func (p *Parser) WriteUsageForSubcommand(w io.Writer, subcommand ...string) erro
|
||||||
// writeUsageForSubcommand writes usage information for the given subcommand
|
// writeUsageForSubcommand writes usage information for the given subcommand
|
||||||
func (p *Parser) writeUsageForSubcommand(w io.Writer, cmd *command) {
|
func (p *Parser) writeUsageForSubcommand(w io.Writer, cmd *command) {
|
||||||
var positionals, longOptions, shortOptions []*spec
|
var positionals, longOptions, shortOptions []*spec
|
||||||
|
var hasVersionOption bool
|
||||||
for _, spec := range cmd.specs {
|
for _, spec := range cmd.specs {
|
||||||
switch {
|
switch {
|
||||||
case spec.positional:
|
case spec.positional:
|
||||||
positionals = append(positionals, spec)
|
positionals = append(positionals, spec)
|
||||||
case spec.long != "":
|
case spec.long != "":
|
||||||
longOptions = append(longOptions, spec)
|
longOptions = append(longOptions, spec)
|
||||||
|
if spec.long == "version" {
|
||||||
|
hasVersionOption = true
|
||||||
|
}
|
||||||
case spec.short != "":
|
case spec.short != "":
|
||||||
shortOptions = append(shortOptions, spec)
|
shortOptions = append(shortOptions, spec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.version != "" {
|
|
||||||
fmt.Fprintln(w, p.version)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make a list of ancestor commands so that we print with full context
|
// make a list of ancestor commands so that we print with full context
|
||||||
|
// also determine if any ancestor has a version option spec
|
||||||
var ancestors []string
|
var ancestors []string
|
||||||
ancestor := cmd
|
ancestor := cmd
|
||||||
for ancestor != nil {
|
for ancestor != nil {
|
||||||
|
for _, spec := range ancestor.specs {
|
||||||
|
if spec.long == "version" {
|
||||||
|
hasVersionOption = true
|
||||||
|
}
|
||||||
|
}
|
||||||
ancestors = append(ancestors, ancestor.name)
|
ancestors = append(ancestors, ancestor.name)
|
||||||
ancestor = ancestor.parent
|
ancestor = ancestor.parent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !hasVersionOption && p.version != "" {
|
||||||
|
fmt.Fprintln(w, p.version)
|
||||||
|
}
|
||||||
|
|
||||||
// print the beginning of the usage string
|
// print the beginning of the usage string
|
||||||
fmt.Fprint(w, "Usage:")
|
fmt.Fprint(w, "Usage:")
|
||||||
for i := len(ancestors) - 1; i >= 0; i-- {
|
for i := len(ancestors) - 1; i >= 0; i-- {
|
||||||
|
|
Loading…
Reference in New Issue