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