arg/parse.go

382 lines
9.5 KiB
Go
Raw Normal View History

2015-11-01 13:34:22 -06:00
// Package arg parses command line arguments using the fields from a struct.
2015-11-01 15:24:35 -06:00
//
// For example,
2015-11-01 13:34:22 -06:00
//
2015-11-01 13:40:09 -06:00
// var args struct {
// Iter int
// Debug bool
// }
// arg.MustParse(&args)
2015-11-01 13:34:22 -06:00
//
// defines two command line arguments, which can be set using any of
//
2015-11-01 15:24:35 -06:00
// ./example --iter=1 --bar // bar is a boolean flag so its value is set to true
2015-11-01 13:40:09 -06:00
// ./example -iter 1 // bar will default to its zero value
// ./example --bar=true // foo will default to its zero value
2015-11-01 13:34:22 -06:00
//
// The fastest way to learn how to use go-arg is to read the examples below.
//
// Fields can be bool, string, any float type, or any signed or unsigned integer type.
// They can also be slices of any of the above, or slices of pointers to any of the above.
//
// Tags can be specified using the `arg` package name:
//
2015-11-01 13:40:09 -06:00
// var args struct {
// Input string `arg:"positional"`
// Log string `arg:"positional,required"`
// Debug bool `arg:"-d,help:turn on debug mode"`
// RealMode bool `arg:"--real"
// Wr io.Writer `arg:"-"`
// }
2015-11-01 13:34:22 -06:00
//
// The valid tag strings are `positional`, `required`, and `help`. Further, any tag string
// that starts with a single hyphen is the short form for an argument (e.g. `./example -d`),
// and any tag string that starts with two hyphens is the long form for the argument
// (instead of the field name). Fields can be excluded from processing with `arg:"-"`.
2015-10-31 20:26:58 -05:00
package arg
2015-10-31 18:15:24 -05:00
import (
2015-11-01 01:57:26 -05:00
"errors"
2015-10-31 18:15:24 -05:00
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
2015-10-31 20:26:58 -05:00
// spec represents a command line option
type spec struct {
dest reflect.Value
long string
short string
multiple bool
required bool
positional bool
help string
wasPresent bool
}
// ErrHelp indicates that -h or --help were provided
2015-11-01 01:57:26 -05:00
var ErrHelp = errors.New("help requested by user")
// MustParse processes command line arguments and exits upon failure
2015-10-31 20:26:58 -05:00
func MustParse(dest ...interface{}) {
2015-11-01 01:57:26 -05:00
p, err := NewParser(dest...)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = p.Parse(os.Args[1:])
if err == ErrHelp {
p.WriteUsage(os.Stdout)
os.Exit(0)
}
2015-10-31 18:15:24 -05:00
if err != nil {
p.Fail(err.Error())
2015-10-31 18:15:24 -05:00
}
}
// Parse processes command line arguments and stores them in dest
2015-10-31 20:26:58 -05:00
func Parse(dest ...interface{}) error {
2015-11-01 01:57:26 -05:00
p, err := NewParser(dest...)
if err != nil {
return err
2015-11-01 01:57:26 -05:00
}
return p.Parse(os.Args[1:])
2015-10-31 18:15:24 -05:00
}
2015-11-01 01:57:26 -05:00
// Parser represents a set of command line options with destination values
type Parser struct {
spec []*spec
}
2015-10-31 18:15:24 -05:00
2015-11-01 01:57:26 -05:00
// NewParser constructs a parser from a list of destination structs
func NewParser(dests ...interface{}) (*Parser, error) {
2015-10-31 18:15:24 -05:00
var specs []*spec
2015-10-31 20:26:58 -05:00
for _, dest := range dests {
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr {
panic(fmt.Sprintf("%s is not a pointer (did you forget an ampersand?)", v.Type()))
2015-10-31 18:15:24 -05:00
}
2015-10-31 20:26:58 -05:00
v = v.Elem()
if v.Kind() != reflect.Struct {
panic(fmt.Sprintf("%T is not a struct pointer", dest))
2015-10-31 18:15:24 -05:00
}
2015-10-31 20:26:58 -05:00
t := v.Type()
for i := 0; i < t.NumField(); i++ {
// Check for the ignore switch in the tag
field := t.Field(i)
tag := field.Tag.Get("arg")
if tag == "-" {
continue
2015-10-31 18:15:24 -05:00
}
2015-10-31 20:26:58 -05:00
spec := spec{
long: strings.ToLower(field.Name),
dest: v.Field(i),
}
2015-10-31 18:15:24 -05:00
2015-10-31 20:26:58 -05:00
// Get the scalar type for this field
scalarType := field.Type
if scalarType.Kind() == reflect.Slice {
spec.multiple = true
scalarType = scalarType.Elem()
if scalarType.Kind() == reflect.Ptr {
scalarType = scalarType.Elem()
2015-10-31 18:15:24 -05:00
}
2015-10-31 20:26:58 -05:00
}
// Check for unsupported types
switch scalarType.Kind() {
case reflect.Array, reflect.Chan, reflect.Func, reflect.Interface,
reflect.Map, reflect.Ptr, reflect.Struct,
reflect.Complex64, reflect.Complex128:
return nil, fmt.Errorf("%s.%s: %s fields are not supported", t.Name(), field.Name, scalarType.Kind())
}
// Look at the tag
if tag != "" {
for _, key := range strings.Split(tag, ",") {
var value string
if pos := strings.Index(key, ":"); pos != -1 {
value = key[pos+1:]
key = key[:pos]
}
2015-10-31 18:15:24 -05:00
2015-10-31 20:26:58 -05:00
switch {
case strings.HasPrefix(key, "--"):
spec.long = key[2:]
case strings.HasPrefix(key, "-"):
if len(key) != 2 {
return nil, fmt.Errorf("%s.%s: short arguments must be one character only", t.Name(), field.Name)
}
spec.short = key[1:]
case key == "required":
spec.required = true
case key == "positional":
spec.positional = true
case key == "help":
spec.help = value
default:
return nil, fmt.Errorf("unrecognized tag '%s' on field %s", key, tag)
2015-10-31 18:15:24 -05:00
}
}
}
2015-10-31 20:26:58 -05:00
specs = append(specs, &spec)
2015-10-31 18:15:24 -05:00
}
}
return &Parser{spec: specs}, nil
}
// Parse processes the given command line option, storing the results in the field
// of the structs from which NewParser was constructed
func (p *Parser) Parse(args []string) error {
// If -h or --help were specified then print usage
for _, arg := range args {
if arg == "-h" || arg == "--help" {
return ErrHelp
}
if arg == "--" {
break
}
}
// Process all command line arguments
err := process(p.spec, args)
if err != nil {
return err
}
// Validate
return validate(p.spec)
2015-10-31 18:15:24 -05:00
}
// process goes through arguments one-by-one, parses them, and assigns the result to
2015-11-01 01:57:26 -05:00
// the underlying struct field
func process(specs []*spec, args []string) error {
2015-10-31 19:05:14 -05:00
// construct a map from --option to spec
optionMap := make(map[string]*spec)
2015-10-31 18:15:24 -05:00
for _, spec := range specs {
2015-10-31 19:05:14 -05:00
if spec.positional {
continue
}
2015-10-31 18:15:24 -05:00
if spec.long != "" {
2015-10-31 19:05:14 -05:00
optionMap[spec.long] = spec
2015-10-31 18:15:24 -05:00
}
if spec.short != "" {
2015-10-31 19:05:14 -05:00
optionMap[spec.short] = spec
2015-10-31 18:15:24 -05:00
}
}
// process each string from the command line
var allpositional bool
var positionals []string
// must use explicit for loop, not range, because we manipulate i inside the loop
for i := 0; i < len(args); i++ {
arg := args[i]
if arg == "--" {
allpositional = true
continue
}
if !strings.HasPrefix(arg, "-") || allpositional {
positionals = append(positionals, arg)
continue
}
// check for an equals sign, as in "--foo=bar"
var value string
opt := strings.TrimLeft(arg, "-")
if pos := strings.Index(opt, "="); pos != -1 {
value = opt[pos+1:]
opt = opt[:pos]
}
// lookup the spec for this option
2015-10-31 19:05:14 -05:00
spec, ok := optionMap[opt]
2015-10-31 18:15:24 -05:00
if !ok {
return fmt.Errorf("unknown argument %s", arg)
}
spec.wasPresent = true
// deal with the case of multiple values
if spec.multiple {
var values []string
if value == "" {
2015-10-31 19:05:14 -05:00
for i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
values = append(values, args[i+1])
i++
2015-10-31 18:15:24 -05:00
}
} else {
values = append(values, value)
}
2015-10-31 20:26:58 -05:00
err := setSlice(spec.dest, values)
2015-10-31 19:05:14 -05:00
if err != nil {
return fmt.Errorf("error processing %s: %v", arg, err)
}
2015-10-31 18:15:24 -05:00
continue
}
// if it's a flag and it has no value then set the value to true
2015-10-31 20:26:58 -05:00
if spec.dest.Kind() == reflect.Bool && value == "" {
2015-10-31 18:15:24 -05:00
value = "true"
}
// if we have something like "--foo" then the value is the next argument
if value == "" {
if i+1 == len(args) || strings.HasPrefix(args[i+1], "-") {
return fmt.Errorf("missing value for %s", arg)
}
value = args[i+1]
i++
}
2015-10-31 20:26:58 -05:00
err := setScalar(spec.dest, value)
2015-10-31 18:15:24 -05:00
if err != nil {
return fmt.Errorf("error processing %s: %v", arg, err)
}
}
2015-10-31 19:05:14 -05:00
// process positionals
for _, spec := range specs {
if spec.positional {
if spec.multiple {
2015-10-31 20:26:58 -05:00
err := setSlice(spec.dest, positionals)
2015-10-31 19:05:14 -05:00
if err != nil {
2015-10-31 20:26:58 -05:00
return fmt.Errorf("error processing %s: %v", spec.long, err)
2015-10-31 19:05:14 -05:00
}
positionals = nil
} else if len(positionals) > 0 {
2015-10-31 20:26:58 -05:00
err := setScalar(spec.dest, positionals[0])
2015-10-31 19:05:14 -05:00
if err != nil {
2015-10-31 20:26:58 -05:00
return fmt.Errorf("error processing %s: %v", spec.long, err)
2015-10-31 19:05:14 -05:00
}
positionals = positionals[1:]
} else if spec.required {
2015-10-31 20:26:58 -05:00
return fmt.Errorf("%s is required", spec.long)
2015-10-31 19:05:14 -05:00
}
}
}
if len(positionals) > 0 {
return fmt.Errorf("too many positional arguments at '%s'", positionals[0])
}
2015-10-31 18:15:24 -05:00
return nil
}
// validate an argument spec after arguments have been parse
func validate(spec []*spec) error {
for _, arg := range spec {
2015-10-31 19:05:14 -05:00
if !arg.positional && arg.required && !arg.wasPresent {
2015-10-31 20:26:58 -05:00
return fmt.Errorf("--%s is required", arg.long)
2015-10-31 18:15:24 -05:00
}
}
return nil
}
// parse a value as the apropriate type and store it in the struct
2015-10-31 19:05:14 -05:00
func setSlice(dest reflect.Value, values []string) error {
if !dest.CanSet() {
return fmt.Errorf("field is not writable")
}
var ptr bool
elem := dest.Type().Elem()
if elem.Kind() == reflect.Ptr {
ptr = true
elem = elem.Elem()
}
for _, s := range values {
v := reflect.New(elem)
if err := setScalar(v.Elem(), s); err != nil {
return err
}
if ptr {
v = v.Addr()
}
dest.Set(reflect.Append(dest, v.Elem()))
}
2015-10-31 18:15:24 -05:00
return nil
}
// set a value from a string
func setScalar(v reflect.Value, s string) error {
if !v.CanSet() {
2015-10-31 19:05:14 -05:00
return fmt.Errorf("field is not exported")
2015-10-31 18:15:24 -05:00
}
switch v.Kind() {
case reflect.String:
v.Set(reflect.ValueOf(s))
case reflect.Bool:
x, err := strconv.ParseBool(s)
if err != nil {
return err
}
v.Set(reflect.ValueOf(x))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
x, err := strconv.ParseInt(s, 10, v.Type().Bits())
if err != nil {
return err
}
v.Set(reflect.ValueOf(x).Convert(v.Type()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
x, err := strconv.ParseUint(s, 10, v.Type().Bits())
if err != nil {
return err
}
v.Set(reflect.ValueOf(x).Convert(v.Type()))
case reflect.Float32, reflect.Float64:
x, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil {
return err
}
v.Set(reflect.ValueOf(x).Convert(v.Type()))
default:
return fmt.Errorf("not a scalar type: %s", v.Kind())
}
return nil
}