Commit ec8be23c authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

cmds/main decomposed handleParseError

parent c25bf522
...@@ -92,17 +92,47 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) { ...@@ -92,17 +92,47 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
// handle parse error (which means the commandline input was wrong, // handle parse error (which means the commandline input was wrong,
// e.g. incorrect number of args, or nonexistent subcommand) // e.g. incorrect number of args, or nonexistent subcommand)
if err != nil { if err != nil {
return nil, nil, handleParseError(req, root, cmd, path)
}
configPath, err := getConfigRoot(req)
if err != nil {
return nil, nil, err
}
conf, err := getConfig(configPath)
if err != nil {
return nil, nil, err
}
ctx := req.Context()
ctx.ConfigRoot = configPath
ctx.Config = conf
// if no encoding was specified by user, default to plaintext encoding
// (if command doesn't support plaintext, use JSON instead)
if !req.Option("encoding").Found() {
if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
req.SetOption("encoding", cmds.Text)
} else {
req.SetOption("encoding", cmds.JSON)
}
}
return req, root, nil
}
func handleParseError(req cmds.Request, root *cmds.Command, cmd *cmds.Command, path []string) (err error) {
var longHelp, shortHelp bool var longHelp, shortHelp bool
if req != nil { if req != nil {
// help and h are defined in the root. We expect them to be bool. // help and h are defined in the root. We expect them to be bool.
longHelp, _, err = req.Option("help").Bool() longHelp, _, err = req.Option("help").Bool()
if err != nil { if err != nil {
return nil, nil, err return err
} }
shortHelp, _, err = req.Option("h").Bool() shortHelp, _, err = req.Option("h").Bool()
if err != nil { if err != nil {
return nil, nil, err return err
} }
} }
...@@ -130,33 +160,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) { ...@@ -130,33 +160,7 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
if htErr != nil { if htErr != nil {
fmt.Println(htErr) fmt.Println(htErr)
} }
return nil, nil, err return err
}
configPath, err := getConfigRoot(req)
if err != nil {
return nil, nil, err
}
conf, err := getConfig(configPath)
if err != nil {
return nil, nil, err
}
ctx := req.Context()
ctx.ConfigRoot = configPath
ctx.Config = conf
// if no encoding was specified by user, default to plaintext encoding
// (if command doesn't support plaintext, use JSON instead)
if !req.Option("encoding").Found() {
if req.Command().Marshallers != nil && req.Command().Marshallers[cmds.Text] != nil {
req.SetOption("encoding", cmds.Text)
} else {
req.SetOption("encoding", cmds.JSON)
}
}
return req, root, nil
} }
func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) { func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment