diff options
| author | Alex Flint <[email protected]> | 2023-07-14 15:52:33 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-14 15:52:33 -0400 |
| commit | 660b9045e10d4d2f8474e2739e6b0013636c065c (patch) | |
| tree | eafc87355350d43926401b91abe98d4d2e1d4ec1 /parse.go | |
| parent | 463902ef7d1219df0c6306a3838f4e003da92f91 (diff) | |
| parent | c73f38cd547cccc7930cf2e302c835e3f424ffd4 (diff) | |
Merge pull request #223 from hhromic/fix-version-flag
Improve handling of version flag
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -69,10 +69,10 @@ type command struct { parent *command } -// ErrHelp indicates that -h or --help were provided +// ErrHelp indicates that the builtin -h or --help were provided var ErrHelp = errors.New("help requested by user") -// ErrVersion indicates that --version was provided +// ErrVersion indicates that the builtin --version was provided var ErrVersion = errors.New("version requested by user") // for monkey patching in example code @@ -591,6 +591,15 @@ func (p *Parser) process(args []string) error { } } + // determine if the current command has a version option spec + var hasVersionOption bool + for _, spec := range curCmd.specs { + if spec.long == "version" { + hasVersionOption = true + break + } + } + // process each string from the command line var allpositional bool var positionals []string @@ -648,7 +657,9 @@ func (p *Parser) process(args []string) error { case "-h", "--help": return ErrHelp case "--version": - return ErrVersion + if !hasVersionOption && p.version != "" { + return ErrVersion + } } // check for an equals sign, as in "--foo=bar" |
