Skip to content

Commit 1fb2e67

Browse files
committed
Set HelpFormatter print stream
Avoid System.setOut() hack
1 parent 9ff24a0 commit 1fb2e67

3 files changed

Lines changed: 54 additions & 26 deletions

File tree

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1919

2020
import java.io.BufferedReader;
2121
import java.io.IOException;
22+
import java.io.PrintStream;
2223
import java.io.PrintWriter;
2324
import java.io.Serializable;
2425
import java.io.StringReader;
@@ -29,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2930
import java.util.Comparator;
3031
import java.util.Iterator;
3132
import java.util.List;
33+
import java.util.Objects;
3234
import java.util.function.Supplier;
3335

3436
/**
@@ -78,9 +80,25 @@ public static final class Builder implements Supplier<HelpFormatter> {
7880
*/
7981
private boolean showDeprecated;
8082

83+
/**
84+
* The output PrintStream, defaults to {@link System#out}.
85+
*/
86+
private PrintStream printStream = System.out;
87+
8188
@Override
8289
public HelpFormatter get() {
83-
return new HelpFormatter(showDeprecated);
90+
return new HelpFormatter(showDeprecated, printStream);
91+
}
92+
93+
/**
94+
* Sets the output PrintStream, defaults to {@link System#out}.
95+
*
96+
* @param printStream the output PrintStream, not null.
97+
* @return this.
98+
*/
99+
public Builder setPrintStream(final PrintStream printStream) {
100+
this.printStream = Objects.requireNonNull(printStream, "printStream");
101+
return this;
84102
}
85103

86104
/**
@@ -100,6 +118,7 @@ public Builder setShowDeprecated(final boolean showDeprecated) {
100118
* This class implements the {@code Comparator} interface for comparing Options.
101119
*/
102120
private static final class OptionComparator implements Comparator<Option>, Serializable {
121+
103122
/** The serial version UID. */
104123
private static final long serialVersionUID = 5305467873966684014L;
105124

@@ -232,6 +251,11 @@ public static Builder builder() {
232251
*/
233252
private final boolean showDeprecated;
234253

254+
/**
255+
* Where to print help.
256+
*/
257+
private final PrintStream printStream;
258+
235259
/**
236260
* The separator displayed between the long option and its value.
237261
*/
@@ -241,16 +265,18 @@ public static Builder builder() {
241265
* Constructs a new instance.
242266
*/
243267
public HelpFormatter() {
244-
this.showDeprecated = false;
268+
this(false, System.out);
245269
}
246270

247271
/**
248272
* Constructs a new instance.
273+
* @param printStream TODO
249274
*/
250-
private HelpFormatter(final boolean showDeprecated) {
275+
private HelpFormatter(final boolean showDeprecated, final PrintStream printStream) {
251276
// TODO All other instance HelpFormatter instance variables.
252277
// Make HelpFormatter immutable for 2.0
253278
this.showDeprecated = showDeprecated;
279+
this.printStream = printStream;
254280
}
255281

256282
/**
@@ -460,7 +486,7 @@ public int getWidth() {
460486

461487
/**
462488
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
463-
* to System.out.
489+
* to {@link System#out} by default.
464490
*
465491
* @param width the number of characters to be displayed on each line
466492
* @param cmdLineSyntax the syntax for this application
@@ -474,7 +500,7 @@ public void printHelp(final int width, final String cmdLineSyntax, final String
474500

475501
/**
476502
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
477-
* to System.out.
503+
* to {@link System#out} by default.
478504
*
479505
* @param width the number of characters to be displayed on each line
480506
* @param cmdLineSyntax the syntax for this application
@@ -485,7 +511,7 @@ public void printHelp(final int width, final String cmdLineSyntax, final String
485511
*/
486512
public void printHelp(final int width, final String cmdLineSyntax, final String header, final Options options, final String footer,
487513
final boolean autoUsage) {
488-
final PrintWriter pw = new PrintWriter(System.out);
514+
final PrintWriter pw = new PrintWriter(printStream);
489515
printHelp(pw, width, cmdLineSyntax, header, options, getLeftPadding(), getDescPadding(), footer, autoUsage);
490516
pw.flush();
491517
}
@@ -545,7 +571,7 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
545571

546572
/**
547573
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
548-
* to System.out.
574+
* to {@link System#out} by default.
549575
*
550576
* @param cmdLineSyntax the syntax for this application
551577
* @param options the Options instance
@@ -556,7 +582,7 @@ public void printHelp(final String cmdLineSyntax, final Options options) {
556582

557583
/**
558584
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
559-
* to System.out.
585+
* to {@link System#out} by default.
560586
*
561587
* @param cmdLineSyntax the syntax for this application
562588
* @param options the Options instance
@@ -568,7 +594,7 @@ public void printHelp(final String cmdLineSyntax, final Options options, final b
568594

569595
/**
570596
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
571-
* to System.out.
597+
* to {@link System#out} by default.
572598
*
573599
* @param cmdLineSyntax the syntax for this application
574600
* @param header the banner to display at the beginning of the help
@@ -581,7 +607,7 @@ public void printHelp(final String cmdLineSyntax, final String header, final Opt
581607

582608
/**
583609
* Prints the help for {@code options} with the specified command line syntax. This method prints help information
584-
* to System.out.
610+
* to {@link System#out} by default.
585611
*
586612
* @param cmdLineSyntax the syntax for this application
587613
* @param header the banner to display at the beginning of the help

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
23-
import static org.junit.jupiter.api.Assertions.fail;
2423

2524
import java.io.ByteArrayOutputStream;
2625
import java.io.PrintWriter;

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,30 +231,33 @@ public void test13425() throws Exception {
231231
@Test
232232
public void test13666() throws Exception {
233233
final Options options = new Options();
234-
final Option dir = OptionBuilder.withDescription("dir").hasArg().create('d');
235-
options.addOption(dir);
236-
234+
final Option dirOption = OptionBuilder.withDescription("dir").hasArg().create('d');
235+
options.addOption(dirOption);
237236
final PrintStream oldSystemOut = System.out;
238237
try {
239-
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
240-
final PrintStream print = new PrintStream(bytes);
241-
242-
// capture this platform's eol symbol
243-
print.println();
244-
final String eol = bytes.toString();
245-
bytes.reset();
246-
247-
System.setOut(new PrintStream(bytes));
248-
238+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
239+
final String eol = System.lineSeparator();
240+
System.setOut(new PrintStream(baos));
249241
final HelpFormatter formatter = new HelpFormatter();
250242
formatter.printHelp("dir", options);
251-
252-
assertEquals("usage: dir" + eol + " -d <arg> dir" + eol, bytes.toString());
243+
assertEquals("usage: dir" + eol + " -d <arg> dir" + eol, baos.toString());
253244
} finally {
254245
System.setOut(oldSystemOut);
255246
}
256247
}
257248

249+
@Test
250+
public void test13666_Builder() throws Exception {
251+
final Options options = new Options();
252+
final Option dirOption = OptionBuilder.withDescription("dir").hasArg().create('d');
253+
options.addOption(dirOption);
254+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
255+
final String eol = System.lineSeparator();
256+
final HelpFormatter formatter = HelpFormatter.builder().setPrintStream(new PrintStream(baos)).get();
257+
formatter.printHelp("dir", options);
258+
assertEquals("usage: dir" + eol + " -d <arg> dir" + eol, baos.toString());
259+
}
260+
258261
@Test
259262
public void test13935() throws Exception {
260263
final OptionGroup directions = new OptionGroup();

0 commit comments

Comments
 (0)