-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.go
136 lines (101 loc) · 3.27 KB
/
cmdline.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
package core
import (
"errors"
"flag"
"strings"
)
const ProcyonApplicationCommandLinePropertySource = "ProcyonApplicationCommandLinePropertySource"
const NonOptionArgsPropertyName = "nonOptionArgs"
type CommandLinePropertySource interface {
PropertySource
ContainsOption(name string) bool
GetOptionValues(name string) []string
GetNonOptionArgs() []string
}
type SimpleCommandLinePropertySource struct {
source CommandLineArgs
}
func NewSimpleCommandLinePropertySource(args []string) SimpleCommandLinePropertySource {
cmdLineArgs, err := NewCommandLineArgsParser().Parse(args)
if err != nil {
panic(err)
}
cmdlinePropertySource := SimpleCommandLinePropertySource{
source: cmdLineArgs,
}
return cmdlinePropertySource
}
func (cmdLineSource SimpleCommandLinePropertySource) GetName() string {
return ProcyonApplicationCommandLinePropertySource
}
func (cmdLineSource SimpleCommandLinePropertySource) GetSource() interface{} {
return cmdLineSource.source
}
func (cmdLineSource SimpleCommandLinePropertySource) GetProperty(name string) interface{} {
if NonOptionArgsPropertyName == name {
nonOptValues := cmdLineSource.GetNonOptionArgs()
if nonOptValues != nil {
return strings.Join(nonOptValues, ",")
}
return nil
}
optValues := cmdLineSource.GetOptionValues(name)
if optValues != nil {
return strings.Join(optValues, ",")
}
return nil
}
func (cmdLineSource SimpleCommandLinePropertySource) ContainsProperty(name string) bool {
if NonOptionArgsPropertyName == name {
return len(cmdLineSource.GetNonOptionArgs()) != 0
}
return cmdLineSource.ContainsOption(name)
}
func (cmdLineSource SimpleCommandLinePropertySource) ContainsOption(name string) bool {
return cmdLineSource.source.containsOption(name)
}
func (cmdLineSource SimpleCommandLinePropertySource) GetOptionValues(name string) []string {
return cmdLineSource.source.getOptionValues(name)
}
func (cmdLineSource SimpleCommandLinePropertySource) GetNonOptionArgs() []string {
return cmdLineSource.source.getNonOptionArgs()
}
func (cmdLineSource SimpleCommandLinePropertySource) GetPropertyNames() []string {
return cmdLineSource.source.getOptionNames()
}
type SimpleCommandLineArgsParser struct {
}
func NewCommandLineArgsParser() SimpleCommandLineArgsParser {
return SimpleCommandLineArgsParser{}
}
func (parser SimpleCommandLineArgsParser) Parse(args []string) (CommandLineArgs, error) {
cmdLineArgs := NewCommandLineArgs()
appArgumentFlagSet := flag.NewFlagSet("ProcyonApplicationArguments", flag.ContinueOnError)
err := appArgumentFlagSet.Parse(args)
if err != nil {
return cmdLineArgs, err
}
for _, arg := range appArgumentFlagSet.Args() {
if strings.HasPrefix(arg, "--") {
optionText := arg[2:]
indexOfEqualSign := strings.Index(optionText, "=")
optionName := ""
optionValue := ""
if indexOfEqualSign > -1 {
optionName = optionText[0:indexOfEqualSign]
optionValue = optionText[indexOfEqualSign+1:]
} else {
optionName = optionText
}
optionName = strings.TrimSpace(optionName)
optionValue = strings.TrimSpace(optionValue)
if optionName == "" {
return cmdLineArgs, errors.New("Invalid argument syntax : " + arg)
}
cmdLineArgs.addOptionArgs(optionName, optionValue)
} else {
cmdLineArgs.addNonOptionArgs(arg)
}
}
return cmdLineArgs, nil
}