Skip to content

Commit f7ad3a8

Browse files
committed
- [+] add int flag env var support to commandlineFlag
1 parent f4677fb commit f7ad3a8

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

test/commandlineFlag.ref

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Options struct {
2424
ExtTmpl string // `extension` of template file
2525
StrFrom string // replace from, the from string used for the replace function
2626
StrTo string // replace to, the to string used for the replace function
27+
IntV int // int var
2728
Batch time.Duration // batch interval
2829
Debug int // debugging `level`
2930
}
@@ -56,6 +57,8 @@ func initVars() {
5657
"replace from, the from string used for the replace function")
5758
flag.StringVar(&Opts.StrTo, "rt", "",
5859
"replace to, the to string used for the replace function")
60+
flag.IntVar(&Opts.IntV, "intv", 0,
61+
"int var")
5962
flag.DurationVar(&Opts.Batch, "batch", 120*time.Second,
6063
"batch interval")
6164
flag.IntVar(&Opts.Debug, "d", 0,
@@ -98,6 +101,24 @@ func initVals() {
98101
len(os.Getenv("EASYGEN_RT")) != 0 {
99102
Opts.StrTo = os.Getenv("EASYGEN_RT")
100103
}
104+
if Opts.IntV == 0 ||
105+
len(os.Getenv("EASYGEN_INTV")) != 0 {
106+
if i, err := strconv.Atoi(os.Getenv("EASYGEN_INTV")); err == nil {
107+
Opts.IntV = i
108+
}
109+
}
110+
if Opts.Debug == 0 ||
111+
len(os.Getenv("EASYGEN_D")) != 0 {
112+
if i, err := strconv.Atoi(os.Getenv("EASYGEN_D")); err == nil {
113+
Opts.Debug = i
114+
}
115+
}
116+
if Opts.Debug == 0 ||
117+
len(os.Getenv("EASYGEN_DEBUG")) != 0 {
118+
if i, err := strconv.Atoi(os.Getenv("EASYGEN_DEBUG")); err == nil {
119+
Opts.Debug = i
120+
}
121+
}
101122

102123
}
103124

test/commandlineFlag.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ func initVals() {
5555
}{{end}}{{else}}{{if eq .Type "bool" }}
5656
if _, exists = os.LookupEnv("{{$envVar}}"); {{$.StructVar}}.{{.Name}}|| exists {
5757
{{$.StructVar}}.{{.Name}} = true
58-
}{{end}}{{end}}{{end}}{{end}}
58+
}{{else}}{{if eq .Type "int" }}{{if (stringsContains .Flag ",")}}{{$cf := .}}{{range (stringsSplit .Flag ",")}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .)}}
59+
if {{$.StructVar}}.{{$cf.Name}} == 0 ||
60+
len(os.Getenv("{{$envVar}}")) != 0 {
61+
if i, err := strconv.Atoi(os.Getenv("{{$envVar}}")); err == nil {
62+
{{$.StructVar}}.{{$cf.Name}} = i
63+
}
64+
}{{end}}{{else}}
65+
if {{$.StructVar}}.{{.Name}} == 0 ||
66+
len(os.Getenv("{{$envVar}}")) != 0 {
67+
if i, err := strconv.Atoi(os.Getenv("{{$envVar}}")); err == nil {
68+
{{$.StructVar}}.{{.Name}} = i
69+
}
70+
}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}
5971

6072
}
6173

test/commandlineFlag.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Options:
6262
Value: '""'
6363
Usage: "replace to, the to string used for the replace function"
6464

65+
- Name: IntV
66+
Type: int
67+
Flag: intv
68+
Value: 0
69+
Usage: "int var"
70+
6571
- Name: Batch
6672
Type: duration
6773
Flag: batch

0 commit comments

Comments
 (0)