-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
63 lines (52 loc) · 1.37 KB
/
flag.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
package rig
import (
"flag"
"github.com/pkg/errors"
)
// A Flag represents the state and definition of a flag.
type Flag struct {
flag.Value
Name string
Env string
Usage string
TypeHint string
Required bool
set bool
defaultValue string
}
type isBoolFlagger interface {
IsBoolFlag() bool
}
// Set proxies the .Set method on the underlying flag.Value. It is used to keep track
// of wether a flag has been set or not.
func (f *Flag) Set(v string) error {
err := f.Value.Set(v)
if err != nil {
return err
}
f.set = true
return nil
}
// IsBoolFlag proxies the .IsBoolFlag method on the underlying flag.Value, if defined.
func (f *Flag) IsBoolFlag() bool {
if boolFlagger, ok := f.Value.(isBoolFlagger); ok {
return boolFlagger.IsBoolFlag()
}
return false
}
func (f Flag) missingError() error {
switch {
default:
return errors.New("configuration variable doesn't have a flag or environment variable specified")
case f.Name != "" && f.Env != "":
return errors.Errorf("missing command line flag -%s or environment variable %s", f.Name, f.Env)
case f.Name != "":
return errors.Errorf("missing command line flag -%s", f.Name)
case f.Env != "":
return errors.Errorf("missing environment variable %s", f.Env)
}
}
// IsSet returns true if the flag was set via the command line or the environment
func (f Flag) IsSet() bool {
return f.set
}