From 7ddc2c9e95ac2b46cf84ef9776173ad70b5117cd Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 17 Jul 2018 07:04:43 +0200 Subject: cmd/swarm: add implicit subcommand help (fix #786) (#788) * cmd/swarm: add implicit subcommand help (fix #786) * cmd/swarm: moved implicit help to a recursive func --- cmd/swarm/main.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'cmd') diff --git a/cmd/swarm/main.go b/cmd/swarm/main.go index 5b0767951..d66191628 100644 --- a/cmd/swarm/main.go +++ b/cmd/swarm/main.go @@ -190,6 +190,15 @@ var ( SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set" ) +// this help command gets added to any subcommand that does not define it explicitly +var defaultSubcommandHelp = cli.Command{ + Action: func(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "", 1) }, + CustomHelpTemplate: helpTemplate, + Name: "help", + Usage: "shows this help", + Hidden: true, +} + var defaultNodeConfig = node.DefaultConfig // This init function sets defaults so cmd/swarm can run alongside geth. @@ -377,6 +386,11 @@ pv(1) tool to get a progress bar: // See config.go DumpConfigCommand, } + + // append a hidden help subcommand to all commands that have subcommands + // if a help command was already defined above, that one will take precedence. + addDefaultHelpSubcommands(app.Commands) + sort.Sort(cli.CommandsByName(app.Commands)) app.Flags = []cli.Flag{ @@ -613,3 +627,16 @@ func injectBootnodes(srv *p2p.Server, nodes []string) { srv.AddPeer(n) } } + +// addDefaultHelpSubcommand scans through defined CLI commands and adds +// a basic help subcommand to each +// if a help command is already defined, it will take precedence over the default. +func addDefaultHelpSubcommands(commands []cli.Command) { + for i := range commands { + cmd := &commands[i] + if cmd.Subcommands != nil { + cmd.Subcommands = append(cmd.Subcommands, defaultSubcommandHelp) + addDefaultHelpSubcommands(cmd.Subcommands) + } + } +} -- cgit v1.2.3