Skip to content
Closed
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
7 changes: 6 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ func ParseAndRun(ctx context.Context, root *Command, args []string, options *Run
if err := Parse(root, args); err != nil {
if errors.Is(err, ErrHelp) {
options = checkAndSetRunOptions(options)
_, _ = fmt.Fprintln(options.Stdout, DefaultUsage(root))
cmd := root.terminal()
if cmd.UsageFunc != nil {
_, _ = fmt.Fprintln(options.Stdout, cmd.UsageFunc(cmd))
} else {
_, _ = fmt.Fprintln(options.Stdout, DefaultUsage(root))
}
return nil
}
return err
Expand Down
32 changes: 32 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,36 @@ func TestRun(t *testing.T) {
require.Equal(t, val, GetFlag[string](root.state, "text"))
}
})
t.Run("ParseAndRun uses UsageFunc on help", func(t *testing.T) {
t.Parallel()

root := &Command{
Name: "myapp",
ShortHelp: "my application",
UsageFunc: func(c *Command) string {
return "custom usage output"
},
Exec: func(ctx context.Context, s *State) error { return nil },
}

stdout := bytes.NewBuffer(nil)
err := ParseAndRun(context.Background(), root, []string{"-help"}, &RunOptions{Stdout: stdout})
require.NoError(t, err)
require.Contains(t, stdout.String(), "custom usage output")
})
t.Run("ParseAndRun falls back to DefaultUsage without UsageFunc", func(t *testing.T) {
t.Parallel()

root := &Command{
Name: "myapp",
ShortHelp: "my application",
Exec: func(ctx context.Context, s *State) error { return nil },
}

stdout := bytes.NewBuffer(nil)
err := ParseAndRun(context.Background(), root, []string{"-help"}, &RunOptions{Stdout: stdout})
require.NoError(t, err)
require.Contains(t, stdout.String(), "my application")
require.Contains(t, stdout.String(), "Usage:")
})
}
4 changes: 0 additions & 4 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func DefaultUsage(root *Command) string {

var b strings.Builder

if terminalCmd.UsageFunc != nil {
return terminalCmd.UsageFunc(terminalCmd)
}

if terminalCmd.ShortHelp != "" {
b.WriteString(terminalCmd.ShortHelp)
b.WriteString("\n\n")
Expand Down
24 changes: 24 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,27 @@ func TestWriteFlagSection(t *testing.T) {
require.NotContains(t, output, "Inherited Flags:")
})
}

func TestDefaultUsageComposableFromUsageFunc(t *testing.T) {
t.Parallel()

cmd := &Command{
Name: "myapp",
ShortHelp: "my application",
Exec: func(ctx context.Context, s *State) error { return nil },
}
cmd.UsageFunc = func(c *Command) string {
// Calling DefaultUsage from within UsageFunc should not recurse infinitely.
s := DefaultUsage(c)
return s + "\n\nExamples:\n myapp --verbose"
}

err := Parse(cmd, []string{})
require.NoError(t, err)

output := cmd.UsageFunc(cmd)
require.Contains(t, output, "my application")
require.Contains(t, output, "Usage:")
require.Contains(t, output, "Examples:")
require.Contains(t, output, "myapp --verbose")
}