1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.cli2.util; |
18 | |
|
19 | |
import java.io.PrintWriter; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Collections; |
22 | |
import java.util.Comparator; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.commons.cli2.DisplaySetting; |
29 | |
import org.apache.commons.cli2.Group; |
30 | |
import org.apache.commons.cli2.HelpLine; |
31 | |
import org.apache.commons.cli2.Option; |
32 | |
import org.apache.commons.cli2.OptionException; |
33 | |
import org.apache.commons.cli2.resource.ResourceConstants; |
34 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class HelpFormatter { |
40 | |
|
41 | |
|
42 | |
|
43 | |
public static final int DEFAULT_FULL_WIDTH = 80; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static final String DEFAULT_GUTTER_LEFT = ""; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final String DEFAULT_GUTTER_CENTER = " "; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static final String DEFAULT_GUTTER_RIGHT = ""; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public static final Set DEFAULT_FULL_USAGE_SETTINGS; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public static final Set DEFAULT_LINE_USAGE_SETTINGS; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public static final Set DEFAULT_DISPLAY_USAGE_SETTINGS; |
81 | |
|
82 | |
static { |
83 | 1 | final Set fullUsage = new HashSet(DisplaySetting.ALL); |
84 | 1 | fullUsage.remove(DisplaySetting.DISPLAY_ALIASES); |
85 | 1 | fullUsage.remove(DisplaySetting.DISPLAY_GROUP_NAME); |
86 | 1 | fullUsage.remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP); |
87 | 1 | DEFAULT_FULL_USAGE_SETTINGS = Collections.unmodifiableSet(fullUsage); |
88 | |
|
89 | 1 | final Set lineUsage = new HashSet(); |
90 | 1 | lineUsage.add(DisplaySetting.DISPLAY_ALIASES); |
91 | 1 | lineUsage.add(DisplaySetting.DISPLAY_GROUP_NAME); |
92 | 1 | lineUsage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT); |
93 | 1 | DEFAULT_LINE_USAGE_SETTINGS = Collections.unmodifiableSet(lineUsage); |
94 | |
|
95 | 1 | final Set displayUsage = new HashSet(DisplaySetting.ALL); |
96 | 1 | displayUsage.remove(DisplaySetting.DISPLAY_PARENT_ARGUMENT); |
97 | 1 | DEFAULT_DISPLAY_USAGE_SETTINGS = Collections.unmodifiableSet(displayUsage); |
98 | 1 | } |
99 | |
|
100 | 108 | private Set fullUsageSettings = new HashSet(DEFAULT_FULL_USAGE_SETTINGS); |
101 | 108 | private Set lineUsageSettings = new HashSet(DEFAULT_LINE_USAGE_SETTINGS); |
102 | 108 | private Set displaySettings = new HashSet(DEFAULT_DISPLAY_USAGE_SETTINGS); |
103 | 108 | private OptionException exception = null; |
104 | |
private Group group; |
105 | 108 | private Comparator comparator = null; |
106 | 108 | private String divider = null; |
107 | 108 | private String header = null; |
108 | 108 | private String footer = null; |
109 | 108 | private String shellCommand = ""; |
110 | 108 | private PrintWriter out = new PrintWriter(System.out); |
111 | |
|
112 | |
|
113 | |
private final String gutterLeft; |
114 | |
private final String gutterCenter; |
115 | |
private final String gutterRight; |
116 | |
private final int pageWidth; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public HelpFormatter() { |
122 | 83 | this(DEFAULT_GUTTER_LEFT, DEFAULT_GUTTER_CENTER, DEFAULT_GUTTER_RIGHT, DEFAULT_FULL_WIDTH); |
123 | 83 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public HelpFormatter(final String gutterLeft, |
133 | |
final String gutterCenter, |
134 | |
final String gutterRight, |
135 | 108 | final int fullWidth) { |
136 | |
|
137 | 108 | this.gutterLeft = (gutterLeft == null) ? DEFAULT_GUTTER_LEFT : gutterLeft; |
138 | |
|
139 | |
|
140 | 108 | this.gutterCenter = (gutterCenter == null) ? DEFAULT_GUTTER_CENTER : gutterCenter; |
141 | |
|
142 | |
|
143 | 108 | this.gutterRight = (gutterRight == null) ? DEFAULT_GUTTER_RIGHT : gutterRight; |
144 | |
|
145 | |
|
146 | 108 | this.pageWidth = fullWidth - this.gutterLeft.length() - this.gutterRight.length(); |
147 | |
|
148 | |
|
149 | 108 | int availableWidth = fullWidth - pageWidth + this.gutterCenter.length(); |
150 | |
|
151 | 108 | if (availableWidth < 2) { |
152 | 0 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.HELPFORMATTER_GUTTER_TOO_LONG)); |
153 | |
} |
154 | 108 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void print() { |
160 | 12 | printHeader(); |
161 | 12 | printException(); |
162 | 12 | printUsage(); |
163 | 12 | printHelp(); |
164 | 12 | printFooter(); |
165 | 12 | out.flush(); |
166 | 12 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void printException() { |
172 | 13 | if (exception != null) { |
173 | 3 | printDivider(); |
174 | 3 | printWrapped(exception.getMessage()); |
175 | |
} |
176 | 13 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public void printHelp() { |
182 | 15 | printDivider(); |
183 | |
|
184 | |
final Option option; |
185 | |
|
186 | 15 | if ((exception != null) && (exception.getOption() != null)) { |
187 | 3 | option = exception.getOption(); |
188 | |
} else { |
189 | 12 | option = group; |
190 | |
} |
191 | |
|
192 | |
|
193 | 15 | final List helpLines = option.helpLines(0, displaySettings, comparator); |
194 | |
|
195 | |
|
196 | 15 | int usageWidth = 0; |
197 | |
|
198 | 15 | for (final Iterator i = helpLines.iterator(); i.hasNext();) { |
199 | 119 | final HelpLine helpLine = (HelpLine) i.next(); |
200 | 119 | final String usage = helpLine.usage(lineUsageSettings, comparator); |
201 | 119 | usageWidth = Math.max(usageWidth, usage.length()); |
202 | 119 | } |
203 | |
|
204 | |
|
205 | 15 | final StringBuffer blankBuffer = new StringBuffer(); |
206 | |
|
207 | 316 | for (int i = 0; i < usageWidth; i++) { |
208 | 301 | blankBuffer.append(' '); |
209 | |
} |
210 | |
|
211 | |
|
212 | 15 | final int descriptionWidth = Math.max(1, pageWidth - gutterCenter.length() - usageWidth); |
213 | |
|
214 | |
|
215 | 15 | for (final Iterator i = helpLines.iterator(); i.hasNext();) { |
216 | |
|
217 | 119 | final HelpLine helpLine = (HelpLine) i.next(); |
218 | |
|
219 | |
|
220 | 119 | final List descList = wrap(helpLine.getDescription(), descriptionWidth); |
221 | 119 | final Iterator descriptionIterator = descList.iterator(); |
222 | |
|
223 | |
|
224 | 119 | printGutterLeft(); |
225 | 119 | pad(helpLine.usage(lineUsageSettings, comparator), usageWidth, out); |
226 | 119 | out.print(gutterCenter); |
227 | 119 | pad((String) descriptionIterator.next(), descriptionWidth, out); |
228 | 119 | printGutterRight(); |
229 | 119 | out.println(); |
230 | |
|
231 | |
|
232 | 288 | while (descriptionIterator.hasNext()) { |
233 | 169 | printGutterLeft(); |
234 | |
|
235 | |
|
236 | 169 | out.print(blankBuffer); |
237 | 169 | out.print(gutterCenter); |
238 | 169 | pad((String) descriptionIterator.next(), descriptionWidth, out); |
239 | 169 | printGutterRight(); |
240 | 169 | out.println(); |
241 | |
} |
242 | 119 | } |
243 | |
|
244 | 15 | printDivider(); |
245 | 15 | } |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public void printUsage() { |
251 | 13 | printDivider(); |
252 | |
|
253 | 13 | final StringBuffer buffer = new StringBuffer("Usage:\n"); |
254 | 13 | buffer.append(shellCommand).append(' '); |
255 | 13 | group.appendUsage(buffer, fullUsageSettings, comparator, " "); |
256 | 13 | printWrapped(buffer.toString()); |
257 | 13 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public void printHeader() { |
263 | 13 | if (header != null) { |
264 | 4 | printDivider(); |
265 | 4 | printWrapped(header); |
266 | |
} |
267 | 13 | } |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
public void printFooter() { |
273 | 13 | if (footer != null) { |
274 | 4 | printWrapped(footer); |
275 | 4 | printDivider(); |
276 | |
} |
277 | 13 | } |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public void printWrapped(final String text) { |
284 | 24 | for (final Iterator i = wrap(text, pageWidth).iterator(); i.hasNext();) { |
285 | 47 | printGutterLeft(); |
286 | 47 | pad((String) i.next(), pageWidth, out); |
287 | 47 | printGutterRight(); |
288 | 47 | out.println(); |
289 | |
} |
290 | |
|
291 | 24 | out.flush(); |
292 | 24 | } |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public void printGutterLeft() { |
298 | 335 | if (gutterLeft != null) { |
299 | 335 | out.print(gutterLeft); |
300 | |
} |
301 | 335 | } |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public void printGutterRight() { |
307 | 335 | if (gutterRight != null) { |
308 | 335 | out.print(gutterRight); |
309 | |
} |
310 | 335 | } |
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public void printDivider() { |
316 | 55 | if (divider != null) { |
317 | 19 | out.println(divider); |
318 | |
} |
319 | 55 | } |
320 | |
|
321 | |
protected static void pad(final String text, |
322 | |
final int width, |
323 | |
final PrintWriter writer) { |
324 | |
final int left; |
325 | |
|
326 | |
|
327 | 458 | if (text == null) { |
328 | 1 | left = 0; |
329 | |
} else { |
330 | 457 | writer.write(text); |
331 | 457 | left = text.length(); |
332 | |
} |
333 | |
|
334 | |
|
335 | 7860 | for (int i = left; i < width; ++i) { |
336 | 7402 | writer.write(' '); |
337 | |
} |
338 | 458 | } |
339 | |
|
340 | |
protected static List wrap(final String text, |
341 | |
final int width) { |
342 | |
|
343 | 151 | if (width < 1) { |
344 | 1 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.HELPFORMATTER_WIDTH_TOO_NARROW, |
345 | |
new Object[] { |
346 | |
new Integer(width) |
347 | |
})); |
348 | |
} |
349 | |
|
350 | |
|
351 | 150 | if (text == null) { |
352 | 22 | return Collections.singletonList(""); |
353 | |
} |
354 | |
|
355 | 128 | final List lines = new ArrayList(); |
356 | 128 | final char[] chars = text.toCharArray(); |
357 | 128 | int left = 0; |
358 | |
|
359 | |
|
360 | 338 | while (left < chars.length) { |
361 | |
|
362 | 332 | int right = left; |
363 | |
|
364 | |
|
365 | 4987 | while ((right < chars.length) && (chars[right] != '\n') && |
366 | |
(right < (left + width + 1))) { |
367 | 4655 | right++; |
368 | |
} |
369 | |
|
370 | |
|
371 | 332 | if ((right < chars.length) && (chars[right] == '\n')) { |
372 | |
|
373 | 21 | final String line = new String(chars, left, right - left); |
374 | 21 | lines.add(line); |
375 | |
|
376 | |
|
377 | 21 | left = right + 1; |
378 | |
|
379 | 21 | if (left == chars.length) { |
380 | 1 | lines.add(""); |
381 | |
} |
382 | |
|
383 | |
|
384 | |
continue; |
385 | |
} |
386 | |
|
387 | |
|
388 | 311 | right = (left + width) - 1; |
389 | |
|
390 | |
|
391 | 311 | if (chars.length <= right) { |
392 | |
|
393 | 122 | final String line = new String(chars, left, chars.length - left); |
394 | 122 | lines.add(line); |
395 | |
|
396 | |
|
397 | 122 | break; |
398 | |
} |
399 | |
|
400 | |
|
401 | 460 | while ((right >= left) && (chars[right] != ' ')) { |
402 | 271 | right--; |
403 | |
} |
404 | |
|
405 | |
|
406 | 189 | if (right >= left) { |
407 | |
|
408 | 27 | final String line = new String(chars, left, right - left); |
409 | 27 | lines.add(line); |
410 | |
|
411 | |
|
412 | 54 | while ((right < chars.length) && (chars[right] == ' ')) { |
413 | 27 | right++; |
414 | |
} |
415 | |
|
416 | 27 | left = right; |
417 | |
|
418 | |
|
419 | 27 | continue; |
420 | |
} |
421 | |
|
422 | |
|
423 | 162 | right = Math.min(left + width, chars.length); |
424 | |
|
425 | |
|
426 | 162 | final String line = new String(chars, left, right - left); |
427 | 162 | lines.add(line); |
428 | |
|
429 | |
|
430 | 187 | while ((right < chars.length) && (chars[right] == ' ')) { |
431 | 25 | right++; |
432 | |
} |
433 | |
|
434 | 162 | left = right; |
435 | 162 | } |
436 | |
|
437 | 128 | return lines; |
438 | |
} |
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
public void setComparator(Comparator comparator) { |
445 | 1 | this.comparator = comparator; |
446 | 1 | } |
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
public void setDisplaySettings(Set displaySettings) { |
456 | 1 | this.displaySettings = displaySettings; |
457 | 1 | } |
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
public void setDivider(String divider) { |
464 | 23 | this.divider = divider; |
465 | 23 | } |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
public void setException(OptionException exception) { |
472 | 4 | this.exception = exception; |
473 | 4 | } |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
public void setFooter(String footer) { |
480 | 24 | this.footer = footer; |
481 | 24 | } |
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
public void setFullUsageSettings(Set fullUsageSettings) { |
490 | 2 | this.fullUsageSettings = fullUsageSettings; |
491 | 2 | } |
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
public void setGroup(Group group) { |
498 | 42 | this.group = group; |
499 | 42 | } |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public void setHeader(String header) { |
506 | 24 | this.header = header; |
507 | 24 | } |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
public void setLineUsageSettings(Set lineUsageSettings) { |
516 | 1 | this.lineUsageSettings = lineUsageSettings; |
517 | 1 | } |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
public void setShellCommand(String shellCommand) { |
524 | 26 | this.shellCommand = shellCommand; |
525 | 26 | } |
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
public Comparator getComparator() { |
531 | 1 | return comparator; |
532 | |
} |
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
public Set getDisplaySettings() { |
538 | 1 | return displaySettings; |
539 | |
} |
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
public String getDivider() { |
545 | 1 | return divider; |
546 | |
} |
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public OptionException getException() { |
552 | 0 | return exception; |
553 | |
} |
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
public String getFooter() { |
559 | 1 | return footer; |
560 | |
} |
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
public Set getFullUsageSettings() { |
566 | 5 | return fullUsageSettings; |
567 | |
} |
568 | |
|
569 | |
|
570 | |
|
571 | |
|
572 | |
public Group getGroup() { |
573 | 1 | return group; |
574 | |
} |
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
public String getGutterCenter() { |
580 | 2 | return gutterCenter; |
581 | |
} |
582 | |
|
583 | |
|
584 | |
|
585 | |
|
586 | |
public String getGutterLeft() { |
587 | 2 | return gutterLeft; |
588 | |
} |
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
public String getGutterRight() { |
594 | 2 | return gutterRight; |
595 | |
} |
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
public String getHeader() { |
601 | 1 | return header; |
602 | |
} |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
public Set getLineUsageSettings() { |
608 | 4 | return lineUsageSettings; |
609 | |
} |
610 | |
|
611 | |
|
612 | |
|
613 | |
|
614 | |
public int getPageWidth() { |
615 | 1 | return pageWidth; |
616 | |
} |
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
public String getShellCommand() { |
622 | 1 | return shellCommand; |
623 | |
} |
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
public void setPrintWriter(PrintWriter out) { |
629 | 24 | this.out = out; |
630 | 24 | } |
631 | |
|
632 | |
|
633 | |
|
634 | |
|
635 | |
public PrintWriter getPrintWriter() { |
636 | 1 | return out; |
637 | |
} |
638 | |
} |