-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflagset.go
174 lines (144 loc) · 3.33 KB
/
flagset.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package flagstruct
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"reflect"
)
// A FlagSet represents a set of defined flags.
type FlagSet struct {
*flag.FlagSet
name string
errorHandling flag.ErrorHandling
output io.Writer
env map[string]Value
}
// NewFlagSet returns a new, empty flag set with the specified name and error
// handling property.
func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet {
return &FlagSet{flag.NewFlagSet(name, errorHandling), name, errorHandling, nil, map[string]Value{}}
}
// Configure sets up enhanced usage help, loads a structure, parses environment
// and parses flags.
func (s *FlagSet) Configure(conf interface{}, arguments []string) error {
err := s.Struct(conf)
if err != nil {
return err
}
err = s.ParseEnv()
if err != nil {
return err
}
err = s.Parse(arguments)
if err != nil {
return err
}
return nil
}
// MakeStructUsage creates a usage function from a struct.
func (s *FlagSet) MakeStructUsage(conf interface{}) func() {
// Cache struct usage (otherwise default values change)
buf, oldout := bytes.Buffer{}, s.output
s.output = &buf
s.PrintStruct(conf)
s.output = oldout
return func() {
if s.name == "" {
fmt.Fprintf(s.out(), "Usage:\n")
} else {
fmt.Fprintf(s.out(), "Usage of %s:\n", s.name)
}
fmt.Fprint(s.out(), buf.String())
}
}
// MakeUsage creates a usage function that prints the flags.
func (s *FlagSet) MakeUsage() func() {
return func() {
if s.name == "" {
fmt.Fprintf(s.out(), "Usage:\n")
} else {
fmt.Fprintf(s.out(), "Usage of %s:\n", s.name)
}
s.PrintDefaults()
}
}
func (s *FlagSet) out() io.Writer {
if s.output == nil {
return os.Stderr
}
return s.output
}
// SetOutput sets the destination for usage and error messages.
// If output is nil, os.Stderr is used.
func (s *FlagSet) SetOutput(output io.Writer) {
s.output = output
s.FlagSet.SetOutput(output)
}
// Struct loads parameters based off of a struct object.
func (s *FlagSet) Struct(conf interface{}) error {
var err error
v := reflect.ValueOf(conf).Elem()
t := reflect.TypeOf(conf).Elem()
for i, l := 0, t.NumField(); i < l; i++ {
var key, name, usage string
var addr interface{}
var val Value
ft, fv := t.Field(i), v.Field(i)
// Skip unexported fields.
if ft.PkgPath != "" {
continue
}
// Handle 'env' flag.
key = ft.Tag.Get("env")
if key != "" && key != "-" {
addr = fv.Addr().Interface()
s.env[key], err = valueFromPointer(addr)
if err != nil {
goto HandleErr
}
}
name, usage = ft.Tag.Get("flag"), ft.Tag.Get("usage")
if name == "" || name == "-" {
continue
}
addr = fv.Addr().Interface()
// Get Value from pointer.
val, err = valueFromPointer(addr)
HandleErr:
if err != nil {
if s.errorHandling == flag.ContinueOnError {
return err
}
// Panic even on exit-on-error case; do not swallow error.
panic(err)
}
s.Var(val.(Value), name, usage)
}
s.Usage = s.MakeStructUsage(conf)
return nil
}
// ParseEnv parses environment variables.
func (s *FlagSet) ParseEnv() error {
var err error
for key, val := range s.env {
v, ok := os.LookupEnv(key)
if !ok {
continue
}
err = val.Set(v)
if err != nil {
break
}
}
if err != nil {
switch s.errorHandling {
case flag.ExitOnError:
exit(2)
case flag.PanicOnError:
panic(err)
}
}
return err
}