mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
cmd/ipfs2: Output generated help text on error or help flag
This commit is contained in:

committed by
Juan Batiz-Benet

parent
3255bb02d1
commit
165c69543b
@ -41,22 +41,49 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createRequest(args []string) (cmds.Request, *cmds.Command) {
|
func createRequest(args []string) (cmds.Request, *cmds.Command) {
|
||||||
req, root, cmd, err := cmdsCli.Parse(args, Root, commands.Root)
|
req, root, cmd, path, err := cmdsCli.Parse(args, Root, commands.Root)
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
var options cmds.Request
|
||||||
if cmd != nil {
|
if req != nil && root != nil {
|
||||||
if cmd.Help != "" {
|
var err2 error
|
||||||
fmt.Println(cmd.Help)
|
options, err2 = getOptions(req, root)
|
||||||
}
|
if err2 != nil {
|
||||||
} else {
|
fmt.Println(err2)
|
||||||
fmt.Println(Root.Help)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
options, err := getOptions(req, root)
|
// handle parse error (which means the commandline input was wrong,
|
||||||
|
// e.g. incorrect number of args, or nonexistent subcommand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
// if the -help flag wasn't specified, show the error message
|
||||||
|
if options != nil {
|
||||||
|
opt, _ := options.Option("help")
|
||||||
|
help, _ := opt.(bool)
|
||||||
|
if !help {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if path != nil && len(path) > 0 {
|
||||||
|
// if a path was returned (user specified a valid subcommand), show the error message
|
||||||
|
// (this means there was an option or argument error)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// when generating help for the root command, we don't want the autogenerated subcommand text
|
||||||
|
// (since we have better hand-made subcommand list in the root Help field)
|
||||||
|
if cmd == nil {
|
||||||
|
root = &*commands.Root
|
||||||
|
root.Subcommands = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate the help text for the command the user was trying to call (or root)
|
||||||
|
helpText, err := cmdsCli.HelpText("ipfs", root, path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(helpText)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +123,12 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
|
|||||||
|
|
||||||
if help, found := options.Option("help"); found {
|
if help, found := options.Option("help"); found {
|
||||||
if helpBool, ok := help.(bool); helpBool && ok {
|
if helpBool, ok := help.(bool); helpBool && ok {
|
||||||
fmt.Println(req.Command().Help)
|
helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Println(helpText)
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
fmt.Println("error: expected 'help' option to be a bool")
|
fmt.Println("error: expected 'help' option to be a bool")
|
||||||
|
Reference in New Issue
Block a user