This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse.go
116 lines (91 loc) · 2.8 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package jhanda
import (
"flag"
"fmt"
"github.com/pivotal-cf/jhanda/internal/parser"
"io/ioutil"
"reflect"
"time"
)
// Parse will populate the "receiver" object with the parsed values of the
// given "args". If the parser encounters a non-flag value, it will stop
// parsing and return the remainer of arguments. Parse will return an error in
// the case that the flags cannot be parsed, or a required flag is missing.
// The receiver is expected to be a pointer.
func Parse(receiver interface{}, args []string) ([]string, error) {
set := flag.NewFlagSet("", flag.ContinueOnError)
set.SetOutput(ioutil.Discard)
set.Usage = func() {}
ptr := reflect.ValueOf(receiver)
if ptr.Kind() != reflect.Ptr {
return nil, fmt.Errorf("unexpected non-pointer type %s for flag receiver", ptr.Kind())
}
v := ptr.Elem()
t := v.Type()
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("unexpected pointer to non-struct type %s", t.Kind())
}
flags, err := parseFields(t, set, v)
if err != nil {
return nil, err
}
err = set.Parse(args)
if err != nil {
return nil, err
}
set.Visit(func(ef *flag.Flag) {
for _, ff := range flags {
ff.SetIfMatched(ef)
}
})
for _, ff := range flags {
if err := ff.Validate(); err != nil {
return nil, err
}
}
return set.Args(), nil
}
func parseFields(t reflect.Type, set *flag.FlagSet, v reflect.Value) ([]*parser.Flag, error) {
var flags []*parser.Flag
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
var (
f *parser.Flag
err error
)
switch {
case field.Type.Kind() == reflect.Bool:
f, err = parser.NewBool(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Float64:
f, err = parser.NewFloat64(set, v.Field(i), field.Tag)
case field.Type == reflect.TypeOf(time.Duration(0)):
f, err = parser.NewDuration(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Int64:
f, err = parser.NewInt64(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Int:
f, err = parser.NewInt(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.String:
f, err = parser.NewString(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Uint64:
f, err = parser.NewUint64(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Uint:
f, err = parser.NewUint(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Slice:
f, err = parser.NewSlice(set, v.Field(i), field.Tag)
case field.Type.Kind() == reflect.Struct:
nestedFlags, err := parseFields(v.Field(i).Type(), set, v.Field(i))
if err != nil {
return nil, err
}
flags = append(flags, nestedFlags...)
continue
default:
return nil, fmt.Errorf("unexpected flag receiver field type %s", field.Type.Kind())
}
if err != nil {
return nil, err
}
flags = append(flags, f)
}
return flags, nil
}