Skip to content

Commit 648c03a

Browse files
author
Robert James Oxspring
committed
Long descriptions with newlines are now correctly formatted
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@161579 13f79535-47bb-0310-9956-ffa450edef68
1 parent c7391b9 commit 648c03a

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/java/org/apache/commons/cli2/util/HelpFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ protected static List wrap(final String text, final int width) {
362362

363363
while (left < chars.length) {
364364
int right = left;
365-
while (right < chars.length && chars[right] != '\n') {
365+
while (right < chars.length && chars[right] != '\n' && right<left+width+1) {
366366
right++;
367367
}
368-
if (right < chars.length) {
368+
if (right<chars.length && chars[right] == '\n') {
369369
final String line = new String(chars, left, right - left);
370370
lines.add(line);
371371
left = right + 1;

src/test/org/apache/commons/cli2/util/HelpFormatterTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.StringReader;
2222
import java.io.StringWriter;
2323
import java.util.Iterator;
24+
import java.util.List;
2425

2526
import junit.framework.TestCase;
2627

@@ -345,6 +346,23 @@ public void testWrap_NewLine() {
345346
assertEquals("", i.next());
346347
assertFalse(i.hasNext());
347348
}
349+
350+
public void testWrap_NewLine2() {
351+
List wrapped = HelpFormatter.wrap("A really quite long general description of the option with specific alternatives documented:\n"+
352+
" Indented special case\n"+
353+
" Alternative scenario", 30);
354+
355+
final Iterator i =
356+
wrapped.iterator();
357+
358+
assertEquals("A really quite long general", i.next());
359+
assertEquals("description of the option", i.next());
360+
assertEquals("with specific alternatives", i.next());
361+
assertEquals("documented:", i.next());
362+
assertEquals(" Indented special case", i.next());
363+
assertEquals(" Alternative scenario", i.next());
364+
assertFalse(i.hasNext());
365+
}
348366

349367
public void testWrap_Below1Length() {
350368
try{

0 commit comments

Comments
 (0)