Author: bayard Date: Wed Feb 18 05:42:58 2009 New Revision: 745388 URL: http://svn.apache.org/viewvc?rev=745388&view=rev Log: 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.
Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/bug/BugCLI162Test.java Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java?rev=745388&r1=745387&r2=745388&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java (original) +++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java Wed Feb 18 05:42:58 2009 @@ -821,7 +821,6 @@ while (true) { - int lastPos = pos; text = padding + text.substring(pos).trim(); pos = findWrapPos(text, width, 0); @@ -830,10 +829,12 @@ sb.append(text); return sb; - } else - if (pos == lastPos) - { - throw new RuntimeException("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: " + text); + } + + if ( (text.length() > width) && (pos == nextLineTabStop - 1) ) { + sb.append(text); + + return sb; } sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine); Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/bug/BugCLI162Test.java URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/bug/BugCLI162Test.java?rev=745388&r1=745387&r2=745388&view=diff ============================================================================== --- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/bug/BugCLI162Test.java (original) +++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/bug/BugCLI162Test.java Wed Feb 18 05:42:58 2009 @@ -42,11 +42,7 @@ public void testInfiniteLoop() { HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(20); - try { - formatter.printHelp("app", options); // hang & crash - } catch(RuntimeException re) { - assertTrue(re.getMessage().startsWith("Text too long for line - throwing exception to avoid infinite loop [CLI-162]: ")); - } + formatter.printHelp("app", options); // used to hang & crash } private void testPrintHelp(Options options) throws ParseException, IOException {