Skip to content

Commit 483f811

Browse files
committed
Indented lines in the header and footer are now preserved (CLI-207)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@966306 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9922274 commit 483f811

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package org.apache.commons.cli;
1919

20+
import java.io.BufferedReader;
21+
import java.io.IOException;
2022
import java.io.PrintWriter;
23+
import java.io.StringReader;
2124
import java.util.ArrayList;
2225
import java.util.Arrays;
2326
import java.util.Collection;
@@ -724,7 +727,7 @@ public void printWrapped(PrintWriter pw, int width, int nextLineTabStop, String
724727
{
725728
StringBuffer sb = new StringBuffer(text.length());
726729

727-
renderWrappedText(sb, width, nextLineTabStop, text);
730+
renderWrappedTextBlock(sb, width, nextLineTabStop, text);
728731
pw.println(sb.toString());
729732
}
730733

@@ -885,6 +888,35 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
885888
}
886889
}
887890

891+
/**
892+
* Render the specified text width a maximum width. This method differs
893+
* from renderWrappedText by not removing leading spaces after a new line.
894+
*
895+
* @param sb The StringBuffer to place the rendered text into.
896+
* @param width The number of characters to display per line
897+
* @param nextLineTabStop The position on the next line for the first tab.
898+
* @param text The text to be rendered.
899+
*/
900+
private StringBuffer renderWrappedTextBlock(StringBuffer sb, int width, int nextLineTabStop, String text) {
901+
try {
902+
BufferedReader in = new BufferedReader(new StringReader(text));
903+
String line;
904+
boolean firstLine = true;
905+
while ((line = in.readLine()) != null) {
906+
if (!firstLine) {
907+
sb.append(getNewLine());
908+
} else {
909+
firstLine = false;
910+
}
911+
renderWrappedText(sb, width, nextLineTabStop, line);
912+
}
913+
} catch (IOException e) {
914+
// cannot happen
915+
}
916+
917+
return sb;
918+
}
919+
888920
/**
889921
* Finds the next text wrap position after <code>startPos</code> for the
890922
* text in <code>text</code> with the column width <code>width</code>.

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,26 @@ public void testHeaderStartingWithLineSeparator()
428428
, out.toString());
429429
}
430430

431+
public void testIndentedHeaderAndFooter()
432+
{
433+
// related to CLI-207
434+
Options options = new Options();
435+
HelpFormatter formatter = new HelpFormatter();
436+
String header = " Header1\n Header2";
437+
String footer = " Footer1\n Footer2";
438+
StringWriter out = new StringWriter();
439+
formatter.printHelp(new PrintWriter(out), 80, "foobar", header, options, 2, 2, footer, true);
440+
441+
assertEquals(
442+
"usage: foobar" + EOL +
443+
" Header1" + EOL +
444+
" Header2" + EOL +
445+
"" + EOL +
446+
" Footer1" + EOL +
447+
" Footer2" + EOL
448+
, out.toString());
449+
}
450+
431451
public void testOptionWithoutShortFormat()
432452
{
433453
// related to Bugzilla #19383 (CLI-67)

0 commit comments

Comments
 (0)