Skip to content

Commit 82403d0

Browse files
authored
[CLI-287] Allow whitespace-only header and footer (apache#26)
* [CLI-287] Allow whitespace-only header and footer * [CLI-287] Add tests
1 parent 8dcf119 commit 82403d0

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,13 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
482482
printUsage(pw, width, cmdLineSyntax);
483483
}
484484

485-
if (header != null && !header.trim().isEmpty()) {
485+
if (header != null && !header.isEmpty()) {
486486
printWrapped(pw, width, header);
487487
}
488488

489489
printOptions(pw, width, options, leftPad, descPad);
490490

491-
if (footer != null && !footer.trim().isEmpty()) {
491+
if (footer != null && !footer.isEmpty()) {
492492
printWrapped(pw, width, footer);
493493
}
494494
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,62 @@ public void testOptionWithoutShortFormat2() {
293293
//@formatter:on
294294
}
295295

296+
@Test
297+
public void testPrintHelpNewlineFooter()
298+
{
299+
final HelpFormatter formatter = new HelpFormatter();
300+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
301+
final PrintWriter pw = new PrintWriter(out);
302+
303+
final Options options = new Options();
304+
options.addOption("a", "b");
305+
306+
formatter.printHelp(
307+
pw,
308+
80,
309+
"test" + EOL,
310+
"header" + EOL,
311+
options,
312+
0,
313+
0,
314+
EOL
315+
);
316+
final String expected = "usage: test" + EOL +
317+
"header" + EOL +
318+
"-ab" + EOL +
319+
EOL;
320+
pw.flush();
321+
assertEquals("footer newline", expected, out.toString());
322+
}
323+
324+
@Test
325+
public void testPrintHelpNewlineHeader()
326+
{
327+
final HelpFormatter formatter = new HelpFormatter();
328+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
329+
final PrintWriter pw = new PrintWriter(out);
330+
331+
final Options options = new Options();
332+
options.addOption("a", "b");
333+
334+
formatter.printHelp(
335+
pw,
336+
80,
337+
"test" + EOL,
338+
EOL,
339+
options,
340+
0,
341+
0,
342+
"footer" + EOL
343+
);
344+
String expected = "usage: test" + EOL +
345+
EOL +
346+
"-ab" + EOL +
347+
"footer" + EOL;
348+
pw.flush();
349+
assertEquals("header newline", expected, out.toString());
350+
}
351+
296352
@Test
297353
public void testPrintHelpWithEmptySyntax() {
298354
final HelpFormatter formatter = new HelpFormatter();

0 commit comments

Comments
 (0)