Skip to content

Commit 3423608

Browse files
author
zuoxf1
committed
refine Usage to print last command help
1 parent 156b4c5 commit 3423608

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

argparse.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Command struct {
1919
args []*arg
2020
commands []*Command
2121
parsed bool
22+
happened bool
2223
parent *Command
2324
}
2425

@@ -255,7 +256,7 @@ func (o *Command) Selector(short string, long string, options []string, opts *Op
255256
// Happened shows whether Command was specified on CLI arguments or not. If Command did not "happen", then
256257
// all its descendant commands and arguments are not parsed. Returns a boolean value.
257258
func (o *Command) Happened() bool {
258-
return o.parsed
259+
return o.happened
259260
}
260261

261262
// Usage returns a multiline string that is the same as a help message for this Parser or Command.
@@ -266,6 +267,11 @@ func (o *Command) Happened() bool {
266267
// Accepts an interface that can be error, string or fmt.Stringer that will be prepended to a message.
267268
// All other interface types will be ignored
268269
func (o *Command) Usage(msg interface{}) string {
270+
for _, cmd := range o.commands {
271+
if cmd.Happened() {
272+
return cmd.Usage(msg)
273+
}
274+
}
269275
var result string
270276
// Stay classy
271277
maxWidth := 80

command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (o *Command) parse(args *[]string) error {
6363
}
6464
}
6565

66+
// Set happened status to true when command happend
67+
o.happened = true
68+
6669
// Reduce arguments by removing Command name
6770
*args = (*args)[1:]
6871

0 commit comments

Comments
 (0)