Skip to content

Commit 6bcbf15

Browse files
author
John Keyes
committed
applied some fixes to HelpFormatter, thanks to Rob and Boon
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129850 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5dae769 commit 6bcbf15

1 file changed

Lines changed: 81 additions & 44 deletions

File tree

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

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ public void printHelp( PrintWriter pw,
220220
* @param width ??
221221
* @param appName The application name
222222
* @param options The command line Options
223+
* @see #appendOptionGroup(StringBuffer,OptionGroup)
224+
* @see #appendOption(StringBuffer,Option,boolean)
223225
*
224226
*/
225227
public void printUsage( PrintWriter pw, int width, String app, Options options )
@@ -228,11 +230,11 @@ public void printUsage( PrintWriter pw, int width, String app, Options options )
228230
StringBuffer buff = new StringBuffer( defaultSyntaxPrefix ).append( app ).append( " " );
229231

230232
// create a list for processed option groups
231-
ArrayList list = new ArrayList();
233+
final Collection processedGroups = new ArrayList();
232234

233235
// temp variable
234236
Option option;
235-
237+
236238
// iterate over the options
237239
for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
238240
{
@@ -242,50 +244,24 @@ public void printUsage( PrintWriter pw, int width, String app, Options options )
242244
// check if the option is part of an OptionGroup
243245
OptionGroup group = options.getOptionGroup( option );
244246

245-
// if the option is part of a group and the group has not already
246-
// been processed
247-
if( group != null && !list.contains(group)) {
248-
249-
// add the group to the processed list
250-
list.add( group );
251-
252-
// get the names of the options from the OptionGroup
253-
Collection names = group.getNames();
254-
255-
buff.append( "[" );
256-
257-
// for each option in the OptionGroup
258-
for( Iterator iter = names.iterator(); iter.hasNext(); ) {
259-
buff.append( iter.next() );
260-
if( iter.hasNext() ) {
261-
buff.append( " | " );
262-
}
247+
// if the option is part of a group
248+
if( group != null) {
249+
// and if the group has not already been processed
250+
if( !processedGroups.contains(group) ) {
251+
// add the group to the processed list
252+
processedGroups.add( group );
253+
// add the usage clause
254+
appendOptionGroup( buff, group );
263255
}
264-
buff.append( "]" );
256+
// otherwise the option was displayed in the group
257+
// previously so ignore it.
265258
}
266259
// if the Option is not part of an OptionGroup
267260
else {
268-
// if the Option is not a required option
269-
if( !option.isRequired() ) {
270-
buff.append( "[" );
271-
}
272-
273-
if( !" ".equals( option.getOpt() ) ) {
274-
buff.append( "-" ).append( option.getOpt() );
275-
}
276-
else {
277-
buff.append( "--" ).append( option.getLongOpt() );
278-
}
279-
280-
// if the Option has a value
281-
if( option.hasArg() && option.getArgName() != null ) {
282-
buff.append( " " ).append( option.getArgName() );
283-
}
284-
285-
// if the Option is not a required option
286-
if( !option.isRequired() ) {
287-
buff.append( "]" );
288-
}
261+
appendOption( buff, option, option.isRequired() );
262+
}
263+
264+
if( i.hasNext() ){
289265
buff.append( " " );
290266
}
291267
}
@@ -294,6 +270,67 @@ public void printUsage( PrintWriter pw, int width, String app, Options options )
294270
printWrapped( pw, width, buff.toString().indexOf(' ')+1,
295271
buff.toString() );
296272
}
273+
274+
/**
275+
* Appends the usage clause for an OptionGroup to a StringBuffer.
276+
* The clause is wrapped in square brackets if the group is required.
277+
* The display of the options is handled by appendOption
278+
* @param buff the StringBuffer to append to
279+
* @param group the group to append
280+
* @see #appendOption(StringBuffer,Option,boolean)
281+
*/
282+
private static void appendOptionGroup( final StringBuffer buff, final OptionGroup group )
283+
{
284+
if( !group.isRequired() ) {
285+
buff.append( "[" );
286+
}
287+
288+
// for each option in the OptionGroup
289+
for( Iterator i = group.getOptions().iterator(); i.hasNext(); ) {
290+
// whether the option is required or not is handled at group level
291+
appendOption( buff, (Option)i.next(), true);
292+
if( i.hasNext() ) {
293+
buff.append( " | " );
294+
}
295+
}
296+
297+
if( !group.isRequired() ) {
298+
buff.append( "]" );
299+
}
300+
}
301+
302+
/**
303+
* Appends the usage clause for an Option to a StringBuffer.
304+
* The clause is wrapped in square brackets if the group is required.
305+
* The display of the options is handled by appendOption
306+
* @param buff the StringBuffer to append to
307+
* @param group the group to append
308+
* @see #appendOption(StringBuffer,Option,boolean)
309+
*/
310+
private static void appendOption( final StringBuffer buff, final Option option, final boolean required)
311+
{
312+
if( !required ) {
313+
buff.append( "[" );
314+
}
315+
316+
if( option.getOpt() != null ) {
317+
buff.append( "-" ).append( option.getOpt() );
318+
}
319+
else {
320+
buff.append( "--" ).append( option.getLongOpt() );
321+
}
322+
323+
// if the Option has a value
324+
if( option.hasArg() && option.getArgName() != null ) {
325+
buff.append( " <" ).append( option.getArgName() ).append( ">" );
326+
}
327+
328+
// if the Option is not a required option
329+
if( !required ) {
330+
buff.append( "]" );
331+
}
332+
}
333+
297334

298335
public void printUsage( PrintWriter pw, int width, String cmdLineSyntax )
299336
{
@@ -346,7 +383,7 @@ protected StringBuffer renderOptions( StringBuffer sb,
346383
option = (Option) i.next();
347384
optBuf = new StringBuffer(8);
348385

349-
if (option.getOpt().equals(" "))
386+
if ( option.getOpt() == null )
350387
{
351388
optBuf.append(lpad).append(" " + defaultLongOptPrefix).append(option.getLongOpt());
352389
}
@@ -362,7 +399,7 @@ protected StringBuffer renderOptions( StringBuffer sb,
362399

363400
if( option.hasArg() ) {
364401
if( option.hasArgName() ) {
365-
optBuf.append(" <").append( option.getArgName() ).append( '>' );
402+
optBuf.append(" <").append( option.getArgName() ).append( ">" );
366403
}
367404
else {
368405
optBuf.append(' ');

0 commit comments

Comments
 (0)