-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
flagx.go
96 lines (82 loc) · 2.31 KB
/
flagx.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
package flagx
import (
"time"
"github.com/spf13/pflag"
"github.com/spf13/cobra"
"github.com/ory/x/cmdx"
)
func NewFlagSet(name string) *pflag.FlagSet {
return pflag.NewFlagSet(name, pflag.ContinueOnError)
}
// MustGetBool returns a bool flag or fatals if an error occurs.
func MustGetBool(cmd *cobra.Command, name string) bool {
ok, err := cmd.Flags().GetBool(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return ok
}
// MustGetString returns a string flag or fatals if an error occurs.
func MustGetString(cmd *cobra.Command, name string) string {
s, err := cmd.Flags().GetString(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return s
}
// MustGetDuration returns a time.Duration flag or fatals if an error occurs.
func MustGetDuration(cmd *cobra.Command, name string) time.Duration {
d, err := cmd.Flags().GetDuration(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return d
}
// MustGetStringSlice returns a []string flag or fatals if an error occurs.
func MustGetStringSlice(cmd *cobra.Command, name string) []string {
ss, err := cmd.Flags().GetStringSlice(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return ss
}
// MustGetStringArray returns a []string flag or fatals if an error occurs.
func MustGetStringArray(cmd *cobra.Command, name string) []string {
ss, err := cmd.Flags().GetStringArray(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return ss
}
// MustGetStringToStringMap returns a map[string]string flag or fatals if an error occurs.
func MustGetStringToStringMap(cmd *cobra.Command, name string) map[string]string {
ss, err := cmd.Flags().GetStringToString(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return ss
}
// MustGetInt returns a int flag or fatals if an error occurs.
func MustGetInt(cmd *cobra.Command, name string) int {
ss, err := cmd.Flags().GetInt(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return ss
}
// MustGetUint8 returns a uint8 flag or fatals if an error occurs.
func MustGetUint8(cmd *cobra.Command, name string) uint8 {
v, err := cmd.Flags().GetUint8(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return v
}
// MustGetUint32 returns a uint32 flag or fatals if an error occurs.
func MustGetUint32(cmd *cobra.Command, name string) uint32 {
v, err := cmd.Flags().GetUint32(name)
if err != nil {
cmdx.Fatalf(err.Error())
}
return v
}