From a955ec39a3ea28c07a8c32315acfa8e6ebc2f4ba Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sat, 11 May 2024 16:34:50 +0200 Subject: [PATCH 1/4] added ability to display deprecation information in help output --- .../org/apache/commons/cli/HelpFormatter.java | 47 +++++++++------ .../apache/commons/cli/HelpFormatterTest.java | 59 +++++++++++++++++++ 2 files changed, 87 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/apache/commons/cli/HelpFormatter.java b/src/main/java/org/apache/commons/cli/HelpFormatter.java index 539e18ceb..7f3e9aee0 100644 --- a/src/main/java/org/apache/commons/cli/HelpFormatter.java +++ b/src/main/java/org/apache/commons/cli/HelpFormatter.java @@ -30,6 +30,8 @@ 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.Function; +import java.util.function.BiFunction; import java.util.function.Supplier; /** @@ -74,10 +76,12 @@ public static final class Builder implements Supplier { // TODO All other instance HelpFormatter instance variables. // Make HelpFormatter immutable for 2.0 + private static final BiFunction DEFAULT_DEPRECATED_FORMAT = (d,o) -> "[Deprecated] "+d; + /** - * Whether to show deprecated options. + * Formatter for deprecated options. */ - private boolean showDeprecated; + private BiFunction deprecatedFormatFunc; /** * The output PrintWriter, defaults to wrapping {@link System#out}. @@ -86,7 +90,7 @@ public static final class Builder implements Supplier { @Override public HelpFormatter get() { - return new HelpFormatter(showDeprecated, printStream); + return new HelpFormatter(deprecatedFormatFunc, printStream); } /** @@ -103,14 +107,23 @@ public Builder setPrintWriter(final PrintWriter printWriter) { /** * Sets whether to show deprecated options. * - * @param showDeprecated Whether to show deprecated options. + * @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter. * @return this. */ - public Builder setShowDeprecated(final boolean showDeprecated) { - this.showDeprecated = showDeprecated; - return this; + public Builder setShowDeprecated(final boolean useDefaultFormat) { + return setShowDeprecated(useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null); } + /** + * Sets whether to show deprecated options. + * + * @param showDeprecatedFunc Specify the format for the deprecated options. + * @return this. + */ + public Builder setShowDeprecated(final BiFunction showDeprecatedFunc) { + this.deprecatedFormatFunc = showDeprecatedFunc; + return this; + } } /** @@ -250,9 +263,9 @@ private static PrintWriter createDefaultPrintWriter() { protected Comparator