cmds/main decomposed handleParseError
Juan Batiz-Benet committed
Nov 12, 2014 at 01:40 UTC
ec8be23cbc22a4890516f4cfa3a55181140980ac
1 file changed
+43
-39
cmd/ipfs2/main.go
+43
-39
@@ -92,45 +92,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
92
// handle parse error (which means the commandline input was wrong,
93
// e.g. incorrect number of args, or nonexistent subcommand)
94
if err != nil {
95
- var longHelp, shortHelp bool
96
-
97
- if req != nil {
98
- // help and h are defined in the root. We expect them to be bool.
99
- longHelp, _, err = req.Option("help").Bool()
100
- if err != nil {
101
- return nil, nil, err
102
- }
103
- shortHelp, _, err = req.Option("h").Bool()
104
- if err != nil {
105
- return nil, nil, err
106
- }
107
- }
108
-
109
- // if the -help flag wasn't specified, show the error message
110
- // or if a path was returned (user specified a valid subcommand), show the error message
111
- // (this means there was an option or argument error)
112
- if path != nil && len(path) > 0 {
113
- if !longHelp && !shortHelp {
114
- fmt.Printf(errorFormat, err)
115
- }
116
- }
117
-
118
- if cmd == nil {
119
- root = commands.Root
120
- }
121
-
122
- // show the long help text if the -help flag was specified or we are at the root command
123
- // otherwise, show short help text
124
- helpFunc := cmdsCli.ShortHelp
125
- if longHelp || len(path) == 0 {
126
- helpFunc = cmdsCli.LongHelp
127
- }
128
-
129
- htErr := helpFunc("ipfs", root, path, os.Stdout)
130
- if htErr != nil {
131
- fmt.Println(htErr)
132
- }
133
- return nil, nil, err
95
+ return nil, nil, handleParseError(req, root, cmd, path)
96
}
97
98
configPath, err := getConfigRoot(req)
@@ -159,6 +121,48 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
121
return req, root, nil
122
}
123
124
+func handleParseError(req cmds.Request, root *cmds.Command, cmd *cmds.Command, path []string) (err error) {
125
+ var longHelp, shortHelp bool
126
+
127
+ if req != nil {
128
+ // help and h are defined in the root. We expect them to be bool.
129
+ longHelp, _, err = req.Option("help").Bool()
130
+ if err != nil {
131
+ return err
132
+ }
133
+ shortHelp, _, err = req.Option("h").Bool()
134
+ if err != nil {
135
+ return err
136
+ }
137
+ }
138
+
139
+ // if the -help flag wasn't specified, show the error message
140
+ // or if a path was returned (user specified a valid subcommand), show the error message
141
+ // (this means there was an option or argument error)
142
+ if path != nil && len(path) > 0 {
143
+ if !longHelp && !shortHelp {
144
+ fmt.Printf(errorFormat, err)
145
+ }
146
+ }
147
+
148
+ if cmd == nil {
149
+ root = commands.Root
150
+ }
151
+
152
+ // show the long help text if the -help flag was specified or we are at the root command
153
+ // otherwise, show short help text
154
+ helpFunc := cmdsCli.ShortHelp
155
+ if longHelp || len(path) == 0 {
156
+ helpFunc = cmdsCli.LongHelp
157
+ }
158
+
159
+ htErr := helpFunc("ipfs", root, path, os.Stdout)
160
+ if htErr != nil {
161
+ fmt.Println(htErr)
162
+ }
163
+ return err
164
+}
165
+
166
func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
167
longHelp, _, err := req.Option("help").Bool()
168
if err != nil {