Skip to content

Commit 2ddaae1

Browse files
committed
Applying my second attempt at a patch to CLI-162. This fixes Gary's reported bug (one of which was an example of CLI-162, and one a bug in my first attempt to patch). Open question is whether to output text that is too long, or try and break it up to fit the screen width.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@745388 13f79535-47bb-0310-9956-ffa450edef68
1 parent 88cf9e4 commit 2ddaae1

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

src/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,6 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
821821

822822
while (true)
823823
{
824-
int lastPos = pos;
825824
text = padding + text.substring(pos).trim();
826825
pos = findWrapPos(text, width, 0);
827826

@@ -830,10 +829,12 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
830829
sb.append(text);
831830

832831
return sb;
833-
} else
834-
if (pos == lastPos)
835-
{
836-
throw new RuntimeException("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: " + text);
832+
}
833+
834+
if ( (text.length() > width) && (pos == nextLineTabStop - 1) ) {
835+
sb.append(text);
836+
837+
return sb;
837838
}
838839

839840
sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);

src/test/org/apache/commons/cli/bug/BugCLI162Test.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public void setUp() {
4242
public void testInfiniteLoop() {
4343
HelpFormatter formatter = new HelpFormatter();
4444
formatter.setWidth(20);
45-
try {
46-
formatter.printHelp("app", options); // hang & crash
47-
} catch(RuntimeException re) {
48-
assertTrue(re.getMessage().startsWith("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: "));
49-
}
45+
formatter.printHelp("app", options); // used to hang & crash
5046
}
5147

5248
private void testPrintHelp(Options options) throws ParseException, IOException {

0 commit comments

Comments
 (0)