forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionLoader.java
More file actions
160 lines (144 loc) · 4.83 KB
/
OptionLoader.java
File metadata and controls
160 lines (144 loc) · 4.83 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package act.inject.param;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.cli.CliContext;
import act.cli.Optional;
import act.cli.Required;
import act.util.ActContext;
import org.osgl.inject.BeanSpec;
import org.osgl.util.E;
import org.osgl.util.Keyword;
import org.osgl.util.S;
import org.osgl.util.StringValueResolver;
/**
* Load command line options
*/
class OptionLoader extends CliParamValueLoader implements ParamValueLoader {
final String bindName;
String lead1;
String lead2;
final String defVal;
final String requiredGroup;
final boolean required;
final BeanSpec beanSpec;
final String errorTemplate;
private final StringValueResolver resolver;
OptionLoader(String bindName, Optional optional, StringValueResolver resolver, BeanSpec beanSpec) {
this.bindName = bindName;
this.required = false;
this.parseLeads(optional.lead());
this.defVal = optional.defVal();
this.requiredGroup = null;
this.beanSpec = beanSpec;
this.errorTemplate = errorTemplate(optional);
this.resolver = resolver;
CliContext.ParsingContextBuilder.foundOptional();
}
OptionLoader(String bindName, Required required, StringValueResolver resolver, BeanSpec beanSpec) {
this.bindName = bindName;
this.required = true;
this.parseLeads(required.lead());
this.defVal = null;
String group = required.group();
this.requiredGroup = S.blank(group) ? bindName : group;
this.beanSpec = beanSpec;
this.errorTemplate = errorTemplate(required);
this.resolver = resolver;
CliContext.ParsingContextBuilder.foundRequired(this.requiredGroup);
}
@Override
public Object load(Object cachedBean, ActContext<?> context, boolean noDefaultValue) {
CliContext ctx = (CliContext) context;
String optVal = ctx.paramVal(bindName);
if (S.blank(optVal) && required) {
optVal = getFirstArgument(ctx);
}
Object val = null;
if (S.notBlank(optVal)) {
val = resolve(optVal);
}
if (null == val && null != cachedBean) {
val = cachedBean;
}
if (null == val) {
if (!required) {
val = S.notBlank(defVal) ? resolve(defVal) : resolve(null);
}
}
if (null != val && required) {
ctx.parsingContext().foundRequired(requiredGroup);
}
return val;
}
@Override
public String bindName() {
return this.bindName;
}
private Object resolve(String val) {
return resolve(val, resolver);
}
private <T> T resolve(String val, StringValueResolver<T> resolver) {
if (null == errorTemplate) {
return resolver.resolve(val);
}
try {
return resolver.resolve(val);
} catch (Exception e) {
throw E.unexpected(errorTemplate, val);
}
}
private String errorTemplate(Optional optional) {
return verifyErrorTemplate(optional.errorTemplate());
}
private String errorTemplate(Required required) {
return verifyErrorTemplate(required.errorTemplate());
}
private String verifyErrorTemplate(String s) {
if (S.blank(s)) {
return null;
}
if (!s.contains("%")) {
throw E.invalidConfiguration("Error template must have format argument placeholder, e.g. %s inside it: " + s);
}
if (s.split("%").length > 2) {
throw E.invalidConfiguration("Error template must not have more than one format argument placeholder, e.g. %s inside it: " + s);
}
return s;
}
private void parseLeads(String[] specs) {
lead1 = specs[0];
if (specs.length > 1) {
lead2 = specs[1];
} else {
String[] sa = lead1.split("[,;\\s]+");
if (sa.length > 2) {
throw E.unexpected("Option cannot have more than two leads");
}
if (sa.length > 1) {
lead1 = sa[0];
lead2 = sa[1];
}
}
if (S.blank(lead1)) {
lead1 = "-" + bindName.charAt(0);
lead2 = "--" + Keyword.of(bindName).dashed();
}
}
}