-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
152 lines (129 loc) · 4.32 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
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
package cli
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type errorHandle func(error)
var (
parseErrorHandleFunc errorHandle
)
// SetParseErrorHandle set the error handle function used for cli parsing flags.
// An error handle example:
// cli.SetParseErrorHandle(func(err error) {
// fmt.Println(err)
// os.Exit(3)
// })
func SetParseErrorHandle(f errorHandle) {
parseErrorHandleFunc = f
}
// GetStringFlagValue get the string value for the given StringFlag from the local flags
// of the cobra command.
func GetStringFlagValue(cmd *cobra.Command, flag StringFlag) string {
return getStringFlagValue(cmd.Flags(), flag)
}
// GetStringPersistentFlagValue get the string value for the given StringFlag from the persistent
// flags of the cobra command.
func GetStringPersistentFlagValue(cmd *cobra.Command, flag StringFlag) string {
return getStringFlagValue(cmd.PersistentFlags(), flag)
}
func getStringFlagValue(fs *pflag.FlagSet, flag StringFlag) string {
val, err := fs.GetString(flag.Name)
if err != nil {
handleParseError(err)
return ""
}
return val
}
// GetBoolFlagValue get the bool value for the given BoolFlag from the local flags of the
// cobra command.
func GetBoolFlagValue(cmd *cobra.Command, flag BoolFlag) bool {
return getBoolFlagValue(cmd.Flags(), flag)
}
// GetBoolPersistentFlagValue get the bool value for the given BoolFlag from the persistent flags
// of the given cobra command.
func GetBoolPersistentFlagValue(cmd *cobra.Command, flag BoolFlag) bool {
return getBoolFlagValue(cmd.PersistentFlags(), flag)
}
func getBoolFlagValue(fs *pflag.FlagSet, flag BoolFlag) bool {
val, err := fs.GetBool(flag.Name)
if err != nil {
handleParseError(err)
return false
}
return val
}
// GetIntFlagValue get the int value for the given IntFlag from the local flags of the
// cobra command.
func GetIntFlagValue(cmd *cobra.Command, flag IntFlag) int {
return getIntFlagValue(cmd.Flags(), flag)
}
// GetIntPersistentFlagValue get the int value for the given IntFlag from the persistent
// flags of the cobra command.
func GetIntPersistentFlagValue(cmd *cobra.Command, flag IntFlag) int {
return getIntFlagValue(cmd.PersistentFlags(), flag)
}
func getIntFlagValue(fs *pflag.FlagSet, flag IntFlag) int {
val, err := fs.GetInt(flag.Name)
if err != nil {
handleParseError(err)
return 0
}
return val
}
// GetStringSliceFlagValue get the string slice value for the given StringSliceFlag from
// the local flags of the cobra command.
func GetStringSliceFlagValue(cmd *cobra.Command, flag StringSliceFlag) []string {
return getStringSliceFlagValue(cmd.Flags(), flag)
}
// GetStringSlicePersistentFlagValue get the string slice value for the given StringSliceFlag
// from the persistent flags of the cobra command.
func GetStringSlicePersistentFlagValue(cmd *cobra.Command, flag StringSliceFlag) []string {
return getStringSliceFlagValue(cmd.PersistentFlags(), flag)
}
func getStringSliceFlagValue(fs *pflag.FlagSet, flag StringSliceFlag) []string {
val, err := fs.GetStringSlice(flag.Name)
if err != nil {
handleParseError(err)
return nil
}
return val
}
// GetIntSliceFlagValue get the int slice value for the given IntSliceFlag from
// the local flags of the cobra command.
func GetIntSliceFlagValue(cmd *cobra.Command, flag IntSliceFlag) []int {
return getIntSliceFlagValue(cmd.Flags(), flag)
}
// GetIntSlicePersistentFlagValue get the int slice value for the given IntSliceFlag
// from the persistent flags of the cobra command.
func GetIntSlicePersistentFlagValue(cmd *cobra.Command, flag IntSliceFlag) []int {
return getIntSliceFlagValue(cmd.PersistentFlags(), flag)
}
func getIntSliceFlagValue(fs *pflag.FlagSet, flag IntSliceFlag) []int {
val, err := fs.GetIntSlice(flag.Name)
if err != nil {
handleParseError(err)
return nil
}
return val
}
// IsFlagChanged returns whether the flag has been changed in command
func IsFlagChanged(cmd *cobra.Command, flag Flag) bool {
name := getFlagName(flag)
return cmd.Flags().Changed(name)
}
// HasFlagsChanged returns whether any of the flags is set by user in the command
func HasFlagsChanged(cmd *cobra.Command, flags []Flag) bool {
fs := cmd.Flags()
for _, flag := range flags {
name := getFlagName(flag)
if fs.Changed(name) {
return true
}
}
return false
}
func handleParseError(err error) {
if parseErrorHandleFunc != nil {
parseErrorHandleFunc(err)
}
}