This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git
The following commit(s) were added to refs/heads/master by this push:
new 0f561e59 [CLI-354] Fix HelpFormatter wrapped description indent (#439)
0f561e59 is described below
commit 0f561e5979d1f10b93ae98ef1fcd908ec33c048e
Author: dev_Hakaze <[email protected]>
AuthorDate: Sat Aug 22 21:18:51 2026 +0700
[CLI-354] Fix HelpFormatter wrapped description indent (#439)
When remaining pad equals indent, continuation lines dropped the leading
indent and padded the right instead, so some wrapped description lines sat
one space left of the others. Apply indent when restLen >= indent.
Co-authored-by: arimu1 <[email protected]>
---
.../org/apache/commons/cli/help/TextStyle.java | 2 +-
.../apache/commons/cli/help/HelpFormatterTest.java | 33 ++++++++++++++++++++++
.../org/apache/commons/cli/help/TextStyleTest.java | 11 ++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/cli/help/TextStyle.java
b/src/main/java/org/apache/commons/cli/help/TextStyle.java
index fef720f3..02346daa 100644
--- a/src/main/java/org/apache/commons/cli/help/TextStyle.java
+++ b/src/main/java/org/apache/commons/cli/help/TextStyle.java
@@ -369,7 +369,7 @@ public final class TextStyle {
rest = "";
} else {
int restLen = maxWidth - text.length();
- if (addIndent && restLen > indent) {
+ if (addIndent && restLen >= indent) {
indentPad = Util.repeatSpace(indent);
restLen -= indent;
} else {
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 04041f64..e7b995bc 100644
--- a/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
+++ b/src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
@@ -340,6 +340,39 @@ class HelpFormatterTest {
assertEquals(expected, actual);
}
+ /**
+ * Continuation description lines must share the same indent.
+ *
+ * @see <a href="https://issues.apache.org/jira/browse/CLI-354">[CLI-354]
HelpFormatter: Description indentation is incorrect</a>
+ */
+ @Test
+ void testPrintHelpWrappedDescriptionIndent() throws IOException {
+ final StringBuilder sb = new StringBuilder();
+ final TextHelpAppendable serializer = new TextHelpAppendable(sb);
+ final HelpFormatter formatter =
HelpFormatter.builder().setHelpAppendable(serializer).setShowSince(false).get();
+ final String description = "an argument passed to the remote command.
The value will be wrapped in double quotes "
+ + "and appended to the command-line. This option can be added
multiple times.";
+ final Options options = new
Options().addOption(Option.builder("V").longOpt("argument-value").hasArg().desc(description).get());
+
+ final List<String> expected = new ArrayList<>();
+ expected.add(" usage: cs [-V <arg>]");
+ expected.add("");
+ expected.add(" header");
+ expected.add("");
+ expected.add(" Options
Description ");
+ expected.add(" -V, --argument-value <arg> an argument passed to
the remote command. ");
+ expected.add(" The value will be
wrapped in double quotes");
+ expected.add(" and appended to the
command-line. This ");
+ expected.add(" option can be added
multiple times. ");
+ expected.add("");
+ expected.add(" footer");
+ expected.add("");
+
+ formatter.printHelp("cs", "header", options, "footer", true);
+ final List<String> actual = IOUtils.readLines(new
StringReader(sb.toString()));
+ assertEquals(expected, actual);
+ }
+
@Test
void testSetOptionFormatBuilderTest() {
final HelpFormatter.Builder underTest = HelpFormatter.builder();
diff --git a/src/test/java/org/apache/commons/cli/help/TextStyleTest.java
b/src/test/java/org/apache/commons/cli/help/TextStyleTest.java
index b3e94627..2f18ae3b 100644
--- a/src/test/java/org/apache/commons/cli/help/TextStyleTest.java
+++ b/src/test/java/org/apache/commons/cli/help/TextStyleTest.java
@@ -71,6 +71,17 @@ class TextStyleTest {
builder.setAlignment(TextStyle.Alignment.CENTER);
lst.add(Arguments.of(builder.get(), " Hello world ", " Hello
world "));
+ // width equal to text length + indent applies indent on continuation
lines
+ builder.setMaxWidth(16);
+ builder.setAlignment(TextStyle.Alignment.LEFT);
+ lst.add(Arguments.of(builder.get(), "Hello world ", " Hello
world"));
+
+ builder.setAlignment(TextStyle.Alignment.RIGHT);
+ lst.add(Arguments.of(builder.get(), " Hello world", " Hello
world"));
+
+ builder.setAlignment(TextStyle.Alignment.CENTER);
+ lst.add(Arguments.of(builder.get(), " Hello world ", " Hello world
"));
+
// width greater than text length and less than text length + indent
creates result of text length + pad
builder.setMaxWidth(14);
builder.setAlignment(TextStyle.Alignment.LEFT);