Commit bc8a97c1 authored by Matt Bell's avatar Matt Bell

commands: Cleanup up argument validation

parent d1f1d2f5
...@@ -3,7 +3,6 @@ package commands ...@@ -3,7 +3,6 @@ package commands
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"reflect" "reflect"
"strings" "strings"
...@@ -195,9 +194,6 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) { ...@@ -195,9 +194,6 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
} }
func (c *Command) CheckArguments(req Request) error { func (c *Command) CheckArguments(req Request) error {
// TODO: check file arguments
args := req.Arguments() args := req.Arguments()
// count required argument definitions // count required argument definitions
...@@ -217,13 +213,14 @@ func (c *Command) CheckArguments(req Request) error { ...@@ -217,13 +213,14 @@ func (c *Command) CheckArguments(req Request) error {
} }
// the value for this argument definition. can be nil if it wasn't provided by the caller // the value for this argument definition. can be nil if it wasn't provided by the caller
var v interface{} v, found := "", false
if valueIndex < len(args) { if valueIndex < len(args) {
v = args[valueIndex] v = args[valueIndex]
found = true
valueIndex++ valueIndex++
} }
err := checkArgValue(v, argDef) err := checkArgValue(v, found, argDef)
if err != nil { if err != nil {
return err return err
} }
...@@ -231,7 +228,7 @@ func (c *Command) CheckArguments(req Request) error { ...@@ -231,7 +228,7 @@ func (c *Command) CheckArguments(req Request) error {
// any additional values are for the variadic arg definition // any additional values are for the variadic arg definition
if argDef.Variadic && valueIndex < len(args)-1 { if argDef.Variadic && valueIndex < len(args)-1 {
for _, val := range args[valueIndex:] { for _, val := range args[valueIndex:] {
err := checkArgValue(val, argDef) err := checkArgValue(val, true, argDef)
if err != nil { if err != nil {
return err return err
} }
...@@ -248,26 +245,9 @@ func (c *Command) Subcommand(id string) *Command { ...@@ -248,26 +245,9 @@ func (c *Command) Subcommand(id string) *Command {
} }
// checkArgValue returns an error if a given arg value is not valid for the given Argument // checkArgValue returns an error if a given arg value is not valid for the given Argument
func checkArgValue(v interface{}, def Argument) error { func checkArgValue(v string, found bool, def Argument) error {
if v == nil { if !found && def.Required {
if def.Required { return fmt.Errorf("Argument '%s' is required", def.Name)
return fmt.Errorf("Argument '%s' is required", def.Name)
}
return nil
}
if def.Type == ArgFile {
_, ok := v.(io.Reader)
if !ok {
return fmt.Errorf("Argument '%s' isn't valid", def.Name)
}
} else if def.Type == ArgString {
_, ok := v.(string)
if !ok {
return fmt.Errorf("Argument '%s' must be a string", def.Name)
}
} }
return nil return nil
......
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