You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2010/07/21 18:40:40 UTC
svn commit: r966306 - in /commons/proper/cli/trunk/src:
main/java/org/apache/commons/cli/HelpFormatter.java
test/java/org/apache/commons/cli/HelpFormatterTest.java
Author: ebourg
Date: Wed Jul 21 16:40:40 2010
New Revision: 966306
URL: http://svn.apache.org/viewvc?rev=966306&view=rev
Log:
Indented lines in the header and footer are now preserved (CLI-207)
Modified:
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java?rev=966306&r1=966305&r2=966306&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java Wed Jul 21 16:40:40 2010
@@ -17,7 +17,10 @@
package org.apache.commons.cli;
+import java.io.BufferedReader;
+import java.io.IOException;
import java.io.PrintWriter;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -724,7 +727,7 @@ public class HelpFormatter
{
StringBuffer sb = new StringBuffer(text.length());
- renderWrappedText(sb, width, nextLineTabStop, text);
+ renderWrappedTextBlock(sb, width, nextLineTabStop, text);
pw.println(sb.toString());
}
@@ -886,6 +889,35 @@ public class HelpFormatter
}
/**
+ * Render the specified text width a maximum width. This method differs
+ * from renderWrappedText by not removing leading spaces after a new line.
+ *
+ * @param sb The StringBuffer to place the rendered text into.
+ * @param width The number of characters to display per line
+ * @param nextLineTabStop The position on the next line for the first tab.
+ * @param text The text to be rendered.
+ */
+ private StringBuffer renderWrappedTextBlock(StringBuffer sb, int width, int nextLineTabStop, String text) {
+ try {
+ BufferedReader in = new BufferedReader(new StringReader(text));
+ String line;
+ boolean firstLine = true;
+ while ((line = in.readLine()) != null) {
+ if (!firstLine) {
+ sb.append(getNewLine());
+ } else {
+ firstLine = false;
+ }
+ renderWrappedText(sb, width, nextLineTabStop, line);
+ }
+ } catch (IOException e) {
+ // cannot happen
+ }
+
+ return sb;
+ }
+
+ /**
* Finds the next text wrap position after <code>startPos</code> for the
* text in <code>text</code> with the column width <code>width</code>.
* The wrap point is the last position before startPos+width having a
Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java?rev=966306&r1=966305&r2=966306&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java Wed Jul 21 16:40:40 2010
@@ -428,6 +428,26 @@ public class HelpFormatterTest extends T
, out.toString());
}
+ public void testIndentedHeaderAndFooter()
+ {
+ // related to CLI-207
+ Options options = new Options();
+ HelpFormatter formatter = new HelpFormatter();
+ String header = " Header1\n Header2";
+ String footer = " Footer1\n Footer2";
+ StringWriter out = new StringWriter();
+ formatter.printHelp(new PrintWriter(out), 80, "foobar", header, options, 2, 2, footer, true);
+
+ assertEquals(
+ "usage: foobar" + EOL +
+ " Header1" + EOL +
+ " Header2" + EOL +
+ "" + EOL +
+ " Footer1" + EOL +
+ " Footer2" + EOL
+ , out.toString());
+ }
+
public void testOptionWithoutShortFormat()
{
// related to Bugzilla #19383 (CLI-67)