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