From 7a505b44c79ddbeab006d7ca72ff995a6bda6218 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 11 Nov 2014 22:24:04 -0800 Subject: [PATCH] Handle -h and --help differently (short text vs long text) --- cmd/ipfs2/main.go | 29 ++++++++++++++++++++++------- core/commands2/root.go | 6 ++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index cad7368c8..9d000ce48 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -92,13 +92,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) { // handle parse error (which means the commandline input was wrong, // e.g. incorrect number of args, or nonexistent subcommand) if err != nil { - help, _ := req.Option("help").Bool() + var longHelp, shortHelp bool + + if req != nil { + longHelp, _ = req.Option("help").Bool() + shortHelp, _ = req.Option("h").Bool() + } // if the -help flag wasn't specified, show the error message // or if a path was returned (user specified a valid subcommand), show the error message // (this means there was an option or argument error) if path != nil && len(path) > 0 { - if !help { + if !longHelp && !shortHelp { fmt.Printf(errorFormat, err) } } @@ -107,9 +112,10 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) { root = commands.Root } - // show the long help text if the help flag was specified, otherwise short help text + // show the long help text if the -help flag was specified or we are at the root command + // otherwise, show short help text helpFunc := cmdsCli.ShortHelp - if help { + if longHelp || len(path) == 0 { helpFunc = cmdsCli.LongHelp } @@ -147,14 +153,23 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) { } func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) { - help, err := req.Option("help").Bool() + longHelp, err := req.Option("help").Bool() if err != nil { return false, err } - if !help { + shortHelp, err := req.Option("h").Bool() + if err != nil { + return false, err + } + if !longHelp && !shortHelp { return false, nil } - err = cmdsCli.LongHelp("ipfs", root, req.Path(), os.Stdout) + helpFunc := cmdsCli.ShortHelp + if longHelp || len(req.Path()) == 0 { + helpFunc = cmdsCli.LongHelp + } + + err = helpFunc("ipfs", root, req.Path(), os.Stdout) if err != nil { return false, err } diff --git a/core/commands2/root.go b/core/commands2/root.go index 5d22c5fab..9356ac760 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -38,15 +38,13 @@ Plumbing commands: block Interact with raw blocks in the datastore object Interact with raw dag nodes - - -Use "ipfs --help" for more information about a command. `, Options: []cmds.Option{ cmds.StringOption("config", "c", "Path to the configuration file to use"), cmds.BoolOption("debug", "D", "Operate in debug mode"), - cmds.BoolOption("help", "h", "Show the command help text"), + cmds.BoolOption("help", "Show the full command help text"), + cmds.BoolOption("h", "Show a short version of the command help text"), cmds.BoolOption("local", "L", "Run the command locally, instead of using the daemon"), }, }