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
@@ -466,145 +464,141 @@ catch (ParseException exp) {
deprecated method.
Usage of the deprecated option is announced when the presence of the option is checked
- or the value of the option is retrieved. By default the announcement printed to Standard out.
+ or the value of the option is retrieved. By default, the announcement printed to Standard out.
- The HelpFormatter output will be default show the description prefixed by "[Deprecated]"
+ The HelpFormatter output will by default show the description prefixed by "[Deprecated]"
The examples below will implement doSomething in the following code block.
- public static void main(String[] args) {
- Option n = Option.builder("n")
- .deprecated(DeprecatedAttributes.builder()
- .setDescription("Use '-count' instead")
- .setForRemoval(true)
- .setSince("now").get())
- .hasArg()
- .desc("the number of things")
- .type(Integer.class)
- .build();
- Option count = Option.builder("count")
- .hasArg()
- .desc("the number of things")
- .type(Integer.class)
- .build();
- Options options = new Options().addOption(n).addOption(count);
+ public static void main(String[] args) {
+ Option n = Option.builder("n")
+ .deprecated(DeprecatedAttributes.builder()
+ .setDescription("Use '-count' instead")
+ .setForRemoval(true)
+ .setSince("now").get())
+ .hasArg()
+ .desc("the number of things")
+ .type(Integer.class)
+ .build();
+ Option count = Option.builder("count")
+ .hasArg()
+ .desc("the number of things")
+ .type(Integer.class)
+ .build();
+ Options options = new Options().addOption(n).addOption(count);
- doSomething(options);
- }
-
+ doSomething(options);
+ }
- The usage announcement may be changed by providing a Consumer>Option<> to the
- CommandLine.Builder.deprecatedHandler method.
+ The usage announcement may be changed by providing a Consumer<Option> to the
+ CommandLine.Builder.deprecatedHandler method. This is commonly used to log usage
+ of deprecated options rather than printing them on the standard output.
for example if doSomething is implemented as:
-
- void doSomething(Options options) {
- CommandLineParser parser = new DefaultParser();
- CommandLine line;
- try {
- // parse the command line arguments
- line = parser.parse(options, new String[] {"-n", "5"});
- System.out.println( "n="+line.getParsedOptionValue("n"));
- } catch (ParseException exp) {
- // oops, something went wrong
- System.err.println("Parsing failed. Reason: " + exp.getMessage());
- }
- }
-
+
+ void doSomething(Options options) {
+ CommandLineParser parser = new DefaultParser();
+ CommandLine line;
+ try {
+ // parse the command line arguments
+ line = parser.parse(options, new String[] {"-n", "5"});
+ System.out.println("n=" + line.getParsedOptionValue("n"));
+ } catch (ParseException exp) {
+ // oops, something went wrong
+ System.err.println("Parsing failed. Reason: " + exp.getMessage());
+ }
+ }
The output of the run would be.
- Option 'n': Deprecated for removal since now: Use '-count' instead
- n=5
-
+ Option 'n': Deprecated for removal since now: Use '-count' instead
+ n=5
- ERROR: Option 'n': Deprecated for removal since now: Use '-count' instead
- n=5
-
+ ERROR: Option 'n': Deprecated for removal since now: Use '-count' instead
+ n=5
However, the first line would be printed on system err instead of system out.
+
By default the help formater prints "[Deprecated]" in front of the description for the option. This can
+ be changed to display any values desired.
+
If doSomething is implemented as:
- void doSomething(Options options) {
- HelpFormatter formatter = HelpFormatter.builder().get();
- formatter.printHelp("Command line syntax:", options);
- }
-
+ void doSomething(Options options) {
+ HelpFormatter formatter = HelpFormatter.builder().get();
+ formatter.printHelp("Command line syntax:", options);
+ }
To output is
- usage: Command line syntax:
- -count <arg> the number of things
- -n <arg> [Deprecated] the number of things
-
+ usage: Command line syntax:
+ -count <arg> the number of things
+ -n <arg> [Deprecated] the number of things
The display of deprecated options may be changed through the use of the
HelpFormatter.Builder.setShowDeprecated() method.
Calling HelpFormatter.Builder.setShowDeprecated(false) will disable the "[Deprecated]" tag.
-
Calling HelpFormatter.Builder.setShowDeprecated with a BiFunction<String, Option, String>
+
Calling HelpFormatter.Builder.setShowDeprecated with a Function<Option, String>
will use the output of the function as the description for the option.
As an example of the second case above, changing the implementation of doSomething to
- void doSomething(Options options) {
- BiFunction<String, Option, String> disp = (desc, option) -> String.format( "%s. %s", desc, option.getDeprecated().toString());
- HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(disp).get();
- formatter.printHelp("Command line syntax:", options);
- }
-
+ void doSomething(Options options) {
+ Function<Option, String> disp = option -> String.format("%s. %s", HelpFormatter.getDescription(option),
+ option.getDeprecated().toString());
+ HelpFormatter formatter = HelpFormatter.builder().setShowDeprecated(disp).get();
+ formatter.printHelp("Command line syntax:", options);
+ }
changes the output to
- usage: Command line syntax:
- -count <arg> the number of things
- -n <arg> the number of things. Deprecated for removal since now:
- Use '-count' instead
-
+ usage: Command line syntax:
+ -count <arg> the number of things
+ -n <arg> the number of things. Deprecated for removal since now:
+ Use '-count' instead
diff --git a/src/test/java/org/apache/commons/cli/HelpFormatterTest.java b/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
index 4d702c41b..1ca74002e 100644
--- a/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
+++ b/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
@@ -57,7 +57,7 @@ static Stream deprecatedOptionsProvider() {
hf = HelpFormatter.builder().setShowDeprecated(true).get();
lst.add(Arguments.of(hf, option, "[Deprecated] dddd dddd dddd"));
- hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
+ hf = HelpFormatter.builder().setShowDeprecated(o -> String.format("%s [%s]", HelpFormatter.getDescription(o), o.getDeprecated())).get();
lst.add(Arguments.of(hf, option, "dddd dddd dddd [Deprecated for removal since now: Why why why]"));
option = Option.builder("a").longOpt("aaa")
@@ -74,7 +74,7 @@ static Stream deprecatedOptionsProvider() {
hf = HelpFormatter.builder().setShowDeprecated(true).get();
lst.add(Arguments.of(hf, option, "[Deprecated]"));
- hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
+ hf = HelpFormatter.builder().setShowDeprecated(o -> String.format("%s [%s]", HelpFormatter.getDescription(o), o.getDeprecated())).get();
lst.add(Arguments.of(hf, option, "[Deprecated for removal since now: Why why why]"));
return lst.stream();
@@ -186,9 +186,9 @@ public void testHeaderStartingWithLineSeparator0() {
//@formatter:off
assertEquals(
"usage: foobar" + EOL +
- "" + EOL +
+ EOL +
"Header" + EOL +
- "" + EOL +
+ EOL +
"Footer" + EOL,
out.toString());
//@formatter:on
@@ -217,9 +217,9 @@ public void testHeaderStartingWithLineSeparator1() {
//@formatter:off
assertEquals(
"usage: foobar" + EOL +
- "" + EOL +
+ EOL +
"Header" + EOL +
- "" + EOL +
+ EOL +
"Footer" + EOL,
out.toString());
//@formatter:on
@@ -228,9 +228,9 @@ public void testHeaderStartingWithLineSeparator1() {
//@formatter:off
assertEquals(
"usage: foobar" + EOL +
- "" + EOL +
+ EOL +
"Header" + EOL +
- "" + EOL +
+ EOL +
"Footer" + EOL,
out.toString());
//@formatter:on
@@ -239,9 +239,9 @@ public void testHeaderStartingWithLineSeparator1() {
//@formatter:off
assertEquals(
"usage: foobar" + EOL +
- "" + EOL +
+ EOL +
"Header" + EOL +
- "" + EOL +
+ EOL +
"Footer" + EOL,
out.toString());
//@formatter:on
@@ -250,9 +250,9 @@ public void testHeaderStartingWithLineSeparator1() {
//@formatter:off
assertEquals(
"usage: foobar" + EOL +
- "" + EOL +
+ EOL +
"Header" + EOL +
- "" + EOL +
+ EOL +
"Footer" + EOL,
out.toString());
//@formatter:on