1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.Serializable;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.function.Supplier;
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 public class HelpFormatter {
67
68
69
70
71
72
73 public static final class Builder implements Supplier<HelpFormatter> {
74
75
76
77
78
79
80 private boolean showDeprecated;
81
82
83
84
85 private PrintWriter printStream = createDefaultPrintWriter();
86
87 @Override
88 public HelpFormatter get() {
89 return new HelpFormatter(showDeprecated, printStream);
90 }
91
92
93
94
95
96
97
98 public Builder setPrintWriter(final PrintWriter printWriter) {
99 this.printStream = Objects.requireNonNull(printWriter, "printWriter");
100 return this;
101 }
102
103
104
105
106
107
108
109 public Builder setShowDeprecated(final boolean showDeprecated) {
110 this.showDeprecated = showDeprecated;
111 return this;
112 }
113
114 }
115
116
117
118
119 private static final class OptionComparator implements Comparator<Option>, Serializable {
120
121
122 private static final long serialVersionUID = 5305467873966684014L;
123
124
125
126
127
128
129
130
131
132
133 @Override
134 public int compare(final Option opt1, final Option opt2) {
135 return opt1.getKey().compareToIgnoreCase(opt2.getKey());
136 }
137 }
138
139
140 public static final int DEFAULT_WIDTH = 74;
141
142
143 public static final int DEFAULT_LEFT_PAD = 1;
144
145
146 public static final int DEFAULT_DESC_PAD = 3;
147
148
149 public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
150
151
152 public static final String DEFAULT_OPT_PREFIX = "-";
153
154
155 public static final String DEFAULT_LONG_OPT_PREFIX = "--";
156
157
158
159
160
161
162 public static final String DEFAULT_LONG_OPT_SEPARATOR = " ";
163
164
165 public static final String DEFAULT_ARG_NAME = "arg";
166
167
168
169
170
171
172
173 public static Builder builder() {
174 return new Builder();
175 }
176
177 private static PrintWriter createDefaultPrintWriter() {
178 return new PrintWriter(System.out);
179 }
180
181
182
183
184
185
186 @Deprecated
187 public int defaultWidth = DEFAULT_WIDTH;
188
189
190
191
192
193
194 @Deprecated
195 public int defaultLeftPad = DEFAULT_LEFT_PAD;
196
197
198
199
200
201
202 @Deprecated
203 public int defaultDescPad = DEFAULT_DESC_PAD;
204
205
206
207
208
209
210 @Deprecated
211 public String defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX;
212
213
214
215
216
217
218 @Deprecated
219 public String defaultNewLine = System.lineSeparator();
220
221
222
223
224
225
226 @Deprecated
227 public String defaultOptPrefix = DEFAULT_OPT_PREFIX;
228
229
230
231
232
233
234 @Deprecated
235 public String defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
236
237
238
239
240
241
242 @Deprecated
243 public String defaultArgName = DEFAULT_ARG_NAME;
244
245
246
247
248
249
250 protected Comparator<Option> optionComparator = new OptionComparator();
251
252
253
254
255 private final boolean showDeprecated;
256
257
258
259
260 private final PrintWriter printWriter;
261
262
263
264
265 private String longOptSeparator = DEFAULT_LONG_OPT_SEPARATOR;
266
267
268
269
270 public HelpFormatter() {
271 this(false, createDefaultPrintWriter());
272 }
273
274
275
276
277
278 private HelpFormatter(final boolean showDeprecated, final PrintWriter printStream) {
279
280
281 this.showDeprecated = showDeprecated;
282 this.printWriter = printStream;
283 }
284
285
286
287
288
289
290
291
292 private void appendOption(final StringBuffer buff, final Option option, final boolean required) {
293 if (!required) {
294 buff.append("[");
295 }
296 if (option.getOpt() != null) {
297 buff.append("-").append(option.getOpt());
298 } else {
299 buff.append("--").append(option.getLongOpt());
300 }
301
302 if (option.hasArg() && (option.getArgName() == null || !option.getArgName().isEmpty())) {
303 buff.append(option.getOpt() == null ? longOptSeparator : " ");
304 buff.append("<").append(option.getArgName() != null ? option.getArgName() : getArgName()).append(">");
305 }
306
307 if (!required) {
308 buff.append("]");
309 }
310 }
311
312
313
314
315
316
317
318
319
320 private void appendOptionGroup(final StringBuffer buff, final OptionGroup group) {
321 if (!group.isRequired()) {
322 buff.append("[");
323 }
324
325 final List<Option> optList = new ArrayList<>(group.getOptions());
326 if (getOptionComparator() != null) {
327 Collections.sort(optList, getOptionComparator());
328 }
329
330 for (final Iterator<Option> it = optList.iterator(); it.hasNext();) {
331
332 appendOption(buff, it.next(), true);
333
334 if (it.hasNext()) {
335 buff.append(" | ");
336 }
337 }
338
339 if (!group.isRequired()) {
340 buff.append("]");
341 }
342 }
343
344
345
346
347
348
349
350
351 protected String createPadding(final int len) {
352 final char[] padding = new char[len];
353 Arrays.fill(padding, ' ');
354 return new String(padding);
355 }
356
357
358
359
360
361
362
363
364
365
366
367 protected int findWrapPos(final String text, final int width, final int startPos) {
368
369 int pos = text.indexOf(Char.LF, startPos);
370 if (pos != -1 && pos <= width) {
371 return pos + 1;
372 }
373 pos = text.indexOf(Char.TAB, startPos);
374 if (pos != -1 && pos <= width) {
375 return pos + 1;
376 }
377 if (startPos + width >= text.length()) {
378 return -1;
379 }
380
381 for (pos = startPos + width; pos >= startPos; --pos) {
382 final char c = text.charAt(pos);
383 if (c == Char.SP || c == Char.LF || c == Char.CR) {
384 break;
385 }
386 }
387
388 if (pos > startPos) {
389 return pos;
390 }
391
392 pos = startPos + width;
393
394 return pos == text.length() ? -1 : pos;
395 }
396
397
398
399
400
401
402 public String getArgName() {
403 return defaultArgName;
404 }
405
406
407
408
409
410
411 public int getDescPadding() {
412 return defaultDescPad;
413 }
414
415
416
417
418
419
420 public int getLeftPadding() {
421 return defaultLeftPad;
422 }
423
424
425
426
427
428
429 public String getLongOptPrefix() {
430 return defaultLongOptPrefix;
431 }
432
433
434
435
436
437
438
439 public String getLongOptSeparator() {
440 return longOptSeparator;
441 }
442
443
444
445
446
447
448 public String getNewLine() {
449 return defaultNewLine;
450 }
451
452
453
454
455
456
457
458
459 public Comparator<Option> getOptionComparator() {
460 return optionComparator;
461 }
462
463
464
465
466
467
468 public String getOptPrefix() {
469 return defaultOptPrefix;
470 }
471
472
473
474
475
476
477 public String getSyntaxPrefix() {
478 return defaultSyntaxPrefix;
479 }
480
481
482
483
484
485
486 public int getWidth() {
487 return defaultWidth;
488 }
489
490
491
492
493
494
495
496
497
498
499
500 public void printHelp(final int width, final String cmdLineSyntax, final String header, final Options options, final String footer) {
501 printHelp(width, cmdLineSyntax, header, options, footer, false);
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515 public void printHelp(final int width, final String cmdLineSyntax, final String header, final Options options, final String footer,
516 final boolean autoUsage) {
517 final PrintWriter pw = new PrintWriter(printWriter);
518 printHelp(pw, width, cmdLineSyntax, header, options, getLeftPadding(), getDescPadding(), footer, autoUsage);
519 pw.flush();
520 }
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536 public void printHelp(final PrintWriter pw, final int width, final String cmdLineSyntax, final String header, final Options options, final int leftPad,
537 final int descPad, final String footer) {
538 printHelp(pw, width, cmdLineSyntax, header, options, leftPad, descPad, footer, false);
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555 public void printHelp(final PrintWriter pw, final int width, final String cmdLineSyntax, final String header, final Options options, final int leftPad,
556 final int descPad, final String footer, final boolean autoUsage) {
557 if (cmdLineSyntax == null || cmdLineSyntax.isEmpty()) {
558 throw new IllegalArgumentException("cmdLineSyntax not provided");
559 }
560 if (autoUsage) {
561 printUsage(pw, width, cmdLineSyntax, options);
562 } else {
563 printUsage(pw, width, cmdLineSyntax);
564 }
565 if (header != null && !header.isEmpty()) {
566 printWrapped(pw, width, header);
567 }
568 printOptions(pw, width, options, leftPad, descPad);
569 if (footer != null && !footer.isEmpty()) {
570 printWrapped(pw, width, footer);
571 }
572 }
573
574
575
576
577
578
579
580
581 public void printHelp(final String cmdLineSyntax, final Options options) {
582 printHelp(getWidth(), cmdLineSyntax, null, options, null, false);
583 }
584
585
586
587
588
589
590
591
592
593 public void printHelp(final String cmdLineSyntax, final Options options, final boolean autoUsage) {
594 printHelp(getWidth(), cmdLineSyntax, null, options, null, autoUsage);
595 }
596
597
598
599
600
601
602
603
604
605
606 public void printHelp(final String cmdLineSyntax, final String header, final Options options, final String footer) {
607 printHelp(cmdLineSyntax, header, options, footer, false);
608 }
609
610
611
612
613
614
615
616
617
618
619
620 public void printHelp(final String cmdLineSyntax, final String header, final Options options, final String footer, final boolean autoUsage) {
621 printHelp(getWidth(), cmdLineSyntax, header, options, footer, autoUsage);
622 }
623
624
625
626
627
628
629
630
631
632
633
634 public void printOptions(final PrintWriter pw, final int width, final Options options, final int leftPad, final int descPad) {
635 final StringBuffer sb = new StringBuffer();
636 renderOptions(sb, width, options, leftPad, descPad);
637 pw.println(sb.toString());
638 }
639
640
641
642
643
644
645
646
647 public void printUsage(final PrintWriter pw, final int width, final String cmdLineSyntax) {
648 final int argPos = cmdLineSyntax.indexOf(' ') + 1;
649 printWrapped(pw, width, getSyntaxPrefix().length() + argPos, getSyntaxPrefix() + cmdLineSyntax);
650 }
651
652
653
654
655
656
657
658
659
660 public void printUsage(final PrintWriter pw, final int width, final String app, final Options options) {
661
662 final StringBuffer buff = new StringBuffer(getSyntaxPrefix()).append(app).append(Char.SP);
663
664 final Collection<OptionGroup> processedGroups = new ArrayList<>();
665 final List<Option> optList = new ArrayList<>(options.getOptions());
666 if (getOptionComparator() != null) {
667 Collections.sort(optList, getOptionComparator());
668 }
669
670 for (final Iterator<Option> it = optList.iterator(); it.hasNext();) {
671
672 final Option option = it.next();
673
674 final OptionGroup group = options.getOptionGroup(option);
675
676 if (group != null) {
677
678 if (!processedGroups.contains(group)) {
679
680 processedGroups.add(group);
681
682 appendOptionGroup(buff, group);
683 }
684
685
686 }
687
688 else {
689 appendOption(buff, option, option.isRequired());
690 }
691 if (it.hasNext()) {
692 buff.append(Char.SP);
693 }
694 }
695
696
697 printWrapped(pw, width, buff.toString().indexOf(' ') + 1, buff.toString());
698 }
699
700
701
702
703
704
705
706
707
708 public void printWrapped(final PrintWriter pw, final int width, final int nextLineTabStop, final String text) {
709 final StringBuffer sb = new StringBuffer(text.length());
710 renderWrappedTextBlock(sb, width, nextLineTabStop, text);
711 pw.println(sb.toString());
712 }
713
714
715
716
717
718
719
720
721 public void printWrapped(final PrintWriter pw, final int width, final String text) {
722 printWrapped(pw, width, 0, text);
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736 protected StringBuffer renderOptions(final StringBuffer sb, final int width, final Options options, final int leftPad, final int descPad) {
737 final String lpad = createPadding(leftPad);
738 final String dpad = createPadding(descPad);
739
740
741
742
743 int max = 0;
744 final List<StringBuffer> prefixList = new ArrayList<>();
745 final List<Option> optList = options.helpOptions();
746 if (getOptionComparator() != null) {
747 Collections.sort(optList, getOptionComparator());
748 }
749 for (final Option option : optList) {
750 final StringBuffer optBuf = new StringBuffer();
751 if (option.getOpt() == null) {
752 optBuf.append(lpad).append(" ").append(getLongOptPrefix()).append(option.getLongOpt());
753 } else {
754 optBuf.append(lpad).append(getOptPrefix()).append(option.getOpt());
755 if (option.hasLongOpt()) {
756 optBuf.append(',').append(getLongOptPrefix()).append(option.getLongOpt());
757 }
758 }
759 if (option.hasArg()) {
760 final String argName = option.getArgName();
761 if (argName != null && argName.isEmpty()) {
762
763 optBuf.append(' ');
764 } else {
765 optBuf.append(option.hasLongOpt() ? longOptSeparator : " ");
766 optBuf.append("<").append(argName != null ? option.getArgName() : getArgName()).append(">");
767 }
768 }
769 prefixList.add(optBuf);
770 max = Math.max(optBuf.length(), max);
771 }
772 int x = 0;
773 for (final Iterator<Option> it = optList.iterator(); it.hasNext();) {
774 final Option option = it.next();
775 final StringBuilder optBuf = new StringBuilder(prefixList.get(x++).toString());
776 if (optBuf.length() < max) {
777 optBuf.append(createPadding(max - optBuf.length()));
778 }
779 optBuf.append(dpad);
780 final int nextLineTabStop = max + descPad;
781 if (showDeprecated && option.isDeprecated()) {
782 optBuf.append("[Deprecated]");
783 }
784 if (option.getDescription() != null) {
785 if (showDeprecated && option.isDeprecated()) {
786 optBuf.append(' ');
787 }
788 optBuf.append(option.getDescription());
789 }
790 renderWrappedText(sb, width, nextLineTabStop, optBuf.toString());
791 if (it.hasNext()) {
792 sb.append(getNewLine());
793 }
794 }
795 return sb;
796 }
797
798
799
800
801
802
803
804
805
806
807
808 protected StringBuffer renderWrappedText(final StringBuffer sb, final int width, final int nextLineTabStop, final String text) {
809 String render = text;
810 int nextLineTabStopPos = nextLineTabStop;
811 int pos = findWrapPos(render, width, 0);
812 if (pos == -1) {
813 sb.append(rtrim(render));
814 return sb;
815 }
816 sb.append(rtrim(render.substring(0, pos))).append(getNewLine());
817 if (nextLineTabStopPos >= width) {
818
819 nextLineTabStopPos = 1;
820 }
821
822 final String padding = createPadding(nextLineTabStopPos);
823 while (true) {
824 render = padding + render.substring(pos).trim();
825 pos = findWrapPos(render, width, 0);
826 if (pos == -1) {
827 sb.append(render);
828 return sb;
829 }
830 if (render.length() > width && pos == nextLineTabStopPos - 1) {
831 pos = width;
832 }
833 sb.append(rtrim(render.substring(0, pos))).append(getNewLine());
834 }
835 }
836
837
838
839
840
841
842
843
844
845
846 private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width, final int nextLineTabStop, final String text) {
847 try {
848 final BufferedReader in = new BufferedReader(new StringReader(text));
849 String line;
850 boolean firstLine = true;
851 while ((line = in.readLine()) != null) {
852 if (!firstLine) {
853 sb.append(getNewLine());
854 } else {
855 firstLine = false;
856 }
857 renderWrappedText(sb, width, nextLineTabStop, line);
858 }
859 } catch (final IOException e) {
860
861 }
862 return sb;
863 }
864
865
866
867
868
869
870
871 protected String rtrim(final String s) {
872 if (s == null || s.isEmpty()) {
873 return s;
874 }
875 int pos = s.length();
876 while (pos > 0 && Character.isWhitespace(s.charAt(pos - 1))) {
877 --pos;
878 }
879 return s.substring(0, pos);
880 }
881
882
883
884
885
886
887 public void setArgName(final String name) {
888 this.defaultArgName = name;
889 }
890
891
892
893
894
895
896 public void setDescPadding(final int padding) {
897 this.defaultDescPad = padding;
898 }
899
900
901
902
903
904
905 public void setLeftPadding(final int padding) {
906 this.defaultLeftPad = padding;
907 }
908
909
910
911
912
913
914 public void setLongOptPrefix(final String prefix) {
915 this.defaultLongOptPrefix = prefix;
916 }
917
918
919
920
921
922
923
924
925 public void setLongOptSeparator(final String longOptSeparator) {
926 this.longOptSeparator = longOptSeparator;
927 }
928
929
930
931
932
933
934 public void setNewLine(final String newline) {
935 this.defaultNewLine = newline;
936 }
937
938
939
940
941
942
943
944
945 public void setOptionComparator(final Comparator<Option> comparator) {
946 this.optionComparator = comparator;
947 }
948
949
950
951
952
953
954 public void setOptPrefix(final String prefix) {
955 this.defaultOptPrefix = prefix;
956 }
957
958
959
960
961
962
963 public void setSyntaxPrefix(final String prefix) {
964 this.defaultSyntaxPrefix = prefix;
965 }
966
967
968
969
970
971
972 public void setWidth(final int width) {
973 this.defaultWidth = width;
974 }
975
976 }