1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.exolab.castor.util;
21
22 import java.io.PrintWriter;
23 import java.util.Hashtable;
24 import java.util.Properties;
25 import java.util.Vector;
26
27 import org.castor.core.util.Messages;
28
29
30
31
32
33
34
35 public class CommandLineOptions {
36 private final Vector<String> _flags = new Vector<>();
37 private final Hashtable<String, CmdLineOption> _optionInfo = new Hashtable<>();
38 private final PrintWriter _errorWriter = new PrintWriter(System.out);
39
40
41
42
43
44
45 public void addFlag(final String flag) {
46 addFlag(flag, null, null);
47 }
48
49
50
51
52
53
54
55 public void addFlag(final String flag, final String comment) {
56 addFlag(flag, null, comment, false);
57 }
58
59
60
61
62
63
64
65
66 public void addFlag(final String flag, final String usageText, final String comment) {
67 addFlag(flag, usageText, comment, false);
68 }
69
70
71
72
73
74
75
76
77
78 public void addFlag(final String flag, final String usageText, final String comment,
79 final boolean optional) {
80 if (flag == null) {
81 return;
82 }
83 _flags.add(flag);
84
85 CmdLineOption opt = new CmdLineOption(flag);
86 opt.setComment(comment);
87 opt.setUsageText(usageText);
88 opt.setOptional(optional);
89 _optionInfo.put(flag, opt);
90 }
91
92
93
94
95 public Properties getOptions(final String[] args) {
96 Properties options = new Properties();
97 String flag = null;
98 for (int i = 0; i < args.length; i++) {
99
100 if (args[i].startsWith("-")) {
101
102
103 if (flag != null) {
104 options.put(flag, args[i]);
105 options.put(Integer.valueOf(i), args[i]);
106 }
107
108 flag = args[i].substring(1);
109
110
111
112 if (!_flags.contains(flag)) {
113 int idx = 1;
114 while (idx <= flag.length()) {
115 if (_flags.contains(flag.substring(0, idx))) {
116 if (idx < flag.length()) {
117 options.put(flag.substring(0, idx), flag.substring(idx));
118 break;
119 }
120 } else if (idx == flag.length()) {
121 _errorWriter.println(Messages.format("misc.invalidCLIOption", "-" + flag));
122 printUsage(_errorWriter);
123 }
124 ++idx;
125 }
126 }
127 } else {
128
129 if (flag != null) {
130 options.put(flag, args[i]);
131 }
132 options.put(Integer.valueOf(i), args[i]);
133 flag = null;
134 }
135
136 }
137 if (flag != null) {
138 options.put(flag, "no value");
139 }
140 return options;
141 }
142
143
144
145
146
147
148
149 public void setComment(final String flag, final String comment) {
150 if (flag == null) {
151 return;
152 }
153 CmdLineOption opt = (CmdLineOption) _optionInfo.get(flag);
154 if (opt != null) {
155 opt.setComment(comment);
156 }
157 }
158
159
160
161
162
163
164
165 public void setOptional(final String flag, final boolean optional) {
166 if (flag == null) {
167 return;
168 }
169 CmdLineOption opt = (CmdLineOption) _optionInfo.get(flag);
170 if (opt != null) {
171 opt.setOptional(optional);
172 }
173 }
174
175
176
177
178
179
180
181 public void setUsageInfo(final String flag, final String usage) {
182 if (flag == null) {
183 return;
184 }
185 CmdLineOption opt = (CmdLineOption) _optionInfo.get(flag);
186 if (opt != null) {
187 opt.setUsageText(usage);
188 }
189 }
190
191 public void printUsage(final PrintWriter pw) {
192 pw.println();
193 pw.print(Messages.message("misc.CLIUsage"));
194 for (int i = 0; i < _flags.size(); i++) {
195 String flag = (String) _flags.elementAt(i);
196 CmdLineOption opt = (CmdLineOption) _optionInfo.get(flag);
197 if (opt.getOptional()) {
198 pw.print(" [-");
199 } else {
200 pw.print(" -");
201 }
202 pw.print(flag);
203 String usage = opt.getUsageText();
204 if (usage != null) {
205 pw.print(' ');
206 pw.print(usage);
207 }
208 if (opt.getOptional()) {
209 pw.print(']');
210 }
211 }
212 pw.println();
213 pw.flush();
214 }
215
216 public void printHelp(final PrintWriter pw) {
217 printUsage(pw);
218 pw.println();
219
220 if (!_flags.isEmpty()) {
221 pw.println("Flag Description");
222 pw.println("----------------------------------------------");
223 }
224 for (String flag : _flags) {
225 CmdLineOption opt = _optionInfo.get(flag);
226
227 pw.print('-');
228 pw.print(flag);
229
230 pw.print(' ');
231
232 int spaces = 17 - flag.length();
233 while (spaces > 0) {
234 pw.print(' ');
235 --spaces;
236 }
237
238 pw.print(opt.getComment());
239
240
241
242
243
244
245
246 pw.println();
247 }
248 pw.println();
249 pw.flush();
250 }
251 }
252
253
254 class CmdLineOption {
255 private boolean _optional = false;
256 private String _usageText = null;
257 private String _comment = null;
258 private final String _flag;
259
260
261
262
263
264
265 CmdLineOption(final String flag) {
266 super();
267 _flag = flag;
268 }
269
270
271
272
273
274
275 public String getFlag() {
276 return _flag;
277 }
278
279
280
281
282
283
284 public boolean getOptional() {
285 return _optional;
286 }
287
288
289
290
291
292
293 public String getComment() {
294 return _comment;
295 }
296
297
298
299
300
301
302 public String getUsageText() {
303 return _usageText;
304 }
305
306
307
308
309
310
311 public void setOptional(final boolean optional) {
312 _optional = optional;
313 }
314
315
316
317
318
319
320 public void setComment(final String comment) {
321 _comment = comment;
322 }
323
324
325
326
327
328
329 public void setUsageText(final String usageText) {
330 _usageText = usageText;
331 }
332 }
333