summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/parse.go b/parse.go
index a47f464..a82e377 100644
--- a/parse.go
+++ b/parse.go
@@ -1,12 +1,15 @@
package arg
import (
+ "encoding"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
+
+ scalar "github.com/alexflint/go-scalar"
)
// spec represents a command line option
@@ -442,3 +445,25 @@ func canParse(t reflect.Type) (parseable, boolean, multiple bool) {
return false, false, false
}
+
+var textUnmarshalerType = reflect.TypeOf([]encoding.TextUnmarshaler{}).Elem()
+
+// isScalar returns true if the type can be parsed from a single string
+func isScalar(t reflect.Type) (parseable, boolean bool) {
+ parseable = scalar.CanParse(t)
+ switch {
+ case t.Implements(textUnmarshalerType):
+ return parseable, false
+ case t.Kind() == reflect.Bool:
+ return parseable, true
+ case t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Bool:
+ return parseable, true
+ default:
+ return parseable, false
+ }
+}
+
+// set a value from a string
+func setScalar(v reflect.Value, s string) error {
+ return scalar.ParseValue(v, s)
+}