Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,12 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
}
}

if p.description != "" {
if cmd.help != "" {
fmt.Fprintln(w, cmd.help)
} else if p.description != "" {
fmt.Fprintln(w, p.description)
}

p.writeUsageForSubcommand(w, cmd)

// write the list of positionals
Expand Down
31 changes: 31 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,37 @@ Options:
assert.Equal(t, expectedUsage, usage.String())
}

func TestHelpWithSubcommandWithHelpText(t *testing.T) {
expectedHelp := `
Description of what this subcommand does
Usage: example child [--values VALUES]

Options:
--values VALUES Values

Global options:
--verbose, -v verbosity level
--help, -h display this help and exit
`

var args struct {
Verbose bool `arg:"-v" help:"verbosity level"`
Child *struct {
Values []float64 `help:"Values"`
} `arg:"subcommand:child" help:"Description of what this subcommand does"`
}

os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

_ = p.Parse([]string{"child"})

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
}

func TestUsageWithNestedSubcommands(t *testing.T) {
expectedUsage := "Usage: example child nested [--enable] OUTPUT"

Expand Down