Skip to content

Commit c113423

Browse files
committed
[CLI-339] Help formatter extension #314
- Update changes.xml - Sort members - Only use Javadoc @SInCE tags for public and protected elements - Javadoc: Break before tags - Javadoc: Write complete sentences. - Javadoc: Use @code - Javadoc: Sentences start with a capital letter - Add missing @OverRide annotation - Use final - Rename asArgName to toArgName (it calls another toArgName) - Whitespace - Use longer lines - Normalize test method names - Name test methods after the methods they test
1 parent e9a72ea commit c113423

21 files changed

Lines changed: 1679 additions & 1562 deletions

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<release version="1.9.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
2626
<!-- FIX -->
2727
<!-- ADD -->
28+
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension #314.</action>
2829
<!-- UPDATE -->
2930
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 72 to 77 #302, #304, #310, #315, #320.</action>
3031
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump commons-io:commons-io from 2.16.1 to 2.17.0 #309.</action>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ Licensed to the Apache Software Foundation (ASF) under one or more
5454
* <p>
5555
* This produces the following output:
5656
* </p>
57-
* <pre>
58-
* usage: myapp -f &lt;FILE&gt; [-h] [-v]
57+
* <pre>{@code
58+
* usage: myapp -f <FILE> [-h] [-v]
5959
* Do something useful with an input file
6060
*
61-
* -f,--file &lt;FILE&gt; The file to be processed
61+
* -f,--file <FILE> The file to be processed
6262
* -h,--help
6363
* -v,--version Print the version of the application
6464
*
6565
* Please report issues at https://example.com/issues
66-
* </pre>
67-
* @deprecated Use {@link org.apache.commons.cli.help.HelpFormatter}
66+
* }</pre>
67+
* @deprecated Use {@link org.apache.commons.cli.help.HelpFormatter}.
6868
*/
6969
@Deprecated
7070
public class HelpFormatter {

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

Lines changed: 110 additions & 99 deletions
Large diffs are not rendered by default.

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,34 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020

2121
/**
2222
* An abstract implementation of {@link HelpWriter} that writes output to an {@link Appendable} instance.
23+
*
2324
* @since 1.10.0
2425
*/
2526
public abstract class AbstractHelpWriter implements HelpWriter {
27+
2628
/**
2729
* The Appendable instance to write to.
2830
*/
2931
protected final Appendable output;
3032

3133
/**
3234
* Constructs an instance using the provided Appendable instance.
35+
*
3336
* @param output the Appendable instance to write to.
3437
*/
3538
protected AbstractHelpWriter(final Appendable output) {
3639
this.output = output;
3740
}
3841

3942
@Override
40-
public Appendable append(final CharSequence text) throws IOException {
41-
output.append(text);
43+
public Appendable append(final char ch) throws IOException {
44+
output.append(ch);
4245
return this;
4346
}
4447

4548
@Override
46-
public Appendable append(final char ch) throws IOException {
47-
output.append(ch);
49+
public Appendable append(final CharSequence text) throws IOException {
50+
output.append(text);
4851
return this;
4952
}
5053

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

Lines changed: 85 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2929
* <p>
3030
* Example:
3131
* </p>
32+
*
3233
* <pre>
3334
* Options options = new Options();
3435
* options.addOption(OptionBuilder.withLongOpt("file").withDescription("The file to be processed").hasArg().withArgName("FILE").isRequired().create('f'));
@@ -44,6 +45,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4445
* <p>
4546
* This produces the following output:
4647
* </p>
48+
*
4749
* <pre>
4850
* {@code
4951
* usage: myapp -f <FILE> [-h] [-v]
@@ -56,47 +58,41 @@ Licensed to the Apache Software Foundation (ASF) under one or more
5658
* Please report issues at https://example.com/issues
5759
* }
5860
* </pre>
61+
*
5962
* @since 1.10.0
6063
*/
6164
public class HelpFormatter extends AbstractHelpFormatter {
62-
/** Default number of characters per line */
63-
public static final int DEFAULT_WIDTH = 74;
64-
65-
/** Default padding to the left of each line */
66-
public static final int DEFAULT_LEFT_PAD = 1;
67-
68-
/** Number of space characters to be prefixed to each description line */
69-
public static final int DEFAULT_INDENT = 3;
70-
71-
/** The default number of spaces between columns in the options table */
72-
public static final int DEFAULT_COLUMN_SPACING = 5;
73-
74-
/** If {@code true} show the "Since" column, otherwise ignore it. */
75-
private final boolean showSince;
7665

7766
/**
78-
* A builder for the HelpFormatter. Intended to make more complex uses of the HelpFormatter class easier.
79-
* Default values are:
67+
* A builder for the HelpFormatter. Intended to make more complex uses of the HelpFormatter class easier. Default values are:
8068
* <ul>
81-
* <li>showSince = true</li>
82-
* <li>helpWriter = a {@link TextHelpWriter} writing to {@code System.out}</li>
83-
* <li>optionFormatter.Builder = the default {@link OptionFormatter.Builder}</li>
69+
* <li>showSince = true</li>
70+
* <li>helpWriter = a {@link TextHelpWriter} writing to {@code System.out}</li>
71+
* <li>optionFormatter.Builder = the default {@link OptionFormatter.Builder}</li>
8472
* </ul>
8573
*/
8674
public static class Builder {
75+
8776
/** If {@code true} show the "Since" column, otherwise ignore it. */
8877
private boolean showSince;
78+
8979
/** The {@link HelpWriter} to use */
9080
private HelpWriter helpWriter;
81+
9182
/** The {@link OptionFormatter.Builder} to use to format options in the table. */
9283
private OptionFormatter.Builder optionFormatBuilder;
84+
9385
/** The string to separate option groups with */
9486
private String optionGroupSeparator;
87+
9588
/** The comparator to sort lists of options */
9689
private Comparator<Option> comparator;
90+
9791
/**
98-
* Constructor.
99-
* <p>sets the showSince to {@code true}</p>
92+
* Constructs a new instace.
93+
* <p>
94+
* Sets {@code showSince} to {@code true}.
95+
* </p>
10096
*/
10197
public Builder() {
10298
showSince = true;
@@ -105,27 +101,29 @@ public Builder() {
105101
}
106102

107103
/**
108-
* Sets the showSince flag.
109-
* @param showSince the desired value of the showSince flag.
104+
* Constructs the {@link HelpFormatter}.
105+
*
110106
* @return this.
111107
*/
112-
public Builder setShowSince(final boolean showSince) {
113-
this.showSince = showSince;
114-
return this;
108+
public HelpFormatter build() {
109+
validate();
110+
return new HelpFormatter(this);
115111
}
116112

117113
/**
118-
* Sets the {@link HelpWriter}.
119-
* @param helpWriter the {@link HelpWriter} to use.
114+
* Sets the comparator to use for sorting opitons. If set to {@code null} no sorting is performed.
115+
*
116+
* @param comparator The comparator to use for sorting opitons.
120117
* @return this
121118
*/
122-
public Builder setSerializer(final HelpWriter helpWriter) {
123-
this.helpWriter = helpWriter;
119+
public Builder setComparator(final Comparator<Option> comparator) {
120+
this.comparator = comparator;
124121
return this;
125122
}
126123

127124
/**
128125
* Sets the {@link OptionFormatter.Builder}.
126+
*
129127
* @param optionFormatBuilder the {@link OptionFormatter.Builder} to use.
130128
* @return this
131129
*/
@@ -135,8 +133,8 @@ public Builder setOptionFormatBuilder(final OptionFormatter.Builder optionFormat
135133
}
136134

137135
/**
138-
* Sets the OptionGroup separator. Normally " | " or something similar to denote that only one option may
139-
* be chosen.
136+
* Sets the OptionGroup separator. Normally " | " or something similar to denote that only one option may be chosen.
137+
*
140138
* @param optionGroupSeparator the string to separate option group elements with.
141139
* @return this
142140
*/
@@ -146,17 +144,30 @@ public Builder setOptionGroupSeparator(final String optionGroupSeparator) {
146144
}
147145

148146
/**
149-
* Sets the comparator to use for sorting opitons. If set to {@code null} no sorting is performed.
150-
* @param comparator The comparator to use for sorting opitons.
147+
* Sets the {@link HelpWriter}.
148+
*
149+
* @param helpWriter the {@link HelpWriter} to use.
151150
* @return this
152151
*/
153-
public Builder setComparator(final Comparator<Option> comparator) {
154-
this.comparator = comparator;
152+
public Builder setSerializer(final HelpWriter helpWriter) {
153+
this.helpWriter = helpWriter;
154+
return this;
155+
}
156+
157+
/**
158+
* Sets the showSince flag.
159+
*
160+
* @param showSince the desired value of the showSince flag.
161+
* @return this.
162+
*/
163+
public Builder setShowSince(final boolean showSince) {
164+
this.showSince = showSince;
155165
return this;
156166
}
157167

158168
/**
159169
* Performs a sanity check and sets default values if they are not set.
170+
*
160171
* @return this.
161172
*/
162173
private Builder validate() {
@@ -168,55 +179,63 @@ private Builder validate() {
168179
}
169180
return this;
170181
}
171-
172-
/**
173-
* Constructs the {@link HelpFormatter}.
174-
* @return this.
175-
*/
176-
public HelpFormatter build() {
177-
validate();
178-
return new HelpFormatter(this);
179-
}
180182
}
181183

184+
/** Default number of characters per line */
185+
public static final int DEFAULT_WIDTH = 74;
186+
187+
/** Default padding to the left of each line */
188+
public static final int DEFAULT_LEFT_PAD = 1;
189+
190+
/** Number of space characters to be prefixed to each description line */
191+
public static final int DEFAULT_INDENT = 3;
192+
193+
/** The default number of spaces between columns in the options table */
194+
public static final int DEFAULT_COLUMN_SPACING = 5;
195+
196+
/** If {@code true} show the "Since" column, otherwise ignore it. */
197+
private final boolean showSince;
198+
182199
/**
183200
* Constructs a new instance using the default {@link Builder}.
201+
*
184202
* @see Builder
185203
*/
186204
public HelpFormatter() {
187205
this(new Builder().validate());
188206
}
189207

190-
/**
191-
* Convenience constructor to create an instance using the specified {@link HelpWriter} and the
192-
* remaining default {@link Builder}.
193-
* @param helpWriter the {@link HelpWriter} to use.
194-
*/
195-
public HelpFormatter(final HelpWriter helpWriter) {
196-
this(new Builder().setSerializer(helpWriter).validate());
197-
}
198-
199208
/**
200209
* Constructs the Help formatter.
210+
*
201211
* @param builder the Builder to build from.
202212
*/
203213
private HelpFormatter(final Builder builder) {
204-
super(builder.helpWriter, builder.optionFormatBuilder, builder.comparator,
205-
builder.optionGroupSeparator);
214+
super(builder.helpWriter, builder.optionFormatBuilder, builder.comparator, builder.optionGroupSeparator);
206215

207216
this.showSince = builder.showSince;
208217
}
209218

219+
/**
220+
* Convenience constructor to create an instance using the specified {@link HelpWriter} and the remaining default {@link Builder}.
221+
*
222+
* @param helpWriter the {@link HelpWriter} to use.
223+
*/
224+
public HelpFormatter(final HelpWriter helpWriter) {
225+
this(new Builder().setSerializer(helpWriter).validate());
226+
}
227+
210228
/**
211229
* Gets the table definition for the options.
230+
*
212231
* @param options the collection of {@link Option} instances to create the table from.
213232
* @return A {@link TableDefinition} to display the options.
214233
*/
234+
@Override
215235
public TableDefinition getTableDefinition(final Iterable<Option> options) {
216236
// set up the base TextStyle for the columns configured for the Option opt and arg values..
217-
TextStyle.Builder builder = new TextStyle.Builder().setAlignment(TextStyle.Alignment.LEFT)
218-
.setIndent(DEFAULT_LEFT_PAD).setScalable(false);
219-
List<TextStyle> styles = new ArrayList<>();
237+
final TextStyle.Builder builder = new TextStyle.Builder().setAlignment(TextStyle.Alignment.LEFT).setIndent(DEFAULT_LEFT_PAD).setScalable(false);
238+
final List<TextStyle> styles = new ArrayList<>();
220239
styles.add(builder.get());
221240
// set up showSince column
222241
builder.setScalable(true).setLeftPad(DEFAULT_COLUMN_SPACING);
@@ -229,12 +248,12 @@ public TableDefinition getTableDefinition(final Iterable<Option> options) {
229248
styles.add(builder.get());
230249

231250
// setup the rows for the table.
232-
List<List<String>> rows = new ArrayList<>();
233-
StringBuilder sb = new StringBuilder();
234-
for (Option option : options) {
235-
List<String> row = new ArrayList<>();
251+
final List<List<String>> rows = new ArrayList<>();
252+
final StringBuilder sb = new StringBuilder();
253+
for (final Option option : options) {
254+
final List<String> row = new ArrayList<>();
236255
// create an option formatter to correctly format the parts of the option
237-
OptionFormatter formatter = optionFormatBuilder.build(option);
256+
final OptionFormatter formatter = optionFormatBuilder.build(option);
238257
sb.setLength(0);
239258
// append the opt values.
240259
sb.append(formatter.getBothOpt());
@@ -253,7 +272,7 @@ public TableDefinition getTableDefinition(final Iterable<Option> options) {
253272
}
254273

255274
// return the TableDefinition with the proper column headers.
256-
return showSince ? TableDefinition.from("", styles, Arrays.asList("Options", "Since", "Description"), rows) :
257-
TableDefinition.from("", styles, Arrays.asList("Options", "Description"), rows);
275+
return showSince ? TableDefinition.from("", styles, Arrays.asList("Options", "Since", "Description"), rows)
276+
: TableDefinition.from("", styles, Arrays.asList("Options", "Description"), rows);
258277
}
259278
}

0 commit comments

Comments
 (0)