Skip to content

Commit 5d2c0dc

Browse files
committed
Refactor duplicate code patterns
- Add org.apache.commons.cli.help.HelpAppendable.appendFormat(String, Object...) - Add org.apache.commons.cli.help.HelpAppendable.appendParagraphFormat(String, Object...)
1 parent e44db7a commit 5d2c0dc

8 files changed

Lines changed: 138 additions & 41 deletions

File tree

src/main/java/org/apache/commons/cli/help/AbstractHelpFormatter.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public abstract static class Builder<B extends Builder<B, T>, T extends Abstract
5353
/** The comparator to sort lists of options */
5454
private Comparator<Option> comparator = DEFAULT_COMPARATOR;
5555

56-
/** The {@link HelpAppendable} to use */
56+
/** The {@link HelpAppendable}. */
5757
private HelpAppendable helpAppendable = TextHelpAppendable.systemOut();
5858

5959
/** The {@link OptionFormatter.Builder} to use to format options in the table. */
6060
private OptionFormatter.Builder optionFormatBuilder = OptionFormatter.builder();
6161

62-
/** The string to separate option groups with */
62+
/** The string to separate option groups. */
6363
private String optionGroupSeparator = DEFAULT_OPTION_GROUP_SEPARATOR;
6464

6565
/**
@@ -82,18 +82,38 @@ protected B asThis() {
8282
return (B) this;
8383
}
8484

85+
/**
86+
* Gets the comparator to sort lists of options.
87+
*
88+
* @return the comparator to sort lists of options.
89+
*/
8590
protected Comparator<Option> getComparator() {
8691
return comparator;
8792
}
8893

94+
/**
95+
* Gets {@link HelpAppendable}.
96+
*
97+
* @return the {@link HelpAppendable}.
98+
*/
8999
protected HelpAppendable getHelpAppendable() {
90100
return helpAppendable;
91101
}
92102

103+
/**
104+
* Gets {@link OptionFormatter.Builder} to use to format options in the table.
105+
*
106+
* @return the {@link OptionFormatter.Builder} to use to format options in the table.
107+
*/
93108
protected OptionFormatter.Builder getOptionFormatBuilder() {
94109
return optionFormatBuilder;
95110
}
96111

