From ce884e5d7253ed82379f918e169438eb64fcece0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Mon, 24 Nov 2025 16:49:42 +0100 Subject: [PATCH] chore(cli): clean up generated usage strings for config commands (fixes #10462) (#10463) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Show proper subcommand prefix in generated config CLI. * Remove useless author info and copy command group description. * Really accept (implicit) -h and --help flags. These were disabled by HideHelp, leading to an error message in every usage output. This way, the flags get documented as well. * Override AppHelpTemplate to better match Kong's style. * Override (Sub)commandHelpTemplate to better match Kong's style. * Use and [flags] like Kong. Signed-off-by: André Colomb --- cmd/syncthing/cli/config.go | 60 +++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/cmd/syncthing/cli/config.go b/cmd/syncthing/cli/config.go index 79cde4848..1bb6dc000 100644 --- a/cmd/syncthing/cli/config.go +++ b/cmd/syncthing/cli/config.go @@ -19,6 +19,53 @@ import ( "github.com/urfave/cli" ) +// Try to mimic the kong output format through custom help templates +var customAppHelpTemplate = `Usage: {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .Commands}} [flags]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}} + +{{.Description}}{{if .VisibleFlags}} + +Flags: + {{range $index, $option := .VisibleFlags}}{{if $index}} + {{end}}{{$option}}{{end}}{{end}}{{if .VisibleCommands}} + +Commands:{{range .VisibleCategories}}{{if .Name}} + + {{.Name}}:{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}} +` + +var customCommandHelpTemplate = `Usage: {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [flags]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}} + +{{.Usage}}{{if .VisibleFlags}} + +Flags: + {{range $index, $option := .VisibleFlags}}{{if $index}} + {{end}}{{$option}}{{end}}{{end}}{{if .Category}} + +Category: + {{.Category}}{{end}}{{if .Description}} + +{{.Description}}{{end}} +` + +var customSubcommandHelpTemplate = `Usage: {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}} [flags]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}} + +{{.Description}}{{else}}{{if .Usage}} + +{{.Usage}}{{end}}{{end}}{{if .VisibleFlags}} + +Flags: + {{range $index, $option := .VisibleFlags}}{{if $index}} + {{end}}{{$option}}{{end}}{{end}}{{if .VisibleCommands}} + +Commands:{{range .VisibleCategories}}{{if .Name}} + + {{.Name}}:{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}} +` + type configHandler struct { original, cfg config.Configuration client APIClient @@ -29,13 +76,18 @@ type configCommand struct { Args []string `arg:"" default:"-h"` } -func (c *configCommand) Run(ctx Context, _ *kong.Context) error { +func (c *configCommand) Run(ctx Context, outerCtx *kong.Context) error { app := cli.NewApp() - app.Name = "syncthing" - app.Author = "The Syncthing Authors" + app.Name = "syncthing cli config" + app.HelpName = "syncthing cli config" + app.Description = outerCtx.Selected().Help app.Metadata = map[string]interface{}{ "clientFactory": ctx.clientFactory, } + app.CustomAppHelpTemplate = customAppHelpTemplate + // Override global templates, as this is out only usage of the package + cli.CommandHelpTemplate = customCommandHelpTemplate + cli.SubcommandHelpTemplate = customSubcommandHelpTemplate h := new(configHandler) h.client, h.err = ctx.clientFactory.getClient() @@ -56,6 +108,8 @@ func (c *configCommand) Run(ctx Context, _ *kong.Context) error { app.Commands = commands app.HideHelp = true + // Explicitly re-add help only as flags, not as commands + app.Flags = []cli.Flag{cli.HelpFlag} app.Before = h.configBefore app.After = h.configAfter