diff --git a/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java b/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java index 7a370e129..0e40df300 100644 --- a/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java +++ b/src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java @@ -284,6 +284,19 @@ public final String getSyntaxPrefix() { */ protected abstract TableDefinition getTableDefinition(Iterable
- CLI uses the
+ CLI uses the
Options class, as a container for
-
+
Option instances. There are two ways to create
Options in CLI. One of them is via the constructors,
the other way is via the factory methods defined in
@@ -86,11 +86,11 @@
The parse method defined on
-
+
CommandLineParser takes an Options
instance and a String[] of arguments and
returns a
-
+
CommandLine.
@@ -142,7 +142,7 @@
- An
+ An
Options object must be created and the Option must be
added to it.
Now we need to check if the t option is present. To do
this we will interrogate the
- CommandLine
+ CommandLine
object. The hasOption method takes a
java.lang.String parameter and returns true if the option
represented by the java.lang.String is present, otherwise
@@ -288,33 +288,33 @@
Option property = Option property = Option.builder("D")
+ Option property = Option.builder("D")
.hasArgs()
.valueSeparator('=')
- .build();
+ .get();
The map of properties specified by this option can later be retrieved by
calling getOptionProperties("D") on the CommandLine.
@@ -335,11 +335,11 @@
Now that we have created each
- Option we need
+ Option we need
to create the
- Options
+ Options
instance. This is achieved using the
- addOption
+ addOption
method of Options.
Options options = new Options();
@@ -351,10 +351,10 @@ Creating the Options
options.addOption(verbose);
options.addOption(debug);
options.addOption(emacs);
-options.addOption(logfile);
+options.addOption(logFile);
options.addOption(logger);
options.addOption(listener);
-options.addOption(buildfile);
+options.addOption(buildFile);
options.addOption(find);
options.addOption(property);
@@ -367,7 +367,7 @@
We now need to create a CommandLineParser. This will parse the command
line arguments, using the rules specified by the Options and
- return an instance of CommandLine.
+ return an instance of CommandLine.
public static void main(String[] args) {
// create the parser
@@ -399,7 +399,7 @@ Displaying Usage and Help
CLI also provides the means to automatically generate usage
and help information. This is achieved with the
- HelpFormatter
+ HelpFormatter
class.
// automatically generate the help statement
@@ -454,7 +454,7 @@
Options for this example.
+ Options for this example.
// create the command line parser
CommandLineParser parser = new DefaultParser();
@@ -468,7 +468,7 @@ 


+ .get();
The above will create an option that passes the string value to the Foo constructor when commandLine.getParsedOptionValue(fooOpt) is called.
@@ -595,12 +595,12 @@
Changing Usage Announcement
}
System.err.printf("ERROR: Option %s: %s%n", buf, o.getDeprecated());
};
- DefaultParser parser = DefaultParser.builder().setDeprecatedHandler(deprecatedUsageAnnouncement).build();
+ DefaultParser parser = DefaultParser.builder().setDeprecatedHandler(deprecatedUsageAnnouncement).get();
CommandLine line;
try {
// parse the command line arguments
@@ -728,10 +728,10 @@ Changing help format
7. Defining Option Properties
The following are the properties that each
- Option has. All of these
+ Option has. All of these
can be set using the accessors or using the methods
defined in the
- Option.Builder.
+ Option.Builder.
Option Properties
diff --git a/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java b/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
index 4e54fb29f..6b3065664 100644
--- a/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
+++ b/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
@@ -147,6 +147,26 @@ void testPrintHelp() throws IOException {
assertEquals(0, sb.length(), "Should not write to output");
}
+ @Test
+ void testPrintHelpWithDefaults() throws IOException {
+ final StringBuilder sb = new StringBuilder();
+ final TextHelpAppendable serializer = new TextHelpAppendable(sb);
+ HelpFormatter formatter = HelpFormatter.builder().setHelpAppendable(serializer).get();
+
+ final Options options = new Options().addOption(Option.builder("a").since("1853").hasArg().desc("aaaa aaaa aaaa aaaa aaaa").get());
+
+ List expected = new ArrayList<>();
+ expected.add(" usage: commandSyntax");
+ expected.add("");
+ expected.add(" Options Since Description ");
+ expected.add(" -a 1853 aaaa aaaa aaaa aaaa aaaa");
+ expected.add("");
+
+ formatter.printHelp("commandSyntax", options);
+ List actual = IOUtils.readLines(new StringReader(sb.toString()));
+ assertEquals(expected, actual);
+ }
+
/**
* Tests example from the mailing list that caused an infinite loop.
*