summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorHugo Hromic <[email protected]>2023-07-14 20:12:47 +0100
committerHugo Hromic <[email protected]>2023-07-14 20:12:52 +0100
commitc73f38cd547cccc7930cf2e302c835e3f424ffd4 (patch)
treeeafc87355350d43926401b91abe98d4d2e1d4ec1 /usage.go
parent463902ef7d1219df0c6306a3838f4e003da92f91 (diff)
Improve handling of version flag
* Only use/show builtin `--version` flag if args are versioned with a non-empty `Version()` * If args define a `--version` flag, honor it and disable/hide the builtin version flag * Only return `ErrVersion` when using the builtin version flag
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go9
1 files changed, 8 insertions, 1 deletions
diff --git a/usage.go b/usage.go
index 0498910..a9f9844 100644
--- a/usage.go
+++ b/usage.go
@@ -209,6 +209,7 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error
// writeHelp writes the usage string for the given subcommand
func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
var positionals, longOptions, shortOptions, envOnlyOptions []*spec
+ var hasVersionOption bool
for _, spec := range cmd.specs {
switch {
case spec.positional:
@@ -243,6 +244,9 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
}
for _, spec := range longOptions {
p.printOption(w, spec)
+ if spec.long == "version" {
+ hasVersionOption = true
+ }
}
}
@@ -259,6 +263,9 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
fmt.Fprint(w, "\nGlobal options:\n")
for _, spec := range globals {
p.printOption(w, spec)
+ if spec.long == "version" {
+ hasVersionOption = true
+ }
}
}
@@ -269,7 +276,7 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
short: "h",
help: "display this help and exit",
})
- if p.version != "" {
+ if !hasVersionOption && p.version != "" {
p.printOption(w, &spec{
cardinality: zero,
long: "version",