diff --git a/src/main/java/org/apache/commons/cli/HelpFormatter.java b/src/main/java/org/apache/commons/cli/HelpFormatter.java index 0da529f48..1884437fa 100644 --- a/src/main/java/org/apache/commons/cli/HelpFormatter.java +++ b/src/main/java/org/apache/commons/cli/HelpFormatter.java @@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.util.Iterator; import java.util.List; import java.util.Objects; -import java.util.function.BiFunction; +import java.util.function.Function; import java.util.function.Supplier; /** @@ -78,12 +78,12 @@ public static final class Builder implements Supplier { /** * A function to convert a description (not null) and a deprecated Option (not null) to help description */ - private static final BiFunction DEFAULT_DEPRECATED_FORMAT = (d, o) -> "[Deprecated] " + d; + private static final Function DEFAULT_DEPRECATED_FORMAT = o -> "[Deprecated] " + getDescription(o); /** * Formatter for deprecated options. */ - private BiFunction deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT; + private Function deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT; /** * The output PrintWriter, defaults to wrapping {@link System#out}. @@ -113,7 +113,7 @@ public Builder setPrintWriter(final PrintWriter printWriter) { * @return this. * @since 1.8.0 */ - public Builder setShowDeprecated(final BiFunction showDeprecatedFunc) { + public Builder setShowDeprecated(final Function showDeprecatedFunc) { this.deprecatedFormatFunc = showDeprecatedFunc; return this; } @@ -129,6 +129,16 @@ public Builder setShowDeprecated(final boolean useDefaultFormat) { } } + /** + * Gets the option description or an empty string if the description is {@code null}. + * @param option The option to get the description from. + * @return the option description or an empty string if the description is {@code null}. + * @since 1.8.0 + */ + public static String getDescription(final Option option) { + String desc = option.getDescription(); + return desc == null ? "" : desc; + } /** * This class implements the {@code Comparator} interface for comparing Options. */ @@ -266,9 +276,9 @@ private static PrintWriter createDefaultPrintWriter() { protected Comparator