@@ -32,11 +32,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3232
3333/**
3434 * A formatter of help messages for command line options.
35- *
3635 * <p>
3736 * Example:
3837 * </p>
39- *
4038 * <pre>
4139 * Options options = new Options();
4240 * options.addOption(OptionBuilder.withLongOpt("file").withDescription("The file to be processed").hasArg().withArgName("FILE").isRequired().create('f'));
@@ -49,9 +47,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4947 * HelpFormatter formatter = new HelpFormatter();
5048 * formatter.printHelp("myapp", header, options, footer, true);
5149 * </pre>
52- *
50+ * <p>
5351 * This produces the following output:
54- *
52+ * </p>
5553 * <pre>
5654 * usage: myapp -f <FILE> [-h] [-v]
5755 * Do something useful with an input file
@@ -202,19 +200,16 @@ private void appendOption(final StringBuffer buff, final Option option, final bo
202200 if (!required ) {
203201 buff .append ("[" );
204202 }
205-
206203 if (option .getOpt () != null ) {
207204 buff .append ("-" ).append (option .getOpt ());
208205 } else {
209206 buff .append ("--" ).append (option .getLongOpt ());
210207 }
211-
212208 // if the Option has a value and a non blank argname
213209 if (option .hasArg () && (option .getArgName () == null || !option .getArgName ().isEmpty ())) {
214210 buff .append (option .getOpt () == null ? longOptSeparator : " " );
215211 buff .append ("<" ).append (option .getArgName () != null ? option .getArgName () : getArgName ()).append (">" );
216212 }
217-
218213 // if the Option is not a required option
219214 if (!required ) {
220215 buff .append ("]" );
@@ -263,7 +258,6 @@ private void appendOptionGroup(final StringBuffer buff, final OptionGroup group)
263258 protected String createPadding (final int len ) {
264259 final char [] padding = new char [len ];
265260 Arrays .fill (padding , ' ' );
266-
267261 return new String (padding );
268262 }
269263
@@ -283,29 +277,24 @@ protected int findWrapPos(final String text, final int width, final int startPos
283277 if (pos != -1 && pos <= width ) {
284278 return pos + 1 ;
285279 }
286-
287280 pos = text .indexOf ('\t' , startPos );
288281 if (pos != -1 && pos <= width ) {
289282 return pos + 1 ;
290283 }
291-
292284 if (startPos + width >= text .length ()) {
293285 return -1 ;
294286 }
295-
296287 // look for the last whitespace character before startPos+width
297288 for (pos = startPos + width ; pos >= startPos ; --pos ) {
298289 final char c = text .charAt (pos );
299290 if (c == ' ' || c == '\n' || c == '\r' ) {
300291 break ;
301292 }
302293 }
303-
304294 // if we found it - just return
305295 if (pos > startPos ) {
306296 return pos ;
307297 }
308-
309298 // if we didn't find one, simply chop at startPos+width
310299 pos = startPos + width ;
311300
@@ -433,7 +422,6 @@ public void printHelp(final int width, final String cmdLineSyntax, final String
433422 public void printHelp (final int width , final String cmdLineSyntax , final String header , final Options options , final String footer ,
434423 final boolean autoUsage ) {
435424 final PrintWriter pw = new PrintWriter (System .out );
436-
437425 printHelp (pw , width , cmdLineSyntax , header , options , getLeftPadding (), getDescPadding (), footer , autoUsage );
438426 pw .flush ();
439427 }
@@ -477,19 +465,15 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
477465 if (cmdLineSyntax == null || cmdLineSyntax .isEmpty ()) {
478466 throw new IllegalArgumentException ("cmdLineSyntax not provided" );
479467 }
480-
481468 if (autoUsage ) {
482469 printUsage (pw , width , cmdLineSyntax , options );
483470 } else {
484471 printUsage (pw , width , cmdLineSyntax );
485472 }
486-
487473 if (header != null && !header .isEmpty ()) {
488474 printWrapped (pw , width , header );
489475 }
490-
491476 printOptions (pw , width , options , leftPad , descPad );
492-
493477 if (footer != null && !footer .isEmpty ()) {
494478 printWrapped (pw , width , footer );
495479 }
@@ -557,7 +541,6 @@ public void printHelp(final String cmdLineSyntax, final String header, final Opt
557541 */
558542 public void printOptions (final PrintWriter pw , final int width , final Options options , final int leftPad , final int descPad ) {
559543 final StringBuffer sb = new StringBuffer ();
560-
561544 renderOptions (sb , width , options , leftPad , descPad );
562545 pw .println (sb .toString ());
563546 }
@@ -571,7 +554,6 @@ public void printOptions(final PrintWriter pw, final int width, final Options op
571554 */
572555 public void printUsage (final PrintWriter pw , final int width , final String cmdLineSyntax ) {
573556 final int argPos = cmdLineSyntax .indexOf (' ' ) + 1 ;
574-
575557 printWrapped (pw , width , getSyntaxPrefix ().length () + argPos , getSyntaxPrefix () + cmdLineSyntax );
576558 }
577559
@@ -586,10 +568,8 @@ public void printUsage(final PrintWriter pw, final int width, final String cmdLi
586568 public void printUsage (final PrintWriter pw , final int width , final String app , final Options options ) {
587569 // initialize the string buffer
588570 final StringBuffer buff = new StringBuffer (getSyntaxPrefix ()).append (app ).append (" " );
589-
590571 // create a list for processed option groups
591572 final Collection <OptionGroup > processedGroups = new ArrayList <>();
592-
593573 final List <Option > optList = new ArrayList <>(options .getOptions ());
594574 if (getOptionComparator () != null ) {
595575 Collections .sort (optList , getOptionComparator ());
@@ -598,30 +578,24 @@ public void printUsage(final PrintWriter pw, final int width, final String app,
598578 for (final Iterator <Option > it = optList .iterator (); it .hasNext ();) {
599579 // get the next Option
600580 final Option option = it .next ();
601-
602581 // check if the option is part of an OptionGroup
603582 final OptionGroup group = options .getOptionGroup (option );
604-
605583 // if the option is part of a group
606584 if (group != null ) {
607585 // and if the group has not already been processed
608586 if (!processedGroups .contains (group )) {
609587 // add the group to the processed list
610588 processedGroups .add (group );
611-
612589 // add the usage clause
613590 appendOptionGroup (buff , group );
614591 }
615-
616592 // otherwise the option was displayed in the group
617593 // previously so ignore it.
618594 }
619-
620595 // if the Option is not part of an OptionGroup
621596 else {
622597 appendOption (buff , option , option .isRequired ());
623598 }
624-
625599 if (it .hasNext ()) {
626600 buff .append (" " );
627601 }
@@ -641,7 +615,6 @@ public void printUsage(final PrintWriter pw, final int width, final String app,
641615 */
642616 public void printWrapped (final PrintWriter pw , final int width , final int nextLineTabStop , final String text ) {
643617 final StringBuffer sb = new StringBuffer (text .length ());
644-
645618 renderWrappedTextBlock (sb , width , nextLineTabStop , text );
646619 pw .println (sb .toString ());
647620 }
@@ -671,33 +644,26 @@ public void printWrapped(final PrintWriter pw, final int width, final String tex
671644 protected StringBuffer renderOptions (final StringBuffer sb , final int width , final Options options , final int leftPad , final int descPad ) {
672645 final String lpad = createPadding (leftPad );
673646 final String dpad = createPadding (descPad );
674-
675647 // first create list containing only <lpad>-a,--aaa where
676648 // -a is opt and --aaa is long opt; in parallel look for
677649 // the longest opt string this list will be then used to
678650 // sort options ascending
679651 int max = 0 ;
680652 final List <StringBuffer > prefixList = new ArrayList <>();
681-
682653 final List <Option > optList = options .helpOptions ();
683-
684654 if (getOptionComparator () != null ) {
685655 Collections .sort (optList , getOptionComparator ());
686656 }
687-
688657 for (final Option option : optList ) {
689658 final StringBuffer optBuf = new StringBuffer ();
690-
691659 if (option .getOpt () == null ) {
692660 optBuf .append (lpad ).append (" " ).append (getLongOptPrefix ()).append (option .getLongOpt ());
693661 } else {
694662 optBuf .append (lpad ).append (getOptPrefix ()).append (option .getOpt ());
695-
696663 if (option .hasLongOpt ()) {
697664 optBuf .append (',' ).append (getLongOptPrefix ()).append (option .getLongOpt ());
698665 }
699666 }
700-
701667 if (option .hasArg ()) {
702668 final String argName = option .getArgName ();
703669 if (argName != null && argName .isEmpty ()) {
@@ -708,36 +674,26 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin
708674 optBuf .append ("<" ).append (argName != null ? option .getArgName () : getArgName ()).append (">" );
709675 }
710676 }
711-
712677 prefixList .add (optBuf );
713678 max = Math .max (optBuf .length (), max );
714679 }
715-
716680 int x = 0 ;
717-
718681 for (final Iterator <Option > it = optList .iterator (); it .hasNext ();) {
719682 final Option option = it .next ();
720683 final StringBuilder optBuf = new StringBuilder (prefixList .get (x ++).toString ());
721-
722684 if (optBuf .length () < max ) {
723685 optBuf .append (createPadding (max - optBuf .length ()));
724686 }
725-
726687 optBuf .append (dpad );
727-
728688 final int nextLineTabStop = max + descPad ;
729-
730689 if (option .getDescription () != null ) {
731690 optBuf .append (option .getDescription ());
732691 }
733-
734692 renderWrappedText (sb , width , nextLineTabStop , optBuf .toString ());
735-
736693 if (it .hasNext ()) {
737694 sb .append (getNewLine ());
738695 }
739696 }
740-
741697 return sb ;
742698 }
743699
@@ -755,36 +711,27 @@ protected StringBuffer renderWrappedText(final StringBuffer sb, final int width,
755711 String render = text ;
756712 int nextLineTabStopPos = nextLineTabStop ;
757713 int pos = findWrapPos (render , width , 0 );
758-
759714 if (pos == -1 ) {
760715 sb .append (rtrim (render ));
761-
762716 return sb ;
763717 }
764718 sb .append (rtrim (render .substring (0 , pos ))).append (getNewLine ());
765-
766719 if (nextLineTabStopPos >= width ) {
767720 // stops infinite loop happening
768721 nextLineTabStopPos = 1 ;
769722 }
770-
771723 // all following lines must be padded with nextLineTabStop space characters
772724 final String padding = createPadding (nextLineTabStopPos );
773-
774725 while (true ) {
775726 render = padding + render .substring (pos ).trim ();
776727 pos = findWrapPos (render , width , 0 );
777-
778728 if (pos == -1 ) {
779729 sb .append (render );
780-
781730 return sb ;
782731 }
783-
784732 if (render .length () > width && pos == nextLineTabStopPos - 1 ) {
785733 pos = width ;
786734 }
787-
788735 sb .append (rtrim (render .substring (0 , pos ))).append (getNewLine ());
789736 }
790737 }
@@ -814,7 +761,6 @@ private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width
814761 } catch (final IOException e ) { // NOPMD
815762 // cannot happen
816763 }
817-
818764 return sb ;
819765 }
820766
@@ -829,13 +775,10 @@ protected String rtrim(final String s) {
829775 if (s == null || s .isEmpty ()) {
830776 return s ;
831777 }
832-
833778 int pos = s .length ();
834-
835779 while (pos > 0 && Character .isWhitespace (s .charAt (pos - 1 ))) {
836780 --pos ;
837781 }
838-
839782 return s .substring (0 , pos );
840783 }
841784
0 commit comments