mirror of
https://github.com/syncthing/syncthing.git
synced 2025-12-23 22:18:14 -05:00
* 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 <command> and [flags] like Kong. Signed-off-by: André Colomb <src@andre.colomb.de>
This commit is contained in:
@@ -19,6 +19,53 @@ import (
|
|||||||
"github.com/urfave/cli"
|
"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}} <command> [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}} <command>{{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 {
|
type configHandler struct {
|
||||||
original, cfg config.Configuration
|
original, cfg config.Configuration
|
||||||
client APIClient
|
client APIClient
|
||||||
@@ -29,13 +76,18 @@ type configCommand struct {
|
|||||||
Args []string `arg:"" default:"-h"`
|
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 := cli.NewApp()
|
||||||
app.Name = "syncthing"
|
app.Name = "syncthing cli config"
|
||||||
app.Author = "The Syncthing Authors"
|
app.HelpName = "syncthing cli config"
|
||||||
|
app.Description = outerCtx.Selected().Help
|
||||||
app.Metadata = map[string]interface{}{
|
app.Metadata = map[string]interface{}{
|
||||||
"clientFactory": ctx.clientFactory,
|
"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 := new(configHandler)
|
||||||
h.client, h.err = ctx.clientFactory.getClient()
|
h.client, h.err = ctx.clientFactory.getClient()
|
||||||
@@ -56,6 +108,8 @@ func (c *configCommand) Run(ctx Context, _ *kong.Context) error {
|
|||||||
|
|
||||||
app.Commands = commands
|
app.Commands = commands
|
||||||
app.HideHelp = true
|
app.HideHelp = true
|
||||||
|
// Explicitly re-add help only as flags, not as commands
|
||||||
|
app.Flags = []cli.Flag{cli.HelpFlag}
|
||||||
app.Before = h.configBefore
|
app.Before = h.configBefore
|
||||||
app.After = h.configAfter
|
app.After = h.configAfter
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user