-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_helper.go
51 lines (41 loc) · 934 Bytes
/
flag_helper.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
package helpers
import (
"github.com/stoewer/go-strcase"
"github.com/urfave/cli/v2"
)
type FlagHelper struct {
Prefix string
Name string
Category string
Value string
Usage string
Required bool
flag cli.Flag
}
func (this *FlagHelper) GetFlagName() string {
name := this.Name
if this.Prefix != "" {
name = this.Prefix + "-" + name
}
return name
}
func (this *FlagHelper) GetEnvVar() string {
name := this.GetFlagName()
return strcase.UpperSnakeCase(name)
}
func (this *FlagHelper) StringFlag() *cli.StringFlag {
if this.flag == nil {
this.flag = &cli.StringFlag{
Name: this.GetFlagName(),
EnvVars: []string{this.GetEnvVar()},
Value: this.Value,
Usage: this.Usage,
Required: this.Required,
Category: this.Category,
}
}
return this.flag.(*cli.StringFlag)
}
func (this *FlagHelper) StringValue(cCtx *cli.Context) string {
return cCtx.String(this.GetFlagName())
}