Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage writing when using custom version flag #224

Merged
merged 4 commits into from
Jun 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement scanning of version flag in specs for usage generation
  • Loading branch information
hhromic committed Jun 29, 2024
commit bed89eb683e6016be7247041db3c998e57fc838c
20 changes: 19 additions & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,36 @@ func (p *Parser) WriteUsageForSubcommand(w io.Writer, subcommand ...string) erro
}

var positionals, longOptions, shortOptions []*spec
var hasVersionOption bool
for _, spec := range cmd.specs {
switch {
case spec.positional:
positionals = append(positionals, spec)
case spec.long != "":
longOptions = append(longOptions, spec)
if spec.long == "version" {
hasVersionOption = true
}
case spec.short != "":
shortOptions = append(shortOptions, spec)
}
}

if p.version != "" {
// 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
ancestor := cmd
for ancestor != nil {
for _, spec := range ancestor.specs {
if spec.long == "version" {
hasVersionOption = true
}
}
ancestors = append(ancestors, ancestor.name)
ancestor = ancestor.parent
}

if !hasVersionOption && p.version != "" {
fmt.Fprintln(w, p.version)
}

Expand Down