Display subcommand-relevant description for --help

This commit is contained in:
ymorgenstern 2023-08-07 15:41:02 +03:00 committed by GitHub
parent 660b9045e1
commit b9f119afb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -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) fmt.Fprintln(w, p.description)
} }
p.writeUsageForSubcommand(w, cmd) p.writeUsageForSubcommand(w, cmd)
// write the list of positionals // write the list of positionals

View File

@ -415,6 +415,37 @@ Options:
assert.Equal(t, expectedUsage, usage.String()) 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) { func TestUsageWithNestedSubcommands(t *testing.T) {
expectedUsage := "Usage: example child nested [--enable] OUTPUT" expectedUsage := "Usage: example child nested [--enable] OUTPUT"