Skip to content

Commit 821b85f

Browse files
author
John Keyes
committed
added Pete Maddocks fix for options only created with a longopt
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129813 13f79535-47bb-0310-9956-ffa450edef68
1 parent 79d5a46 commit 821b85f

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,22 @@ protected StringBuffer renderOptions( StringBuffer sb,
338338
{
339339
option = (Option) i.next();
340340
optBuf = new StringBuffer(8);
341-
optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
342-
if ( option.hasLongOpt() )
341+
342+
if (option.getOpt().equals(" "))
343+
{
344+
optBuf.append(lpad).append(" " + defaultLongOptPrefix).append(option.getLongOpt());
345+
}
346+
else
343347
{
344-
optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
348+
optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
349+
if ( option.hasLongOpt() )
350+
{
351+
optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
352+
}
353+
345354
}
355+
356+
346357
if ( option.hasArg() )
347358
{
348359
//FIXME - should have a way to specify arg name per option
@@ -374,14 +385,11 @@ protected StringBuffer renderOptions( StringBuffer sb,
374385
for ( Iterator i = prefixList.iterator(); i.hasNext(); )
375386
{
376387
optBuf = (StringBuffer) i.next();
377-
opt = optBuf.toString();
388+
opt = optBuf.toString().trim();
378389
if( opt.indexOf( ',' ) != -1 ) {
379-
opt = opt.substring( optOffset, opt.indexOf( ',', optOffset ) );
390+
opt = opt.substring(0, opt.indexOf( ',', optOffset ) );
380391
}
381-
else {
382-
opt = opt.substring( optOffset, opt.indexOf( ' ', optOffset ) );
383-
}
384-
option = options.getOption( "-" + opt );
392+
option = options.getOption(opt);
385393

386394
renderWrappedText(sb, width, nextLineTabStop,
387395
optBuf.append(option.getDescription()).toString());
@@ -509,12 +517,26 @@ protected String rtrim( String s )
509517

510518
// ----------------------------------------------------------- Inner classes
511519

512-
private static class StringBufferComparator
513-
implements Comparator
514-
{
515-
public int compare( Object o1, Object o2 )
516-
{
517-
return ((StringBuffer) o1).toString().compareTo(((StringBuffer) o2).toString());
518-
}
519-
}
520+
private static class StringBufferComparator
521+
implements Comparator
522+
{
523+
public int compare( Object o1, Object o2 )
524+
{
525+
String str1 = stripPrefix(o1.toString());
526+
String str2 = stripPrefix(o2.toString());
527+
return (str1.compareTo(str2));
528+
}
529+
530+
private String stripPrefix(String strOption)
531+
{
532+
// Strip any leading '-' characters
533+
int iStartIndex = strOption.lastIndexOf('-');
534+
if (iStartIndex == -1)
535+
{
536+
iStartIndex = 0;
537+
}
538+
return strOption.substring(iStartIndex);
539+
540+
}
541+
}
520542
}

0 commit comments

Comments
 (0)