From 3352aeee15e2c2e703e89c639bc548e003cd74be Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Wed, 12 Nov 2014 19:21:53 -0800 Subject: [PATCH] cmds2: fixed show help on root + noncallable --- cmd/ipfs2/ipfs.go | 4 ++-- cmd/ipfs2/main.go | 23 ++++++++++++----------- commands/cli/helptext.go | 5 +++++ commands/cli/parse.go | 30 ++++++------------------------ core/commands2/root.go | 8 +++++--- 5 files changed, 30 insertions(+), 40 deletions(-) diff --git a/cmd/ipfs2/ipfs.go b/cmd/ipfs2/ipfs.go index 5e8dcff68..d2286e408 100644 --- a/cmd/ipfs2/ipfs.go +++ b/cmd/ipfs2/ipfs.go @@ -6,8 +6,8 @@ import ( ) var Root = &cmds.Command{ - Options: commands.Root.Options, - Help: commands.Root.Help, + Options: commands.Root.Options, + Helptext: commands.Root.Helptext, } var rootSubcommands = map[string]*cmds.Command{ diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index 404c8bc2e..565de9b37 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -68,7 +68,7 @@ func main() { } // parse the commandline into a command invocation - err = invoc.Parse(os.Args[1:]) + parseErr := invoc.Parse(os.Args[1:]) // BEFORE handling the parse error, if we have enough information // AND the user requested help, print it out and exit @@ -84,10 +84,18 @@ func main() { } } - // ok now handle handle parse error (which means cli input was wrong, + // here we handle the cases where + // - commands with no Run func are invoked directly. + // - the main command is invoked. + if invoc.cmd == nil || invoc.cmd.Run == nil { + printHelp(false) + os.Exit(0) + } + + // ok now handle parse error (which means cli input was wrong, // e.g. incorrect number of args, or nonexistent subcommand) - if err != nil { - printErr(err) + if parseErr != nil { + printErr(parseErr) // this was a user error, print help. if invoc.cmd != nil { @@ -248,13 +256,6 @@ func isClientError(err error) bool { if !ok { return false } - - // here we handle the case where commands with - // no Run func are invoked directly. As help requests. - if err == cmds.ErrNotCallable { - return true - } - return cmdErr.Code == cmds.ErrClient } diff --git a/commands/cli/helptext.go b/commands/cli/helptext.go index a7c8e378f..2a305fb4a 100644 --- a/commands/cli/helptext.go +++ b/commands/cli/helptext.go @@ -195,6 +195,11 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer return err } + // default cmd to root if there is no path + if path == nil && cmd == nil { + cmd = root + } + pathStr := rootName if len(path) > 0 { pathStr += " " + strings.Join(path, " ") diff --git a/commands/cli/parse.go b/commands/cli/parse.go index c90c6f86a..8e1dba86f 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -14,36 +14,18 @@ var ErrInvalidSubcmd = errors.New("subcommand not found") // Parse parses the input commandline string (cmd, flags, and args). // returns the corresponding command Request object. -// Multiple root commands are supported: // Parse will search each root to find the one that best matches the requested subcommand. // TODO: get rid of extraneous return values (e.g. we ended up not needing the root value anymore) // TODO: get rid of multiple-root support, we should only need one now -func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) { - var root, cmd *cmds.Command - var path, stringArgs []string - var opts map[string]interface{} - +func Parse(input []string, root *cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, []string, error) { // use the root that matches the longest path (most accurately matches request) - maxLength := 0 - for _, root2 := range roots { - path2, input2, cmd2 := parsePath(input, root2) - opts2, stringArgs2, err := parseOptions(input2) - if err != nil { - return nil, root, cmd2, path2, err - } - - length := len(path2) - if length > maxLength { - maxLength = length - root = root2 - path = path2 - cmd = cmd2 - opts = opts2 - stringArgs = stringArgs2 - } + path, input, cmd := parsePath(input, root) + opts, stringArgs, err := parseOptions(input) + if err != nil { + return nil, root, cmd, path, err } - if maxLength == 0 { + if len(path) == 0 { return nil, root, nil, path, ErrInvalidSubcmd } diff --git a/core/commands2/root.go b/core/commands2/root.go index 29bce74c9..d2372dcc5 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -13,8 +13,10 @@ type TestOutput struct { } var Root = &cmds.Command{ - Description: "Global P2P Merkle-DAG filesystem", - SubcommandHelp: `Basic commands: + Helptext: cmds.HelpText{ + Tagline: "Global P2P Merkle-DAG filesystem", + ShortDescription: ` +Basic commands: init Initialize ipfs local configurationx add Add an object to ipfs @@ -39,7 +41,7 @@ Plumbing commands: block Interact with raw blocks in the datastore object Interact with raw dag nodes `, - + }, Options: []cmds.Option{ cmds.StringOption("config", "c", "Path to the configuration file to use"), cmds.BoolOption("debug", "D", "Operate in debug mode"),