Skip to content

Commit 84234a6

Browse files
committed
Applying additional patch to throw IllegalStateException when the specified width is not enough to fit the flags, indent and 1 character for the description. This closes out CLI-162 (for now :) ).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@746137 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4141904 commit 84234a6

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,13 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
819819
// characters
820820
final String padding = createPadding(nextLineTabStop);
821821

822+
if (nextLineTabStop >= width)
823+
{
824+
// stops infinite loop happening
825+
throw new IllegalStateException("Total width is less than the width of the argument and indent " +
826+
"- no room for the description");
827+
}
828+
822829
while (true)
823830
{
824831
text = padding + text.substring(pos).trim();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ public void testLongLineChunking() throws ParseException, IOException {
259259
" yes.\n" +
260260
"Footer\n";
261261
assertEquals( "Long arguments did not split as expected", expected, sw.toString() );
262+
263+
try {
264+
formatter.printHelp(new PrintWriter(sw), 22, this.getClass().getName(), "Header", options, 0, 5, "Footer");
265+
fail("IllegalStateException expected");
266+
} catch(IllegalStateException ise) {
267+
// expected
268+
}
269+
262270
}
263271

264272
}

0 commit comments

Comments
 (0)