@@ -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 */
6164public 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