@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1919
2020import java .io .BufferedReader ;
2121import java .io .IOException ;
22+ import java .io .PrintStream ;
2223import java .io .PrintWriter ;
2324import java .io .Serializable ;
2425import java .io .StringReader ;
@@ -29,6 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2930import java .util .Comparator ;
3031import java .util .Iterator ;
3132import java .util .List ;
33+ import java .util .Objects ;
3234import 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
0 commit comments