-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
flag_int.go
38 lines (34 loc) · 893 Bytes
/
flag_int.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
package eflag
import (
"os"
"strconv"
"strings"
)
// IntFlag is an int flag implements of Flag interface.
type IntFlag struct {
Name string
Usage string
EnvVar string
Default int
Variable *int
Action func(string, *FlagSet)
}
// Apply implements of Flag Apply function.
func (f *IntFlag) Apply(set *FlagSet) {
for _, field := range strings.Split(f.Name, ",") {
field = strings.TrimSpace(field)
if f.Variable != nil {
set.FlagSet.IntVar(f.Variable, field, getValueByEnvAndDefaultIntValue(f.EnvVar, f.Default), f.Usage)
}
set.FlagSet.Int(field, getValueByEnvAndDefaultIntValue(f.EnvVar, f.Default), f.Usage)
set.actions[field] = f.Action
}
}
func getValueByEnvAndDefaultIntValue(envVar string, defaultValue int) int {
env := os.Getenv(envVar)
if env != "" {
intValue, _ := strconv.ParseInt(env, 10, 64)
return int(intValue)
}
return defaultValue
}