diff options
| author | Dylan Allbee <[email protected]> | 2020-01-23 21:09:21 -0800 |
|---|---|---|
| committer | Dylan Allbee <[email protected]> | 2020-01-25 11:53:34 -0800 |
| commit | 5df19ebe00e88d443e062c6661f52b7069f486d7 (patch) | |
| tree | c9f7cdbf1e8d9b1163d1a0a1f4198ccfd9f18537 /usage.go | |
| parent | 338e831a8474c442b2c5e75eefed7b602276faa8 (diff) | |
Use command passed into p.Parse(...) write methods
It is currently impossible to programatically write help and usage
messages for subcommands, due to parser.WriteHelp and parser.WriteUsage
not taking the state of the parser into account.
Check for the existence of p.lastCmd and use it for the writers when
available.
Enables ability to write unit tests for subcommand help.
Diffstat (limited to 'usage.go')
| -rw-r--r-- | usage.go | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -27,7 +27,11 @@ func (p *Parser) failWithCommand(msg string, cmd *command) { // WriteUsage writes usage information to the given writer func (p *Parser) WriteUsage(w io.Writer) { - p.writeUsageForCommand(w, p.cmd) + cmd := p.cmd + if p.lastCmd != nil { + cmd = p.lastCmd + } + p.writeUsageForCommand(w, cmd) } // writeUsageForCommand writes usage information for the given subcommand @@ -116,7 +120,11 @@ func printTwoCols(w io.Writer, left, help string, defaultVal string) { // WriteHelp writes the usage string followed by the full help string for each option func (p *Parser) WriteHelp(w io.Writer) { - p.writeHelpForCommand(w, p.cmd) + cmd := p.cmd + if p.lastCmd != nil { + cmd = p.lastCmd + } + p.writeHelpForCommand(w, cmd) } // writeHelp writes the usage string for the given subcommand |
