Compare commits

...

2 Commits

Author SHA1 Message Date
fe396b7537 feat: scope help printer to root command run 2026-01-14 13:00:03 +01:00
6eb04aa3c5 refactor: adapt test name 2026-01-14 12:59:14 +01:00
2 changed files with 12 additions and 8 deletions

View File

@@ -69,14 +69,14 @@ func printRunUsage() {
func buildRootCommand(cfg *config.Config) *cli.Command { func buildRootCommand(cfg *config.Config) *cli.Command {
helpPrinterOnce.Do(func() { helpPrinterOnce.Do(func() {
defaultHelpPrinter = cli.HelpPrinter defaultHelpPrinter = cli.HelpPrinter
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
if cmd, ok := data.(*cli.Command); ok && cmd.Root() == cmd {
printRootUsage()
return
}
defaultHelpPrinter(w, templ, data)
}
}) })
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
if cmd, ok := data.(*cli.Command); ok && cmd.Root() == cmd {
printRootUsage()
return
}
defaultHelpPrinter(w, templ, data)
}
root := &cli.Command{ root := &cli.Command{
Name: "goyco", Name: "goyco",
@@ -94,6 +94,10 @@ func buildRootCommand(cfg *config.Config) *cli.Command {
commands.SetJSONOutput(cmd.Bool("json")) commands.SetJSONOutput(cmd.Bool("json"))
return ctx, nil return ctx, nil
}, },
After: func(ctx context.Context, cmd *cli.Command) error {
cli.HelpPrinter = defaultHelpPrinter
return nil
},
Action: func(_ context.Context, cmd *cli.Command) error { Action: func(_ context.Context, cmd *cli.Command) error {
if cmd.NArg() == 0 { if cmd.NArg() == 0 {
printRootUsage() printRootUsage()

View File

@@ -132,7 +132,7 @@ func TestPrintRunUsage(t *testing.T) {
printRunUsage() printRunUsage()
} }
func TestDispatchCommand(t *testing.T) { func TestRootCommandDispatch(t *testing.T) {
t.Run("unknown command", func(t *testing.T) { t.Run("unknown command", func(t *testing.T) {
cfg := testutils.NewTestConfig() cfg := testutils.NewTestConfig()
cmd := buildRootCommand(cfg) cmd := buildRootCommand(cfg)