@@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3030import java .util .Iterator ;
3131import java .util .List ;
3232import java .util .Objects ;
33- import java .util .function .BiFunction ;
33+ import java .util .function .Function ;
3434import java .util .function .Supplier ;
3535
3636/**
@@ -78,12 +78,12 @@ public static final class Builder implements Supplier<HelpFormatter> {
7878 /**
7979 * A function to convert a description (not null) and a deprecated Option (not null) to help description
8080 */
81- private static final BiFunction < String , Option , String > DEFAULT_DEPRECATED_FORMAT = ( d , o ) -> "[Deprecated] " + d ;
81+ private static final Function < Option , String > DEFAULT_DEPRECATED_FORMAT = o -> "[Deprecated] " + getDescription ( o ) ;
8282
8383 /**
8484 * Formatter for deprecated options.
8585 */
86- private BiFunction < String , Option , String > deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT ;
86+ private Function < Option , String > deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT ;
8787
8888 /**
8989 * The output PrintWriter, defaults to wrapping {@link System#out}.
@@ -109,23 +109,23 @@ public Builder setPrintWriter(final PrintWriter printWriter) {
109109 /**
110110 * Sets whether to show deprecated options.
111111 *
112- * @param showDeprecatedFunc Specify the format for the deprecated options .
112+ * @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter .
113113 * @return this.
114- * @since 1.8.0
115114 */
116- public Builder setShowDeprecated (final BiFunction <String , Option , String > showDeprecatedFunc ) {
117- this .deprecatedFormatFunc = showDeprecatedFunc ;
118- return this ;
115+ public Builder setShowDeprecated (final boolean useDefaultFormat ) {
116+ return setShowDeprecated (useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null );
119117 }
120118
121119 /**
122120 * Sets whether to show deprecated options.
123121 *
124- * @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter .
122+ * @param showDeprecatedFunc Specify the format for the deprecated options .
125123 * @return this.
124+ * @since 1.8.0
126125 */
127- public Builder setShowDeprecated (final boolean useDefaultFormat ) {
128- return setShowDeprecated (useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null );
126+ public Builder setShowDeprecated (final Function <Option , String > showDeprecatedFunc ) {
127+ this .deprecatedFormatFunc = showDeprecatedFunc ;
128+ return this ;
129129 }
130130 }
131131
@@ -151,7 +151,6 @@ public int compare(final Option opt1, final Option opt2) {
151151 return opt1 .getKey ().compareToIgnoreCase (opt2 .getKey ());
152152 }
153153 }
154-
155154 /** Default number of characters per line */
156155 public static final int DEFAULT_WIDTH = 74 ;
157156
@@ -194,6 +193,17 @@ private static PrintWriter createDefaultPrintWriter() {
194193 return new PrintWriter (System .out );
195194 }
196195
196+ /**
197+ * Gets the option description or an empty string if the description is {@code null}.
198+ * @param option The option to get the description from.
199+ * @return the option description or an empty string if the description is {@code null}.
200+ * @since 1.8.0
201+ */
202+ public static String getDescription (final Option option ) {
203+ final String desc = option .getDescription ();
204+ return desc == null ? "" : desc ;
205+ }
206+
197207 /**
198208 * Number of characters per line
199209 *
@@ -266,9 +276,9 @@ private static PrintWriter createDefaultPrintWriter() {
266276 protected Comparator <Option > optionComparator = new OptionComparator ();
267277
268278 /**
269- * BiFunction to format the description for a deprecated option.
279+ * Function to format the description for a deprecated option.
270280 */
271- private final BiFunction < String , Option , String > deprecatedFormatFunc ;
281+ private final Function < Option , String > deprecatedFormatFunc ;
272282
273283 /**
274284 * Where to print help.
@@ -291,7 +301,7 @@ public HelpFormatter() {
291301 * Constructs a new instance.
292302 * @param printStream TODO
293303 */
294- private HelpFormatter (final BiFunction < String , Option , String > deprecatedFormatFunc , final PrintWriter printStream ) {
304+ private HelpFormatter (final Function < Option , String > deprecatedFormatFunc , final PrintWriter printStream ) {
295305 // TODO All other instance HelpFormatter instance variables.
296306 // Make HelpFormatter immutable for 2.0
297307 this .deprecatedFormatFunc = deprecatedFormatFunc ;
@@ -570,7 +580,7 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
570580 */
571581 public void printHelp (final PrintWriter pw , final int width , final String cmdLineSyntax , final String header , final Options options , final int leftPad ,
572582 final int descPad , final String footer , final boolean autoUsage ) {
573- if (cmdLineSyntax == null || cmdLineSyntax .isEmpty ()) {
583+ if (Util .isEmpty (cmdLineSyntax )) {
574584 throw new IllegalArgumentException ("cmdLineSyntax not provided" );
575585 }
576586 if (autoUsage ) {
@@ -795,7 +805,7 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin
795805 optBuf .append (dpad );
796806 final int nextLineTabStop = max + descPad ;
797807 if (deprecatedFormatFunc != null && option .isDeprecated ()) {
798- optBuf .append (deprecatedFormatFunc .apply (option . getDescription () == null ? "" : option . getDescription (), option ).trim ());
808+ optBuf .append (deprecatedFormatFunc .apply (option ).trim ());
799809 } else if (option .getDescription () != null ) {
800810 optBuf .append (option .getDescription ());
801811 }
@@ -881,7 +891,7 @@ private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width
881891 * @return The String of without the trailing padding
882892 */
883893 protected String rtrim (final String s ) {
884- if (s == null || s .isEmpty ()) {
894+ if (Util .isEmpty (s )) {
885895 return s ;
886896 }
887897 int pos = s .length ();
0 commit comments