112+
/**
113+
* Gets string to separate option groups.
114+
*
115+
* @return the string to separate option groups.
116+
*/
97117
protected String getOptionGroupSeparator() {
98118
return optionGroupSeparator;
99119
}
@@ -253,9 +273,9 @@ public void printHelp(final String cmdLineSyntax, final String header, final Ite
253273
throw new IllegalArgumentException("cmdLineSyntax not provided");
254274
}
255275
if (autoUsage) {
256-
helpAppendable.appendParagraph(String.format("%s %s %s", syntaxPrefix, cmdLineSyntax, toSyntaxOptions(options)));
276+
helpAppendable.appendParagraphFormat("%s %s %s", syntaxPrefix, cmdLineSyntax, toSyntaxOptions(options));
257277
} else {
258-
helpAppendable.appendParagraph(String.format("%s %s", syntaxPrefix, cmdLineSyntax));
278+
helpAppendable.appendParagraphFormat("%s %s", syntaxPrefix, cmdLineSyntax);
259279
}
260280
if (!Util.isEmpty(header)) {
261281
helpAppendable.appendParagraph(header);

src/main/java/org/apache/commons/cli/help/HelpAppendable.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1818

1919
import java.io.IOException;
2020
import java.util.Collection;
21+
import java.util.Formatter;
2122

2223
/**
2324
* Defines a semantic scribe. The semantic scribe appends text to an output based on the semantic meaning of the type of string. For example, a Paragraph versus
@@ -34,12 +35,33 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3435
*/
3536
public interface HelpAppendable extends Appendable {
3637

38+
/**
39+
* Appends a formatted string using the specified format string and arguments.
40+
* <p>
41+
* Short-hand for:
42+
* </p>
43+
*
44+
* <pre>
45+
* append(String.format(format, args));
46+
* </pre>
47+
*
48+
* @param format The format string for {@link String#format(String, Object...)}.
49+
* @param args Arguments to {@link String#format(String, Object...)}.
50+
* @throws IOException If an output error occurs
51+
* @see String#format(String, Object...)
52+
* @see Formatter
53+
* @see #append(CharSequence)
54+
*/
55+
default void appendFormat(final String format, final Object... args) throws IOException {
56+
append(String.format(format, args));
57+
}
58+
3759
/**
3860
* Appends a header.
3961
*
4062
* @param level the level of the header. This is equivalent to the "1", "2", or "3" in the HTML "h1", "h2", "h3" tags.
4163
* @param text the text for the header
42-
* @throws IOException on write failure
64+
* @throws IOException If an output error occurs
4365
*/
4466
void appendHeader(int level, CharSequence text) throws IOException;
4567

@@ -48,31 +70,46 @@ public interface HelpAppendable extends Appendable {
4870
*
4971
* @param ordered {@code true} if the list should be ordered.
5072
* @param list the list to write.
51-
* @throws IOException on write failure
73+
* @throws IOException If an output error occurs
5274
*/
5375
void appendList(boolean ordered, Collection<CharSequence> list) throws IOException;
5476

5577
/**
5678
* Appends a paragraph.
5779
*
5880
* @param paragraph the paragraph to write.
59-
* @throws IOException on write failure
81+
* @throws IOException If an output error occurs
6082
*/
6183
void appendParagraph(CharSequence paragraph) throws IOException;
6284

85+
/**
86+
* Appends a formatted string as a paragraph.
87+
*
88+
* @param format The format string for {@link String#format(String, Object...)}.
89+
* @param args Arguments to {@link String#format(String, Object...)}.
90+
* @throws IOException If an output error occurs
91+
* @see String#format(String, Object...)
92+
* @see Formatter
93+
* @see #append(CharSequence)
94+
*/
95+
default void appendParagraphFormat(final String format, final Object... args) throws IOException {
96+
appendParagraph(String.format(format, args));
97+
}
98+
6399
/**
64100
* Appends a table.
65101
*
66102
* @param table the table definition to write.
67-
* @throws IOException on write failure
103+
* @throws IOException If an output error occurs
68104
*/
69105
void appendTable(TableDefinition table) throws IOException;
70106

71107
/**
72108
* Appends a title.
73109
*
74110
* @param title the title to write.
75-
* @throws IOException on write failure
111+
* @throws IOException If an output error occurs
76112
*/
77113
void appendTitle(CharSequence title) throws IOException;
114+
78115
}

src/main/java/org/apache/commons/cli/help/TextHelpAppendable.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public static int indexOfWrap(final CharSequence text, final int width, final in
100100
return pos > startPos ? pos : limit - 1;
101101
}
102102

103+
/**
104+
* Creates a new TextHelpAppendable on {@link System#out}.
105+
*
106+
* @return a new TextHelpAppendable on {@link System#out}.
107+
*/
103108
protected static TextHelpAppendable systemOut() {
104109
return new TextHelpAppendable(System.out);
105110
}
@@ -345,7 +350,7 @@ protected List<Queue<String>> makeColumnQueues(final List<String> columnData, fi
345350
*/
346351
private void printQueue(final Queue<String> queue) throws IOException {
347352
for (final String s : queue) {
348-
output.append(String.format("%s%n", Util.rtrim(s)));
353+
appendFormat("%s%n", Util.rtrim(s));
349354
}
350355
}
351356

src/test/java/org/apache/commons/cli/example/AptHelpAppendable.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void appendHeader(final int level, final CharSequence text) throws IOExce
6767
for (int i = 0; i < level; i++) {
6868
output.append("*");
6969
}
70-
output.append(String.format(" %s%n%n", ESCAPE_APT.translate(text)));
70+
appendFormat(" %s%n%n", ESCAPE_APT.translate(text));
7171
}
7272
}
7373

@@ -77,11 +77,11 @@ public void appendList(final boolean ordered, final Collection<CharSequence> lis
7777
if (ordered) {
7878
int idx = 1;
7979
for (final CharSequence s : list) {
80-
output.append(String.format(" [[%s]] %s%n", idx++, ESCAPE_APT.translate(s)));
80+
appendFormat(" [[%s]] %s%n", idx++, ESCAPE_APT.translate(s));
8181
}
8282
} else {
8383
for (final CharSequence s : list) {
84-
output.append(String.format(" * %s%n", ESCAPE_APT.translate(s)));
84+
appendFormat(" * %s%n", ESCAPE_APT.translate(s));
8585
}
8686
}
8787
output.append(System.lineSeparator());
@@ -91,7 +91,7 @@ public void appendList(final boolean ordered, final Collection<CharSequence> lis
9191
@Override
9292
public void appendParagraph(final CharSequence paragraph) throws IOException {
9393
if (StringUtils.isNotEmpty(paragraph)) {
94-
output.append(String.format(" %s%n%n", ESCAPE_APT.translate(paragraph)));
94+
appendFormat(" %s%n%n", ESCAPE_APT.translate(paragraph));
9595
}
9696
}
9797

@@ -121,20 +121,20 @@ public void appendTable(final TableDefinition table) throws IOException {
121121
output.append(sb.toString());
122122
output.append("|");
123123
for (final String header : table.headers()) {
124-
output.append(String.format(" %s |", ESCAPE_APT.translate(header)));
124+
appendFormat(" %s |", ESCAPE_APT.translate(header));
125125
}
126126
output.append(rowSeparator);
127127
// write the table entries
128128
for (final Collection<String> row : table.rows()) {
129129
output.append("|");
130130
for (final String cell : row) {
131-
output.append(String.format(" %s |", ESCAPE_APT.translate(cell)));
131+
appendFormat(" %s |", ESCAPE_APT.translate(cell));
132132
}
133133
output.append(rowSeparator);
134134
}
135135
// write the caption
136136
if (StringUtils.isNotEmpty(table.caption())) {
137-
output.append(String.format("%s%n", ESCAPE_APT.translate(table.caption())));
137+
appendFormat("%s%n", ESCAPE_APT.translate(table.caption()));
138138
}
139139
output.append(System.lineSeparator());
140140
}
@@ -143,7 +143,7 @@ public void appendTable(final TableDefinition table) throws IOException {
143143
@Override
144144
public void appendTitle(final CharSequence title) throws IOException {
145145
if (StringUtils.isNotEmpty(title)) {
146-
output.append(String.format(" -----%n %1$s%n -----%n%n%1$s%n%n", title));
146+
appendFormat(" -----%n %1$s%n -----%n%n%1$s%n%n", title);
147147
}
148148
}
149149
}

src/test/java/org/apache/commons/cli/example/AptHelpAppendableTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public class AptHelpAppendableTest {
3636
private final StringBuilder sb = new StringBuilder();
3737
private final AptHelpAppendable underTest = new AptHelpAppendable(sb);
3838

39+
@Test
40+
public void testAppendFormatTest() throws IOException {
41+
sb.setLength(0);
42+
underTest.appendFormat("Big %s and Phantom %,d", "Joe", 309);
43+
assertEquals(String.format("Big Joe and Phantom 309"), sb.toString());
44+
}
45+
3946
@Test
4047
public void testAppendHeaderTest() throws IOException {
4148
sb.setLength(0);
@@ -59,6 +66,13 @@ public void testAppendListTest() throws IOException {
5966
assertEquals(String.format(" * one%n * two%n * three%n%n"), sb.toString());
6067
}
6168

69+
@Test
70+
public void testAppendParagraphFormatTest() throws IOException {
71+
sb.setLength(0);
72+
underTest.appendParagraphFormat("Hello %s World %,d", "Big Joe", 309);
73+
assertEquals(String.format(" Hello Big Joe World 309%n%n"), sb.toString());
74+
}
75+
6276
@Test
6377
public void testAppendParagraphTest() throws IOException {
6478
sb.setLength(0);

src/test/java/org/apache/commons/cli/example/XhtmlHelpAppendable.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,53 @@ public void appendHeader(final int level, final CharSequence text) throws IOExce
4646
if (level < 1) {
4747
throw new IllegalArgumentException("level must be at least 1");
4848
}
49-
output.append(String.format("<h%s>%s</h%1$s>%n", level, StringEscapeUtils.escapeHtml4(text.toString())));
49+
appendFormat("<h%s>%s</h%1$s>%n", level, StringEscapeUtils.escapeHtml4(text.toString()));
5050
}
5151
}
5252

5353
@Override
5454
public void appendList(final boolean ordered, final Collection<CharSequence> list) throws IOException {
55-
output.append(String.format("<%sl>%n", ordered ? "o" : "u"));
55+
appendFormat("<%sl>%n", ordered ? "o" : "u");
5656
for (final CharSequence line : list) {
57-
output.append(String.format(" <li>%s</li>%n", StringEscapeUtils.escapeHtml4(StringUtils.defaultIfEmpty(line, "").toString())));
57+
appendFormat(" <li>%s</li>%n", StringEscapeUtils.escapeHtml4(StringUtils.defaultIfEmpty(line, "").toString()));
5858
}
59-
output.append(String.format("</%sl>%n", ordered ? "o" : "u"));
59+
appendFormat("</%sl>%n", ordered ? "o" : "u");
6060
}
6161

6262
@Override
6363
public void appendParagraph(final CharSequence paragraph) throws IOException {
6464
if (StringUtils.isNotEmpty(paragraph)) {
65-
output.append(String.format("<p>%s</p>%n", StringEscapeUtils.escapeHtml4(paragraph.toString())));
65+
appendFormat("<p>%s</p>%n", StringEscapeUtils.escapeHtml4(paragraph.toString()));
6666
}
6767
}
6868

6969
@Override
7070
public void appendTable(final TableDefinition table) throws IOException {
71-
output.append(String.format("<table class='commons_cli_table'>%n"));
71+
appendFormat("<table class='commons_cli_table'>%n");
7272
if (StringUtils.isNotEmpty(table.caption())) {
73-
output.append(String.format(" <caption>%s</caption>%n", StringEscapeUtils.escapeHtml4(table.caption())));
73+
appendFormat(" <caption>%s</caption>%n", StringEscapeUtils.escapeHtml4(table.caption()));
7474
}
7575
// write the headers
7676
if (!table.headers().isEmpty()) {
77-
output.append(String.format(" <tr>%n"));
77+
appendFormat(" <tr>%n");
7878
for (final String header : table.headers()) {
79-
output.append(String.format(" <th>%s</th>%n", StringEscapeUtils.escapeHtml4(header)));
79+
appendFormat(" <th>%s</th>%n", StringEscapeUtils.escapeHtml4(header));
8080
}
81-
output.append(String.format(" </tr>%n"));
81+
appendFormat(" </tr>%n");
8282
}
8383
// write the data
8484
for (final List<String> row : table.rows()) {
85-
output.append(String.format(" <tr>%n"));
85+
appendFormat(" <tr>%n");
8686
for (final String column : row) {
87-
output.append(String.format(" <td>%s</td>%n", StringEscapeUtils.escapeHtml4(column)));
87+
appendFormat(" <td>%s</td>%n", StringEscapeUtils.escapeHtml4(column));
8888
}
89-
output.append(String.format(" </tr>%n"));
89+
appendFormat(" </tr>%n");
9090
}
91-
output.append(String.format("</table>%n"));
91+
appendFormat("</table>%n");
9292
}
9393

9494
@Override
9595
public void appendTitle(final CharSequence title) throws IOException {
96-
output.append(String.format("<span class='commons_cli_title'>%s</span>%n", StringEscapeUtils.escapeHtml4(title.toString())));
96+
appendFormat("<span class='commons_cli_title'>%s</span>%n", StringEscapeUtils.escapeHtml4(title.toString()));
9797
}
9898
}

src/test/java/org/apache/commons/cli/example/XhtmlHelpAppendableTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public void testAppendListTest() throws IOException {
5959
assertEquals(String.format("<ul>%n <li>one</li>%n <li>two</li>%n <li>three</li>%n</ul>%n"), sb.toString());
6060
}
6161

62+
@Test
63+
public void testAppendParagraphFormatTest() throws IOException {
64+
sb.setLength(0);
65+
underTest.appendParagraphFormat("Hello %s World %,d", "Joe", 309);
66+
assertEquals(String.format("<p>Hello Joe World 309</p>%n"), sb.toString());
67+
}
68+
6269
@Test
6370
public void testAppendParagraphTest() throws IOException {
6471
sb.setLength(0);

0 commit comments

Comments
 (0)