Skip to content

Commit

Permalink
Add help template support
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Dec 7, 2023
1 parent 6c72759 commit bda5fdd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2026,8 +2026,9 @@ func TestCommand_Run_SubcommandFullPath(t *testing.T) {
out := &bytes.Buffer{}

subCmd := &Command{
Name: "bar",
Usage: "does bar things",
Name: "bar",
Usage: "does bar things",
ArgsUsage: "[arguments...]",
}

cmd := &Command{
Expand Down
3 changes: 3 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func ExampleCommand_Run_appHelp() {
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
ArgsUsage: "[arguments...]",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("i like to describe things")
return nil
Expand Down Expand Up @@ -162,6 +163,7 @@ func ExampleCommand_Run_commandHelp() {
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
ArgsUsage: "[arguments...]",
Action: func(context.Context, *cli.Command) error {
fmt.Println("i like to describe things")
return nil
Expand Down Expand Up @@ -220,6 +222,7 @@ func ExampleCommand_Run_subcommandNoAction() {
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
ArgsUsage: "[arguments...]",
Description: "This is how we describe describeit the function",
},
},
Expand Down
5 changes: 5 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
if _, err := t.New("helpNameTemplate").Parse(helpNameTemplate); err != nil {
handleTemplateError(err)
}

if _, err := t.New("argsTemplate").Parse(argsTemplate); err != nil {
handleTemplateError(err)
}

Check warning on line 383 in help.go

View check run for this annotation

Codecov / codecov/patch

help.go#L382-L383

Added lines #L382 - L383 were not covered by tests

if _, err := t.New("usageTemplate").Parse(usageTemplate); err != nil {
handleTemplateError(err)
}
Expand Down
3 changes: 2 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cli

var helpNameTemplate = `{{$v := offset .FullName 6}}{{wrap .FullName 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}}`
var usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleFlags}} [command [command options]]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}`
var argsTemplate = `{{if .Arguments}}{{range .Arguments}}{{.Usage}}{{end}}{{end}}`
var usageTemplate = `{{if .UsageText}}{{wrap .UsageText 3}}{{else}}{{.FullName}}{{if .VisibleFlags}} [command [command options]]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}{{template "argsTemplate" .}}{{end}}{{end}}`
var descriptionTemplate = `{{wrap .Description 3}}`
var authorsTemplate = `{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
{{range $index, $author := .Authors}}{{if $index}}
Expand Down

0 comments on commit bda5fdd

Please sign in to comment.