mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 10:57:42 +08:00
cmds2: fixed show help on root + noncallable
This commit is contained in:
parent
c46d4c8953
commit
3352aeee15
@ -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{
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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, " ")
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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 <path> 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"),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user