-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlags.java
More file actions
196 lines (171 loc) · 6 KB
/
Copy pathFlags.java
File metadata and controls
196 lines (171 loc) · 6 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.github.yin.flags;
import com.github.yin.flags.analysis.UsagePrinter;
import com.github.yin.flags.annotations.ClassScanner;
import com.google.common.annotations.VisibleForTesting;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
/**
* Provides static API for creating built-in flags, parsing arguments and
* injecting flag values into {@link Flag}'s.
*
* Client applications should not attempt to parse flags multiple times.
* Tests are an exception, where we provide method {@link Flags#parse(Map, Iterable)}).
*
* Example:
* <pre>
* {@literal @}FlagDesc("Processes some actions from command-line.")
* public static class ReportMain {
*
* static final String APP_PACKAGE = "com.github.yin.java.flags.example";
*
* {@literal @}FlagDesc("Print additional information")
* static final Flag<Boolean> verbose = Flags.create(false);
*
* public static main(String[] args) {
* List>String< arguments = Flags.init(args, Arrays.asList({
* APP_PACKAGE
* }));
* if (arguments.size() < 0) {
* if (foo.get() == true) {
* // ...
* } else {
* // ...
* }
* } else {
* Flags.printUsage(APP_PACKAGE);
* }
* }
* }
* </pre>
*/
public class Flags {
private static Flags instance;
private final ClassScanner classScanner;
private final ClassMetadataIndex classMetadataIndex;
private final FlagIndex<FlagMetadata> flagIndex;
/**
* Initializes flag values from command-line style arguments.
* @param args command-line arguments to parse values from
* @param packages list of package roots to scan flags
*/
public static List<String> parse(String[] args, Iterable<String> packages) {
instance().scan(packages);
return instance._parse(args);
}
/**
* Creates {@link Flag} accessor for {@link Integer} type.
*/
public static Flag<Boolean> create(Boolean defaultz) {
return new BasicFlag.BooleanFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link Integer} type.
*/
public static Flag<Integer> create(Integer defaultz) {
return new BasicFlag.IntegerFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link Long} type.
*/
public static Flag<Long> create(Long defaultz) {
return new BasicFlag.LongFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link Float} type.
*/
public static Flag<Float> create(Float defaultz) {
return new BasicFlag.FloatFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link Double} type.
*/
public static Flag<Double> create(Double defaultz) {
return new BasicFlag.DoubleFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link BigInteger} type.
*/
public static Flag<BigInteger> create(BigInteger defaultz) {
return new BasicFlag.BigIntegerFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link BigDecimal} type.
*/
public static Flag<BigDecimal> create(BigDecimal defaultz) {
return new BasicFlag.BigDecimalFlag(defaultz);
}
/**
* Creates {@link Flag} accessor for {@link String} type.
*/
public static Flag<String> create(String defaultz) {
return new BasicFlag.StringFlag(defaultz);
}
/** Prints user-readable usage help for all flags in a given package */
public static void printUsage(String packagePrefix) {
instance().printUsageForPackage(packagePrefix);
}
/**
* Indexes flag values from a <code>Map</code>. This is useful for mocking flag values in
* integration testing. Please do not misuse this function, there will be better way to inject
* your logic into flag processing, surely use this only in your tests.
* @param options Map of flags and their intended values
*/
@VisibleForTesting
public static void parse(Map<String, String> options, Iterable<String> packages) {
instance().scan(packages);
instance()._parse(options);
}
@VisibleForTesting
static ClassMetadataIndex classMetadata() {
return instance().classMetadataIndex;
}
@VisibleForTesting
static FlagIndex flagMetadata() {
return instance().flagIndex;
}
private void scan(Iterable<String> packages) {
for (String pkg : packages) {
classScanner.scanPackage(pkg, flagIndex, classMetadataIndex);
}
}
private void printUsageForPackage(String packagePrefix) {
synchronized (this) {
classScanner.scanPackage(packagePrefix, flagIndex, classMetadataIndex);
}
new UsagePrinter().printUsage(flagIndex, classMetadataIndex, System.out);
}
private static Flags instance() {
synchronized (Flags.class) {
if (instance == null) {
instance = new Flags(new ClassScanner(), new ClassMetadataIndex(), new FlagIndex<>());
}
}
return instance;
}
private Flags(ClassScanner classScanner, ClassMetadataIndex classMetadataIndex, FlagIndex<FlagMetadata> flagIndex) {
this.classScanner = classScanner;
this.classMetadataIndex = classMetadataIndex;
this.flagIndex = flagIndex;
}
private List<String> _parse(String[] args) {
GflagsParser parser = new GflagsParser(flagIndex);
return parser.parse(args);
}
private void _parse(Map<String, String> options) {
MapParser parser = new MapParser(flagIndex);
parser.parse(options);
}
/**
* Indicates a problem in parsing flags. It is thrown from {#link Flags#parse()}.
*/
public static class ParseException extends RuntimeException {
public ParseException(String message, Throwable throwable) {
super(message, throwable);
}
public ParseException(String message) {
super(message);
}
}
}