cmd/ipfs2: Output generated help text on error or help flag
Matt Bell committed
Nov 8, 2014 at 03:18 UTC
165c69543bb37c64b6dca975cbfda8a4d5116ee1
1 file changed
+45
-13
cmd/ipfs2/main.go
+45
-13
@@ -41,22 +41,49 @@ func main() {
41
}
42
43
func createRequest(args []string) (cmds.Request, *cmds.Command) {
44
- req, root, cmd, err := cmdsCli.Parse(args, Root, commands.Root)
45
- if err != nil {
46
- fmt.Println(err)
47
- if cmd != nil {
48
- if cmd.Help != "" {
49
- fmt.Println(cmd.Help)
50
- }
51
- } else {
52
- fmt.Println(Root.Help)
44
+ req, root, cmd, path, err := cmdsCli.Parse(args, Root, commands.Root)
45
+
46
+ var options cmds.Request
47
+ if req != nil && root != nil {
48
+ var err2 error
49
+ options, err2 = getOptions(req, root)
50
+ if err2 != nil {
51
+ fmt.Println(err2)
52
+ os.Exit(1)
53
}
54
- os.Exit(1)
54
}
55
57
- options, err := getOptions(req, root)
56
+ // handle parse error (which means the commandline input was wrong,
57
+ // e.g. incorrect number of args, or nonexistent subcommand)
58
if err != nil {
59
- fmt.Println(err)
59
+ // if the -help flag wasn't specified, show the error message
60
+ if options != nil {
61
+ opt, _ := options.Option("help")
62
+ help, _ := opt.(bool)
63
+ if !help {
64
+ fmt.Println(err)
65
+ }
66
+
67
+ } else if path != nil && len(path) > 0 {
68
+ // if a path was returned (user specified a valid subcommand), show the error message
69
+ // (this means there was an option or argument error)
70
+ fmt.Println(err)
71
+ }
72
+
73
+ // when generating help for the root command, we don't want the autogenerated subcommand text
74
+ // (since we have better hand-made subcommand list in the root Help field)
75
+ if cmd == nil {
76
+ root = &*commands.Root
77
+ root.Subcommands = nil
78
+ }
79
+
80
+ // generate the help text for the command the user was trying to call (or root)
81
+ helpText, err := cmdsCli.HelpText("ipfs", root, path)
82
+ if err != nil {
83
+ fmt.Println(err)
84
+ } else {
85
+ fmt.Println(helpText)
86
+ }
87
os.Exit(1)
88
}
89
@@ -96,7 +123,12 @@ func handleOptions(req cmds.Request, root *cmds.Command) {
123
124
if help, found := options.Option("help"); found {
125
if helpBool, ok := help.(bool); helpBool && ok {
99
- fmt.Println(req.Command().Help)
126
+ helpText, err := cmdsCli.HelpText("ipfs", root, req.Path())
127
+ if err != nil {
128
+ fmt.Println(err.Error())
129
+ } else {
130
+ fmt.Println(helpText)
131
+ }
132
os.Exit(0)
133
} else if !ok {
134
fmt.Println("error: expected 'help' option to be a bool")