Skip to content

Commit b6ce51b

Browse files
committed
Internal refactoring
Special case for using System.out as the default
1 parent e6c96bf commit b6ce51b

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

src/conf/spotbugs-exclude-filter.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
<Field name="defaultWidth" />
8585
<Bug pattern="PA_PUBLIC_PRIMITIVE_ATTRIBUTE" />
8686
</Match>
87+
<Match>
88+
<!-- Special case for using System.out as the default. -->
89+
<Class name="org.apache.commons.cli.HelpFormatter" />
90+
<Method name="createDefaultPrintWriter" />
91+
<Bug pattern="DM_DEFAULT_ENCODING" />
92+
</Match>
8793

8894
<Match>
8995
<!-- TODO ? -->

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static final class Builder implements Supplier<HelpFormatter> {
8282
/**
8383
* The output PrintWriter, defaults to wrapping {@link System#out}.
8484
*/
85-
private PrintWriter printStream = new PrintWriter(System.out);
85+
private PrintWriter printStream = createDefaultPrintWriter();
8686

8787
@Override
8888
public HelpFormatter get() {
@@ -174,6 +174,10 @@ public static Builder builder() {
174174
return new Builder();
175175
}
176176

177+
private static PrintWriter createDefaultPrintWriter() {
178+
return new PrintWriter(System.out);
179+
}
180+
177181
/**
178182
* Number of characters per line
179183
*
@@ -264,7 +268,7 @@ public static Builder builder() {
264268
* Constructs a new instance.
265269
*/
266270
public HelpFormatter() {
267-
this(false, new PrintWriter(System.out));
271+
this(false, createDefaultPrintWriter());
268272
}
269273

270274
/**
@@ -546,7 +550,6 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
546550
* @param descPad the number of characters of padding to be prefixed to each description line
547551
* @param footer the banner to display at the end of the help
548552
* @param autoUsage whether to print an automatically generated usage statement
549-
*
550553
* @throws IllegalStateException if there is no room to print a line
551554
*/
552555
public void printHelp(final PrintWriter pw, final int width, final String cmdLineSyntax, final String header, final Options options, final int leftPad,
@@ -863,7 +866,6 @@ private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width
863866
* Removes the trailing whitespace from the specified String.
864867
*
865868
* @param s The String to remove the trailing padding from.
866-
*
867869
* @return The String of without the trailing padding
868870
*/
869871
protected String rtrim(final String s) {

0 commit comments

Comments
 (0)