-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCLI.go
More file actions
46 lines (40 loc) · 975 Bytes
/
SourceCLI.go
File metadata and controls
46 lines (40 loc) · 975 Bytes
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
package props
import (
"os"
"strings"
)
// SourceCLI 用于将命令行输入参数作为配置数据源.
type SourceCLI struct {
SourceCustom
}
// NewSourceCLI 创建一个 SourceCLI 对象.
func NewSourceCLI() *SourceCLI {
return &SourceCLI{
SourceCustom: SourceCustom{
props: nil,
},
}
}
// Accept 将逐个扫描所有的命令行的输入参数,并将以 `prefix` 为前缀的参数作为配置数据源的配置项.
// 如果有多个前缀,那么可以调用 `Accept函数多次`。
func (s *SourceCLI) Accept(prefix string) {
for i := 1; i < len(os.Args); i++ {
if strings.HasPrefix(os.Args[i], prefix) {
key := ""
val := ""
str := os.Args[i][len(prefix):]
pos := strings.IndexByte(str, '=')
if pos > 0 {
key = strings.TrimSpace(str[0:pos])
val = strings.TrimSpace(str[pos+1:])
} else if pos < 0 {
key = strings.TrimSpace(str)
val = ""
}
if "" == key {
continue
}
s.props[key] = val
}
}
}