Skip to content

Commit 62d1ba7

Browse files
committed
Normalize builder pattern
- Don't mix builder patterns - Reduce new public API footprint - Remove unused elements - Refactor magic number into a constant - Javadoc package info - Don't need both an array constant and a set constant - Make that set immutable - Javadoc semantic fixes - Use same naming pattern as Java: indexOf... - Don't need Javadoc since tag on a method when the class has it for the same version - Rename example package - Reuse StringUtils from tests - Fix generics compiler warnings in tests - Remove dangling commas in array declarations
1 parent aa8360c commit 62d1ba7

18 files changed

Lines changed: 383 additions & 355 deletions

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

Lines changed: 130 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1616
*/
1717
package org.apache.commons.cli.help;
1818

19-
import static java.lang.String.format;
20-
2119
import java.io.IOException;
2220
import java.util.ArrayList;
2321
import java.util.Collection;
@@ -26,32 +24,149 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2624
import java.util.List;
2725
import java.util.Objects;
2826
import java.util.function.Function;
27+
import java.util.function.Supplier;
2928

3029
import org.apache.commons.cli.Option;
3130
import org.apache.commons.cli.OptionGroup;
3231
import org.apache.commons.cli.Options;
3332

3433
/**
35-
* The class for help formatters provides the framework to link the {@link HelpAppendable} with the {@link OptionFormatter} and a default
36-
* {@link TableDefinition} so to produce a standard format help page.
34+
* Helps formatters provides the framework to link a {@link HelpAppendable} with a {@link OptionFormatter} and a default {@link TableDefinition} so to produce
35+
* standardized format help output.
3736
*
3837
* @since 1.10.0
3938
*/
4039
public abstract class AbstractHelpFormatter {
4140

42-
/** The string to display at the beginning of the usage statement */
43-
public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
44-
4541
/**
46-
* The default separator between {@link OptionGroup} elements.
42+
* Abstracts building instances for subclasses.
43+
* <ul>
44+
* <li>helpAppendable = a {@link TextHelpAppendable} writing to {@code System.out}</li>
45+
* <li>optionFormatter.Builder = the default {@link OptionFormatter.Builder}</li>
46+
* </ul>
47+
*
48+
* @param <B> The builder type.
49+
* @param <T> The type to build.
4750
*/
48-
public static final String DEFAULT_OPTION_GROUP_SEPARATOR = " | ";
51+
public abstract static class Builder<B extends Builder<B, T>, T extends AbstractHelpFormatter> implements Supplier<T> {
52+
53+
/** The comparator to sort lists of options */
54+
private Comparator<Option> comparator = DEFAULT_COMPARATOR;
55+
56+
/** The {@link HelpAppendable} to use */
57+
private HelpAppendable helpAppendable = defaultTextHelpAppendable();
58+
59+
/** The {@link OptionFormatter.Builder} to use to format options in the table. */
60+
private OptionFormatter.Builder optionFormatBuilder = defaultOptionFormatterBuilder();
61+
62+
/** The string to separate option groups with */
63+
private String optionGroupSeparator = DEFAULT_OPTION_GROUP_SEPARATOR;
64+
65+
/**
66+
* Constructs a new instace.
67+
* <p>
68+
* Sets {@code showSince} to {@code true}.
69+
* </p>
70+
*/
71+
protected Builder() {
72+
// empty
73+
}
74+
75+
/**
76+
* Returns this instance cast to {@code B}.
77+
*
78+
* @return this instance cast to {@code B}.
79+
*/
80+
@SuppressWarnings("unchecked")
81+
protected B asThis() {
82+
return (B) this;
83+
}
84+
85+
protected OptionFormatter.Builder defaultOptionFormatterBuilder() {
86+
return new OptionFormatter.Builder();
87+
}
88+
89+
protected TextHelpAppendable defaultTextHelpAppendable() {
90+
return new TextHelpAppendable(System.out);
91+
}
92+
93+
protected Comparator<Option> getComparator() {
94+
return comparator;
95+
}
96+
97+
protected HelpAppendable getHelpAppendable() {
98+
return helpAppendable;
99+
}
100+
101+
protected OptionFormatter.Builder getOptionFormatBuilder() {
102+
return optionFormatBuilder;
103+
}
104+
105+
protected String getOptionGroupSeparator() {
106+
return optionGroupSeparator;
107+
}
108+
109+
/**
110+
* Sets the comparator to use for sorting options. If set to {@code null} no sorting is performed.
111+
*
112+
* @param comparator The comparator to use for sorting options.
113+
* @return this
114+
*/
115+
public B setComparator(final Comparator<Option> comparator) {
116+
this.comparator = comparator;
117+
return asThis();
118+
}
119+
120+
/**
121+
* Sets the {@link HelpAppendable}.
122+
*
123+
* @param helpAppendable the {@link HelpAppendable} to use.
124+
* @return this
125+
*/
126+
public B setHelpAppendable(final HelpAppendable helpAppendable) {
127+
this.helpAppendable = helpAppendable != null ? helpAppendable : defaultTextHelpAppendable();
128+
return asThis();
129+
}
130+
131+
/**
132+
* Sets the {@link OptionFormatter.Builder}.
133+
*
134+
* @param optionFormatBuilder the {@link OptionFormatter.Builder} to use.
135+
* @return this
136+
*/
137+
public B setOptionFormatBuilder(final OptionFormatter.Builder optionFormatBuilder) {
138+
this.optionFormatBuilder = optionFormatBuilder != null ? optionFormatBuilder : defaultOptionFormatterBuilder();
139+
return asThis();
140+
}
141+
142+
/**
143+
* Sets the OptionGroup separator. Normally " | " or something similar to denote that only one option may be chosen.
144+
*
145+
* @param optionGroupSeparator the string to separate option group elements with.
146+
* @return this
147+
*/
148+
public B setOptionGroupSeparator(final String optionGroupSeparator) {
149+
this.optionGroupSeparator = Util.defaultValue(optionGroupSeparator, "");
150+
return asThis();
151+
}
152+
153+
}
49154

50155
/**
51156
* The default comparator for {@link Option} implementations.
52157
*/
53158
public static final Comparator<Option> DEFAULT_COMPARATOR = (opt1, opt2) -> opt1.getKey().compareToIgnoreCase(opt2.getKey());
54159

160+
/**
161+
* The default separator between {@link OptionGroup} elements.
162+
*/
163+
public static final String DEFAULT_OPTION_GROUP_SEPARATOR = " | ";
164+
165+
/** The string to display at the beginning of the usage statement */
166+
public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
167+
168+
/** The comparator for sorting {@link Option} collections */
169+
protected Comparator<Option> comparator;
55170
/**
56171
* The {@link HelpAppendable} that produces the final output.
57172
*/
@@ -60,17 +175,15 @@ public abstract class AbstractHelpFormatter {
60175
* The OptionFormatter.Builder used to display options within the help page
61176
*/
62177
protected final OptionFormatter.Builder optionFormatBuilder;
178+
179+
/** The separator between {@link OptionGroup} components. */
180+
protected final String optionGroupSeparator;
181+
63182
/**
64183
* The phrase printed before the syntax line.
65184
*/
66185
protected String syntaxPrefix = DEFAULT_SYNTAX_PREFIX;
67186

68-
/** The comparator for sorting {@link Option} collections */
69-
protected Comparator<Option> comparator;
70-
71-
/** The separator between {@link OptionGroup} components. */
72-
protected final String optionGroupSeparator;
73-
74187
/**
75188
* Constructs the base formatter.
76189
*
@@ -148,9 +261,9 @@ public void printHelp(final String cmdLineSyntax, final String header, final Ite
148261
throw new IllegalArgumentException("cmdLineSyntax not provided");
149262
}
150263
if (autoUsage) {
151-
helpAppendable.appendParagraph(format("%s %s %s", syntaxPrefix, cmdLineSyntax, toSyntaxOptions(options)));
264+
helpAppendable.appendParagraph(String.format("%s %s %s", syntaxPrefix, cmdLineSyntax, toSyntaxOptions(options)));
152265
} else {
153-
helpAppendable.appendParagraph(format("%s %s", syntaxPrefix, cmdLineSyntax));
266+
helpAppendable.appendParagraph(String.format("%s %s", syntaxPrefix, cmdLineSyntax));
154267
}
155268
if (!Util.isEmpty(header)) {
156269
helpAppendable.appendParagraph(header);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ protected FilterHelpAppendable(final Appendable output) {
4949
}
5050

5151
@Override
52-
public Appendable append(final char ch) throws IOException {
52+
public FilterHelpAppendable append(final char ch) throws IOException {
5353
output.append(ch);
5454
return this;
5555
}
5656

5757
@Override
58-
public Appendable append(final CharSequence text) throws IOException {
58+
public FilterHelpAppendable append(final CharSequence text) throws IOException {
5959
output.append(text);
6060
return this;
6161
}
6262

6363
@Override
64-
public Appendable append(final CharSequence csq, final int start, final int end) throws IOException {
64+
public FilterHelpAppendable append(final CharSequence csq, final int start, final int end) throws IOException {
6565
output.append(csq, start, end);
6666
return this;
6767
}

0 commit comments

Comments
 (0)